如何使我的导航栏居中而不影响移动版的{j}

时间:2016-08-25 22:02:27

标签: javascript jquery html css twitter-bootstrap-3

对于Web开发以及堆栈溢出来说是全新的,所以如果我努力解释这个问题,请原谅我。

我找到了一些javascript来让我的网站在小屏幕(手机)上做出响应。然而问题是我想把我的顶部导航栏放在中心,我似乎无法使用text-align:center。因为它使按钮(仅在屏幕较小时出现)与导航栏重叠。 我该如何解决这个问题。 提前谢谢!

以下是我迄今为止写的所有内容:

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>TVG</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <style>

  ul.topnav {
    list-style-type: none;
    margin: 0 auto;

    margin-left:600px;

    padding: 0;

    overflow: hidden;


}

ul.topnav li {float: left;}

ul.topnav li a {
    display: inline-block;
    color:grey;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

ul.topnav li a:hover {background-color: #555;}

ul.topnav li.icon {display: none;}


@media screen and (max-width:680px) {
    ul.topnav {
        margin:0 auto;
    }
  ul.topnav li:not(:first-child) {display: none;}
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.topnav.responsive {position: relative;}
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}

#logodiv {
    float:left;
    padding-top:16px;
    padding-left:45px;
    padding-right:7px;
    padding-bottom:3px;
    margin-left:100px;
}

.break{

    clear:both;
    padding-top:10px;
    padding-left:7px;

    }   
  </style>
  <body>





    <div id="logodiv">
        <img src="images/bbclogo.png" />

    </div>


    <ul class="topnav mainMenu" id="myTopnav">
        <li><a href="#home">Home</a></li>
        <li><a href="#news">About</a></li>
        <li><a href="#contact">Menu</a></li>
        <li><a href="#about">Contact</a></li>
         <li class="icon">
            <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
        </li>
    </ul>

    <div class="break"> </div>



<script>

    function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}

</script>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

https://jsfiddle.net/85Lc1j6t/#&togetherjs=4Gy02O4Krl

2 个答案:

答案 0 :(得分:0)

display: inline-blockmargin: 0 auto关于您想要居中的哪个div。

答案 1 :(得分:0)

您可以使用Bootstrap的默认导航栏:http://getbootstrap.com/components/#navbar

.navbar-nav {
  width: 100%;
  text-align: center;
}
.navbar-nav > li {
  float: none;
  display: inline-block;
}

https://jsfiddle.net/7d9fqpsz/1/