所以我遇到的问题是,如果我选择导航栏上的最后一个选项,因为下拉列表有很多选项,它会超出导航栏左侧的长度并包裹在下面,创建一个选项网格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>
&#13;
答案 0 :(得分:1)
问题是子菜单在主菜单中父项的左边缘对齐。让我们将子菜单对齐主菜单的右边缘。
ul-right
添加到相应的<ul>
。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>