html - 导航栏链接未正确居中

标签 html css navbar

我制作了一个带有标题和导航栏的简单网页,但我遇到了这个实际上非常烦人的小问题。链接不是 100% 在内联列表的中间,这里是截图: https://i.gyazo.com/105d8156e667277d0b31f18ba6a3b7db.png

为了防止混淆,整个页面居中,但截图只是导航栏部分。

HTML:

<!DOCTYPE html>
<html>

<head>
    <title>Test</title>
    <link href="css/style.css" type="text/css" rel="stylesheet" />
</head>

<body>
    <h1>Test website</h1>
    <div id="nav">
        <ul>
            <li><a class="active" href="#">Home</a></li>
            <li><a href="#">Second</a></li>
            <li><a href="#">Third</a></li>
            <li><a href="#">Fourth</a></li>
            <li><a href="#">Fifth</a></li>
        </ul>
    </div>
</body>

</html>

CSS 文件:

/* navigation bar */
#nav {
    width: 490px;
    margin: auto;
    border-top: 1px solid black;
    border-bottom: 1px solid black;
}

/* unordered list in navbar */
#nav ul {
    text-align: center;
}

/* list items in navbar */
#nav li {
    display: inline-block;
}

/* links in items of navbar */
#nav li a {
    text-decoration: none;
    margin: 20px;
    font-family: Arial, sans-serif;
    font-weight: bold;
}

/* header 1 */
h1 {
    text-align: center;
    font-family: Arial, sans-serif;
    font-weight: bold;
}

最佳答案

问题是应用于 ul 的默认填充,只需将填充归零即可:

#nav ul {
    text-align: center;
    padding:0;
}

JSFiddle

关于html - 导航栏链接未正确居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32702670/

相关文章:

javascript - 创建图像元素然后通过javascript错误设置样式属性

css - 并排div之间的间距不可编辑?

jquery - Bootstrap 3 导航栏折叠问题

html - 为什么我的导航栏在 bootstrap 的移动 View 中有边距?

javascript - 通过预定义的 HTML 页面列表进行下一个/上一个导航

css - 浏览器使用什么 CSS 来设置无效 &lt;input type ="email"> 的样式?

jquery - 在Joomla的2.5文章中添加jQuery

css - 背景图像高度切割

html - 设置跨度的样式以使用列表项的 100% 高度

css - 我的导航链接有什么问题 a :hover box doesn't include the padding? 只有内容框会改变颜色。