简体   繁体   中英

How do i make my navigation bar centered without affecting the javascript for mobile version

completely new to web development as well as stack overflow so please forgive me if i struggle to explain the issue.

I found some javascript to make my website responsive on small screens (mobiles). However the problem is i want to centre my top nav bar and i can't seem to do it with text-align:centre. Because it makes the button (which only appears when screen is small) to overlap over the nav bar. How do i go about this. Thanks in advance!

Here is all that I've written so far:

    <!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

display: inline-blockmargin: 0 auto在您要居中的div上显示。

You can use Bootstrap's default navbar : 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM