how to center responsive navigation bar

主宰稳场 提交于 2021-01-28 18:32:41

问题


i cant get this navigation bar to center. Please help.

Here is my html:

<div class="nav">
            <ul id="menu">
                <li><a href="home.html">Home</a></li>
                <li><a href="latestnews.html">Latest News</a></li>
                <li><a href="resultsevents.html">Results & Events</a></li>
                <li><a href="fundraising.html">Fundraising</a></li>
                <li><a href="gallery.html">Gallery</a></li>
            </ul>
</div> 

Here is my css:

#nav {
margin:0px auto; }

ul {
display: inline-block;
list-style-type:none;
margin:0 auto;
padding:0;
position: absolute; }

li {
display:inline;
float: left;
margin-right: 1px; }

li a {
display:inline-block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: Century Gothic, Candara, Tahoma, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none; }

li:hover a {
background: #659d32; }

li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px; }

li:hover ul a:hover {
background: #659d32;
color: #fff; }

li ul {
display: none; }

li ul li {
display: block;
float: none; }

li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px; }

ul li a:hover + .hidden, .hidden:hover {
display: block; }

.show-menu {
font-family: Century Gothic, Candara, Tahoma, sans-serif;
text-decoration: none;
color: #fff;
background: #659d32;
text-align: center;
padding: 10px 0;
display: none; }

input[type=checkbox]{
display: none; }

input[type=checkbox]:checked ~ #menu{
display: block; }

@media screen and (max-width : 760px){

ul {
    position: static;
    display: none;
}

li {
    margin-bottom: 1px;
}

ul li, li a {
    width: 100%;
}

.show-menu {
    display:block;
}
}

I tried adding a div around the whole thing and setting margin to auto but that hasnt worked. Im not sure what to do.


回答1:


Firstly you're targeting #nav while it's .nav according to your html.

Then you need to set a width to your nav parent in order to have a centered position.

Working example

.nav {
    margin:0px auto;
    width:705px;
}

Edit: Since your list items and links aren't responsive, it doesn't make sense to add a percentage to the parent nav.

You can use something like this to have a responsive centered navigation:

.nav {
    margin:0px auto;
    width:90%;
}
ul {
    list-style-type:none;
    padding:0;
}
li {
    float: left;
    width:18%;
}
li a {
    width:100%;
}



回答2:


I think the reason why your auto margins didn't work is because you need to define a width on your wrapper. Also make sure that you are using the right selector # for id and . for class. Here's how id go about centering the navigation.

jsFiddle

<div class="wrapper">
<div class="nav">
            <ul id="menu">
                <li><a href="home.html">Home</a></li>
                <li><a href="latestnews.html">Latest News</a></li>
                <li><a href="resultsevents.html">Results & Events</a></li>
                <li><a href="fundraising.html">Fundraising</a></li>
                <li><a href="gallery.html">Gallery</a></li>
            </ul>
</div> 
</div>

add this to your css:

.wrapper {

    width:80%;
    margin-left:auto;
    margin-right:auto;

}



回答3:


You almost had it, use:

.nav {
  display: block;
  margin: 0 auto;
  width: 100%;
  max-width: 960px;
}

You will simply need to adjust your max-width to move the navbar into the center (depending the width of your page container).

See Example



来源:https://stackoverflow.com/questions/29400246/how-to-center-responsive-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!