How to center a navigation bar properly? [closed]

人走茶凉 提交于 2019-12-25 04:46:09

问题


I understand, lots of people have asked this question on how to center a navigation bar but when I apply this CSS, it centers it but it aligns the nav bar a bit to the right:

nav {
  text-align: center;
}

nav li {
  display: inline-block;
}

Could this be due to some list items having different lengths or do you think this is a different problem?


回答1:


Your <ul> most likely has a padding-left, as this is the default. Just check for it in the developer tools.

nav {
  background: #999;
  text-align: center;
}
nav li {
  background: #ccc;
  display: inline-block;
}
<nav>
  <ul>
    <li>Menu 1</li>
    <li>Menu 2</li>
    <li>Menu 3</li>
  </ul>
</nav>

Just apply padding-left:0; on the appropriate <ul> to fix this.



来源:https://stackoverflow.com/questions/31564637/how-to-center-a-navigation-bar-properly

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