html - 为什么这个无序列表不是居中的?怎么了?

标签 html css list html-lists

只是无法将此列表居中...(元素,li)。

怎么了?

我现在已经看了将近半个小时的代码,仍然找不到问题所在。一直在改变一些东西,但仍然是错误的。

HTML:

<!DOCTYPE html>
<html lang="pl">
<head>
    <link rel="stylesheet" href="style.css">
    <meta charset="utf-8">
    <title>Blog</title>
    <meta http-equiv="X-Ua-Compatible" content="IE=edge,chrome=1">
    <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
    <![endif]-->
</head>
<body>
<header>
    <div id="logo"></div>
    <h1 class="hidden">Devloger</h1>
    <nav>
        <div class="nav">
            <ul>
                <li>
                    Strona Główna
                </li>
                <li>
                    Spis Treści
                </li>
                <li>
                    Kategorie
                </li>
                <li>
                    Współpraca
                </li>
                <li>
                    Kontakt
                </li>
            </ul>
        </div>
    </nav>
</header>


</body>
</html>

还有 CSS:

    body {
    margin: 0;
    background-image: url("2.png");
    background-repeat: repeat-x;
    font-family: 'Open Sans', sans-serif;
    font-size: 17px;
}
#logo {
    background-image: url("logo.png");
    width: 527px;
    height: 58px;
    background-repeat: no-repeat;
    margin-left: auto;
    margin-right: auto;
    margin-top: 7px;
}
.hidden {
    display: none;
}
.nav {
    background-color: #55585d;
    height: 36px;
    margin-top: 10px;
    border-bottom: 2px solid #44474c;
    border-top: 2px solid #44474c;
    text-align: center;
}
.nav > ul {
    list-style: none;
    margin: 0px;
    padding: 0px;
}
.nav > ul li {
    text-align: center;
    float: left;
    padding: 5px;
}

最佳答案

发生这种情况是因为您将 li 标签 float 到左侧,将其删除并在 ul 上添加使用 flexbox,在这种情况下设置所需的宽度我将 100% 和 justify-content: center;所以它可以居中。

    body {
    margin: 0;
    background-image: url("2.png");
    background-repeat: repeat-x;
    font-family: 'Open Sans', sans-serif;
    font-size: 17px;
}
#logo {
    background-image: url("logo.png");
    width: 527px;
    height: 58px;
    background-repeat: no-repeat;
    margin-left: auto;
    margin-right: auto;
    margin-top: 7px;
}
.hidden {
    display: none;
}
.nav {
    background-color: #55585d;
    height: 36px;
    margin-top: 10px;
    border-bottom: 2px solid #44474c;
    border-top: 2px solid #44474c;
    text-align: center;
}
.nav > ul {
    list-style: none;
    margin: 0px;
    padding: 0px;
    display:flex;
    justify-content:center;
    width:100%;
}
.nav > ul li {
    text-align: center;
    
    padding: 5px;
}
<header>
    <div id="logo"></div>
    <h1 class="hidden">Devloger</h1>
    <nav>
        <div class="nav">
            <ul>
                <li>
                    Strona Główna
                </li>
                <li>
                    Spis Treści
                </li>
                <li>
                    Kategorie
                </li>
                <li>
                    Współpraca
                </li>
                <li>
                    Kontakt
                </li>
            </ul>
        </div>
    </nav>
</header>

关于html - 为什么这个无序列表不是居中的?怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009511/

相关文章:

javascript - 想要播放音频并且应该在 10 秒后在 Html 和 javascript 中停止?

html - 是否可以将文本环绕图像,如下所示

list - 如何在OCaml中检查下一个 'head'为空?

c# - 如何基于多个xml属性存储和查找数据?

html - 获取水平列表以对齐 html 表格 css 的顶部

html - 背景大小无法正常工作

javascript - 在 Microsoft Windows 上,当使用 HTML5 拖放时,在拖放过程中我确实看到了拖动图标(幻影)

html - 使用溢出隐藏 div 包裹文本的 CSS 不起作用?

javascript - 使用javascript单击缩略图时临时加载图像和图像选择

python - 关于防止代码重复的代码结构建议(Python、列表)