我有一个带子菜单的菜单栏。我希望在菜单的同一行显示右侧的“帐户”或“个人资料”链接,我想用纯CSS3 / Flex来完成。
但是我想我的css代码有问题,因为最后一个链接(“帐户”)在主菜单下进行了diplaying,因此导航元素的justify-content: space-between
无效。我该怎么办?
这是我的代码:
nav {
font-size: 20px ;
background-color: white;
display: flex;
flex-direction: row;
justify-content: space-between;
}
nav ul {
display: flex;
flex-direction: row;
margin: 0;
padding: 0 ;
position: absolute;
background-color: white;
list-style-type: none;
}
nav ul li {
padding: 0;
margin: 0;
}
nav ul li ul {
display: none;
margin: 0;
padding: 0;
box-shadow: 3px 3px 3px 0px #DDDDDD;
background-color: white;
}
nav ul li:hover ul{
display: block;
}
nav a {
color: #333;
display: block;
margin: 0;
padding: 10px 20px;
text-decoration: none;
}
nav a:hover {
background-color: #DDD;
}
<nav>
<ul>
<li><a href="#">Menu1</a>
<ul>
<li><a href="#">Submenu</a></li>
<li><a href="#">Another longer submenu here</a></li>
<li><a href="#">Yet another submenu</a></li>
<li><a href="#">Etc. etc.</a></li>
</ul>
</li>
<li><a href="#">Menu2</a></li>
<li><a href="#">Menu3</a>
<ul>
<li><a href="#">My submenu 1</a></li>
<li><a href="#">My submenu 2</a></li>
<li><a href="#">My submenu 3</a></li>
<li><a href="#">My submenu 4</a></li>
<li><a href="#">My submenu 5</a></li>
<li><a href="#">My submenu 6</a></li>
<li><a href="#">My submenu 7</a></li>
<li><a href="#">My submenu 8</a></li>
</ul>
</li>
<li><a href="#">Menu4</a>
</li>
</ul>
<a href="#">Account</a>
</nav>
答案 0 :(得分:0)
好的,我刚发现错误。
nav ul { position: absolute; }
不应该在这里定位,但在这里:
nav ul li ul { position: absolute; }
所以解决了片段(希望它可以帮助其他人)......
nav {
font-size: 20px ;
background-color: white;
display: flex;
flex-direction: row;
justify-content: space-between;
}
nav ul {
display: flex;
flex-direction: row;
margin: 0;
padding: 0 ;
background-color: white;
list-style-type: none;
}
nav ul li {
padding: 0;
margin: 0;
}
nav ul li ul {
display: none;
margin: 0;
padding: 0;
box-shadow: 3px 3px 3px 0px #DDDDDD;
background-color: white;
position: absolute;
}
nav ul li:hover ul{
display: block;
}
nav a {
color: #333;
display: block;
margin: 0;
padding: 10px 20px;
text-decoration: none;
}
nav a:hover {
background-color: #DDD;
}
<nav>
<ul>
<li><a href="#">Menu1</a>
<ul>
<li><a href="#">Submenu</a></li>
<li><a href="#">Another longer submenu here</a></li>
<li><a href="#">Yet another submenu</a></li>
<li><a href="#">Etc. etc.</a></li>
</ul>
</li>
<li><a href="#">Menu2</a></li>
<li><a href="#">Menu3</a>
<ul>
<li><a href="#">My submenu 1</a></li>
<li><a href="#">My submenu 2</a></li>
<li><a href="#">My submenu 3</a></li>
<li><a href="#">My submenu 4</a></li>
<li><a href="#">My submenu 5</a></li>
<li><a href="#">My submenu 6</a></li>
<li><a href="#">My submenu 7</a></li>
<li><a href="#">My submenu 8</a></li>
</ul>
</li>
<li><a href="#">Menu4</a>
</li>
</ul>
<a href="#">Account</a>
</nav>