css - 水平居中 CSS 菜单

标签 css menu alignment

浏览了很多关于如何水平居中菜单的文章,我似乎无法杀死野兽。是否有人可以帮助编写代码:

我可能已经完全弄乱了我所有的尝试让它对齐。 :-)

谢谢!

HTML

<div id="container">
<div id="masthead"> </div>
<div id="top-nav" >     
        <ul class="nav">
            <li><a href="#"> Europe </a></li>
            <li><a href="#"> N. America </a></li>
            <li><a href="#"> S. America </a></li>
            <li><a href="#"> Asia </a></li>
            <li><a href="#"> Africa </a></li>
            <li><a href="#"> Australia </a></li>
            <li><a href="#"> Misc </a></li></ul>
            </div>

CSS

}
#top-nav {
height: 50px;
background : transparent;
height : 77px;
list-style : none;
margin-top : 7px;
margin-bottom : 11px;
background-repeat: no-repeat;
}
ul.nav {
border-radius: 15px;
background : transparent;
height : 57px;
line-height : 31px;
list-style : none;
padding : 0 10px;
font-size : 14px;
font-weight: bolder;
}
ul.nav li {
display : inline;
padding : 0;
background : transparent;
}
ul.nav a {
font: 100%;
background: transparent;
height : 30px;
font-size : 12px;
color: #000000;
float : left;
padding : 11px 19px 11px 16px;
text-decoration : none;
border-top: 1px solid #252525;
border-bottom : 4px solid #252525;
border-left : 1px solid transparent;
border-right : 1px solid transparent;
border-radius : 4px;
-moz-border-radius : 4px;
-webkit-border-radius: 4px;
}
ul.nav a:hover {
background : #252525;
border-top : 1px solid #252525;
border-bottom : 4px solid #000;
border-left : 1px solid #252525;
border-right : 1px solid #252525;
color : #FFF;
padding : 11px 19px 11px 16px;
border-radius : 4px;
-moz-border-radius : 4px;
-webkit-border-radius: 4px;
}

最佳答案

这就是你想要的? <强> Fiddle

只需将此 CSS 规则添加到 #top-nav 元素:

text-align: center;

这是 ul.nav 元素:

display: inline-block;

关于css - 水平居中 CSS 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291701/

相关文章:

vim - 仅在 VIM 中的第一个分隔符上对齐文本

javascript - 每次刷新随机交换三种颜色

python - Django 管理面板不会呈现 CSS

html - 如何在每次加载页面时更改 html 页面的一部分

html - 如何去掉菜单中的空格

html - Div 未与边距居中对齐 :auto

javascript - jquery 获取元素的 HTML 并剥离 style 属性

reactjs - 使用扩展类作为组件时,React 中的菜单 Material UI 组件不起作用

android - 在工具栏溢出菜单中显示菜单项图标时,这种奇怪的情况是如何发生的?

html - CSS:如何水平居中和垂直居中对齐文本内容?