我想将菜单项置于中心位置。目前,菜单项位于左侧
HTML:
<ul id="menu">
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
的CSS:
ul#menu{
list-style-type: none;
margin: 0;
padding: 0;
overflow:hidden;
background-color: #000000;
}
ul#menu li {
float: left;
}
ul#menu li a {
display: block;
color: white;
text-align: center;
font-family: verdana;
font-size: 24px;
padding: 30px;
text-decoration: none;
}
ul#menu li a:hover {
background-color: #222222;
}
ul#menu li a:active {
background-color: #222222;
}
我现在有这个: menu left side
我想要这个: menu middle
答案 0 :(得分:2)
您的项目保持不变是因为每个li(菜单项)都有float: left;
。
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
您需要flex-wrap: wrap;
,因为默认情况下,flexbox会将其设置为nowrap
,当您的屏幕尺寸很小时,它不会移动响应。
ul#menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000000;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
ul#menu li {
float: left;
}
ul#menu li a {
display: block;
color: white;
text-align: center;
font-family: verdana;
font-size: 24px;
padding: 30px;
text-decoration: none;
}
ul#menu li a:hover {
background-color: #222222;
}
ul#menu li a:active {
background-color: #222222;
}
&#13;
<ul id="menu">
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
&#13;
text-align: center;
,然后在子容器上使用display: inline-block;
,删除float: left
:ul#menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000000;
text-align: center;
}
ul#menu li {
display: inline-block;
}
ul#menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000000;
text-align: center;
}
ul#menu li {
display: inline-block;
}
ul#menu li a {
display: block;
color: white;
text-align: center;
font-family: verdana;
font-size: 24px;
padding: 30px;
text-decoration: none;
}
ul#menu li a:hover {
background-color: #222222;
}
ul#menu li a:active {
background-color: #222222;
}
&#13;
<ul id="menu">
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
&#13;