我正在尝试制作自己的导航栏,以更好地了解如何创建良好的导航栏。我遇到的问题是我的导航栏居中。现在我知道使用使用 margin-left: auto 和 margin-right: auto 通常可以做到这一点,我过去曾使用过,但在这种情况下,无论我将这些样式元素放在哪里,我的导航栏似乎都不是居中。我将我的身体设置为 100%,并在下面提供了我的代码。*注意:我已经为我的导航栏尝试了一个容器并将其居中,但它似乎仍然不起作用。
#navbar ul {
list-style-type:none;
color: #000;
padding: 0px;
text-align: center;
}
li:hover {
background-color: #cc2c32;
padding: 16px .1px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.subcategory {
display: none;
width: 1031px;
height: 200px;
background-color: #cc2c32;
color: #03F;
position: absolute;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
margin-top: 15px;
}
li:hover .subcategory {
display: block;
}
#navbar {
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
background-color: #dee8ff;
float: left;
font-family: Arial, Helvetica, sans-serif;
margin-left: auto;
margin-right: auto;
}
#navbar ul li {
display: inline;
}
#navbar ul li a {
padding: 0 1.5em;
text-decoration: none;
}
ol {
list-style-type: none;
float: left;
display: block;
}
.contentwrapper {
margin-top: 10px;
margin-bottom: 10px;
height: 180px;
width: 200px;
background-color: inherit;
position: relative;
color: #2d4594;
}
a:link {
color: #2d4594;
text-decoration: none;
}
a:visited {
color: #2d4594;
text-decoration: none;
}
a:hover {
color: #2d4594;
text-decoration: none;
}
我的 HTML 是
<body>
<div id="navbar">
<ul>
<li><a href="#">LinkHere</a>
<div class="subcategory">
<div class="contentwrapper">
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
<ol>Hey</ol>
</div>
</div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
<li><a href="#">LinkHere</a>
<div class="subcategory"></div>
</li>
</ul>
</div>
</body>