gpt4 book ai didi

html - 水平导航栏子导航向下环绕而不是使用它后面(左)的空间

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:49 25 4
gpt4 key购买 nike

所以我遇到的问题是,如果我选择导航栏上的最后一个选项,因为下拉菜单有太多选项,它会超出导航栏左侧的长度并在下方环绕,从而创建一个 2x2 的选项网格。

如果它使用左边的空间而不是向下环绕,我会更喜欢。我怎样才能使这成为可能。

nav {
margin: 0 auto;
text-align: center;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
color: white;
background: #787878;
background: linear-gradient(top, #787878 0%, #272727 100%);
background: -moz-linear-gradient(top, #787878 0%, #272727 100%);
background: -webkit-linear-gradient(top, #787878 0%, #272727 100%);
box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
padding: 0 20px;
border-radius: 25px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: "";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #A8A8A8;
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block;
padding: 15px 50px;
color: white;
text-decoration: none;
}
nav ul ul {
background: #505050;
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
}
nav ul ul li {
float: left;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #fff;
}
nav ul ul li a:hover {
background: #A8A8A8;
}
nav ul ul ul {
position: absolute;
left: 100%;
top: 0;
}
<nav>
<ul>
<li><a href="/p/">Players</a>
<ul>
<li><a href="#">Wonderkids</a>
</li>
<li><a href="#">Cheap Players</a>
</li>
<li><a href="#">Player Comparisons</a>
</li>
</ul>
</li>
<li><a href="/c/">Clubs</a>
<ul>
<li><a href="#">Club Info</a>
</li>
<li><a href="#">Transfer Budgets</a>
</li>
<li><a href="#">Sugar Daddys</a>
</li>
</ul>
</li>
<li><a href="/s/">Downloads</a>
<ul>
<li><a href="#">Tactics</a>
</li>
<li><a href="#">Shortlists</a>
<ul>
<li><a href="#">Various Shortlists</a>
</li>
<li><a href="#">Positional Shortlists</a>
</li>
<li><a href="#">Staff Shortlists</a>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="/g/">Write-Ups</a>
</li>
<li><a href="/p/">Social</a>
<ul>
<li><a href="#">Facebook</a>
</li>
<li><a href="#">Twitter</a>
</li>
<li><a href="#">Youtube</a>
</li>
<li><a href="#">Affiliates</a>
</li>
</ul>
</li>
<li><a href="/aboutus/">About Us</a>
</li>
</ul>
</nav>

enter image description here

最佳答案

问题是子菜单与主菜单中父项的左边缘对齐。让我们将子菜单对齐到主菜单的右边缘。

  1. 添加类(class) ul-right到对应的<ul> .
  2. 添加 CSS 代码 nav ul ul.ul-right { right: 0; } .

查看结果:

nav {
margin: 0 auto;
text-align: center;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
color: white;
background: #787878;
background: linear-gradient(top, #787878 0%, #272727 100%);
background: -moz-linear-gradient(top, #787878 0%, #272727 100%);
background: -webkit-linear-gradient(top, #787878 0%, #272727 100%);
box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
padding: 0 20px;
border-radius: 25px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: "";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #A8A8A8;
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block;
padding: 15px 50px;
color: white;
text-decoration: none;
}
nav ul ul {
background: #505050;
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
}
nav ul ul.ul-right {
right: 0;
}
nav ul ul li {
float: left;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #fff;
}
nav ul ul li a:hover {
background: #A8A8A8;
}
nav ul ul ul {
position: absolute;
left: 100%;
top: 0;
}
<nav>
<ul>
<li><a href="/p/">Players</a>
<ul>
<li><a href="#">Wonderkids</a>
</li>
<li><a href="#">Cheap Players</a>
</li>
<li><a href="#">Player Comparisons</a>
</li>
</ul>
</li>
<li><a href="/c/">Clubs</a>
<ul>
<li><a href="#">Club Info</a>
</li>
<li><a href="#">Transfer Budgets</a>
</li>
<li><a href="#">Sugar Daddys</a>
</li>
</ul>
</li>
<li><a href="/s/">Downloads</a>
<ul>
<li><a href="#">Tactics</a>
</li>
<li><a href="#">Shortlists</a>
<ul>
<li><a href="#">Various Shortlists</a>
</li>
<li><a href="#">Positional Shortlists</a>
</li>
<li><a href="#">Staff Shortlists</a>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="/g/">Write-Ups</a>
</li>
<li><a href="/p/">Social</a>
<ul class="ul-right">
<li><a href="#">Facebook</a>
</li>
<li><a href="#">Twitter</a>
</li>
<li><a href="#">Youtube</a>
</li>
<li><a href="#">Affiliates</a>
</li>
</ul>
</li>
<li><a href="/aboutus/">About Us</a>
</li>
</ul>
</nav>

关于html - 水平导航栏子导航向下环绕而不是使用它后面(左)的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com