javascript - CSS 菜单使链接居中

标签 javascript jquery html css

我的水平菜单有这个 CSS:

nav {  
    height: 40px;  
    width: 100%;  
    background: #F00;  
    font-size: 11pt;  
    font-family: Arial;
    font-weight: bold;  
    position: relative;  
    border-bottom: 2px solid #FFFFFF;  
}  
nav ul {  
    padding: 0;  
    margin: 0 auto;  
    width: 600px;  
    height: 40px;  
}
nav li {  
    display: inline;  
    float: left;  
}  
.clearfix:before,  
.clearfix:after {  
    content: " ";  
    display: table;  
}  
.clearfix:after {  
    clear: both;  
}  
.clearfix {  
    *zoom: 1;  
}  
nav a {  
    color: #000000;  
    display: inline-block;  
    width: 100px;  
    text-align: center;  
    text-decoration: none;  
    line-height: 40px;  
}  
nav li a {  
    border-right: 1px solid #FFFFFF;  
    box-sizing:border-box;  
    -moz-box-sizing:border-box;  
    -webkit-box-sizing:border-box;  
}  
nav li:last-child a {  
    border-right: 0;  
}  
nav a:hover, nav a:active {  
    background-color: #000000;
    color:#FFFFFF;  
} 
nav a#pull {  
    display: none;  
}  
@media screen and (max-width: 600px) {  
    nav {   
        height: auto;  
    }  
    nav ul {  
        width: 100%;  
        display: block;  
        height: auto;  
    }  
    nav li {  
        width: 50%;  
        float: left;  
        position: relative;  
    }  
    nav li a {  
        border-bottom: 1px solid #FFFFFF;  
        border-right: 1px solid #FFFFFF;  
    }  
    nav a {  
        text-align: left;  
        width: 100%;  
        text-indent: 25px;  
    }  
}  
@media only screen and (max-width : 480px) {  
    nav {  
        border-bottom: 0;  
    }  
    nav ul {  
        display: none;  
        height: auto;  
    }  
    nav a#pull {  
        display: block;  
        background-color: #FF0;  
        width: 100%;  
        position: relative;  
    }  
    nav a#pull:after {  
        content:"";  
        background: url('nav-icon.png') no-repeat;  
        width: 30px;  
        height: 30px;  
        display: inline-block;  
        position: absolute;  
        rightright: 15px;  
        top: 10px;  
    }  
}  
@media only screen and (max-width : 320px) {  
    nav li {  
        display: block;  
        float: none;  
        width: 100%;  
    }  
    nav li a {  
        border-bottom: 1px solid #FFFFFF;  
    }  
}  

如何让 li 链接显示在中心?

这是菜单的一部分:http://jsfiddle.net/zJq52/

最佳答案

我会这样做:

nav ul {  
    padding: 0;  
    margin: 0 auto;  
    width: 600px;  
    height: 40px;  
    text-align: center; // add text-align
}
nav li {  
    display: inline; // no more float in here 
}  

DEMO HERE

更新 1: 使宽度自动:

nav a {  
    color: #000000;  
    display: inline-block;  
    width: auto;  // changed this from width: 100px; to auto. 
    text-align: center;  
    text-decoration: none;  
    line-height: 40px;  
}  

DEMO HERE

更新 2: 完成版!

DEMO HERE

关于javascript - CSS 菜单使链接居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19180936/

相关文章:

javascript - 我无法使用 Intl.DateTimeFormat API 更新时间

html - 使用 flexbox,我怎样才能将一个元素对齐在最左边,一个元素在最右边,另一个元素在最右边的下面?

html - 重用具有不同输入的 Angular 组件

javascript - Barba.js (Pjax.js) 和 <head> 替换

javascript - 如何验证 JSON 字符串 JQuery

javascript - 你知道有哪些视频教程完全专注于 JavaScript 中的闭包吗?

javascript - jQuery:在不使用 id 的情况下获取对特定元素的引用

javascript - RequireJS 2.0 - 定义我的 Backbone.Router

PHP 提交和处理表单的最佳且安全的方式

javascript - 添加 HTML 中可写的行