简体   繁体   English

居中导航ul li

[英]Centering Navigation ul li

I can't seem to get the main navigation bar to center align the li elements. 我似乎无法使主导航栏居中对齐li元素。 I'm only having problems when the browser is under 768px. 当浏览器低于768px时,我才遇到问题。 I've tried display: inline-block on the #nav ul and then text align to no avail. 我试过在#nav ul上进行display:inline-block,然后将文本对齐无济于事。 Here is the site: Website 这是网站: 网站

and here is the CSS in question: 这是有问题的CSS:

/* Main Navigation */   
#topmenu {float: left; height: auto; text-align: center; margin: -10px auto -10px 10px; display: block; width: 96%;}
#nav {float: left; border-left: none; margin: 10px auto 0px; text-align: center;  display: block; width: 100%;}
#nav li {float: left; margin-right: auto; border-right: none; width: auto;}
#nav li a:link, #nav li a:active, #nav li a:visited {display: block;text-decoration: none; line-height: 20px; outline: medium none; font-size: 19px; letter-spacing: -0.05em; float: left;    padding: 6px 9px 8px; text-align: center; width: auto;}
span.descmenu {display: none;}

The display:inline-block and text-align: center technique does work here, but you need to make sure that on the smaller screen your ul ( #nav ) is not floated and doesn't have a width set. display:inline-blocktext-align: center技术在这里有效,但是您需要确保在较小的屏幕上ul( #nav )没有浮动并且没有设置宽度。

@media screen and (max-width: 767px) {
  #nav {
    float: none; /* Changed from float: left*/
    border-left: none;
    margin: 10px auto 0px;
    /* text-align: center; NOT NEEDED */
    display: inline-block; /* Changed from display: block */
    /*width: 100%; Remove */
  }
}

This will center the sub heading in the navbar 这将使子标题位于导航栏中

#nav {
 text-align: center;
}

Having a look at the css it looks like a float:left or padding/margin might be overiding the li your wanting to style. 看一下css,它看起来像个float:left或padding / margin可能会覆盖您要样式化的li。

Apply these properties/values 应用这些属性/值

#nav {
    text-align: center; /*add*/
}
#nav li {
    display: inline-block; /*add*/
    text-align: left; /*add*/
    float: none; /*change left to none on max-width: 767px */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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