CSS垂直中心内联元素?

时间:2012-06-02 01:09:11

标签: css html5

您好我无法在无序列表中垂直居中获取锚标记。我的CSS是:

*{ margin: 0; padding: 0;}

html {
    height: 100%;
    font-size: 16px;
}

body {
    font-family: 'Montserrat', sans-serif;
    font-size: 62.5%;
    background-color:rgba(0,0,0, 0.7);
    height: 100%;
}

.container {
    margin-left: 9.814814814814815%; /* 106 / 1080 */
    margin-right: 10.092592592592593%; /* 109 / 1080 */
    width: 80%; /* 864 / 1080 */
    height: 100%;
}

nav ul{
    list-style-type: none;
}

nav ul li {
    display: inline-block;
}

nav ul li a {
    color: white;
    text-transform: uppercase;
    text-decoration: none;
    font-size: 2em;
}

nav {
    background-color: black;
    margin-top: 5.0595238095238096%; /* 85 / 1680 */
    height:  5.476190476190476%; /* 92 / 1680 */
}

然后这是我的HTML:

<div class="container">
    <nav>
        <ul>
            <li><a href="/">home</a></li>
            <li><a href="/get_involved">get involved</a></li>
        </ul>
    </nav>
</div>

1 个答案:

答案 0 :(得分:0)

要实现“垂直居中”,请在<a>元素中添加行高或填充:

nav ul li a {
    color: white;
    text-transform: uppercase;
    text-decoration: none;
    font-size: 2em;
    padding: 10px 0;
}

您还可以调整父<li>元素的高度以微调“居中”。

相关问题