我正在玩这个Navbar,不管怎么说,当你将鼠标悬停在它上面时,'关于和投资组合'菜单会一直向左延伸。 我想阻止它扩展。 您可以从JSFiddle检查代码。提前谢谢。
https://jsfiddle.net/JustCraze/gdc56899/1/#
HTML
<ul id="main-nav">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About ↓</a>
<ul class="hidden">
<li>
<a href="#">Who We Are</a>
</li>
<li>
<a href="#">What We Do</a>
</li>
</ul>
</li>
<li>
<a href="#">Portfolio ↓</a>
<ul class="hidden">
<li>
<a href="#">Photography</a>
</li>
<li>
<a href="#">Web & User Interface Design</a>
</li>
<li>
<a href="#">Illustration</a>
</li>
</ul>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
CSS
#wrap-nav {
display: block;
margin:2rem;
z-index: 900;
padding: 5rem;
}
#main-nav {
display: block;
z-index: 950;
padding: 1rem;
float: right;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:.1rem;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
background: #19c589;
z-index: 980;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px;
z-index: 980;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #19c589;
color: #fff;
z-index: 980;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Responsive Styles*/
@media screen and (max-width : 250px){
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
答案 0 :(得分:1)
诀窍是让li ul
位置绝对,所以他们不会占用空间。
#wrap-nav {
display: block;
margin:2rem;
z-index: 900;
padding: 5rem;
}
#main-nav {
display: block;
z-index: 950;
padding: 1rem;
float: right;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:.1rem;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
background: #19c589;
z-index: 980;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px;
z-index: 980;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #19c589;
color: #fff;
z-index: 980;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
/*this is the trick*/
position:absolute;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Responsive Styles*/
@media screen and (max-width : 250px){
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
&#13;
<ul id="main-nav">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About ↓</a>
<ul class="hidden">
<li>
<a href="#">Who We Are</a>
</li>
<li>
<a href="#">What We Do</a>
</li>
</ul>
</li>
<li>
<a href="#">Portfolio ↓</a>
<ul class="hidden">
<li>
<a href="#">Photography</a>
</li>
<li>
<a href="#">Web & User Interface Design</a>
</li>
<li>
<a href="#">Illustration</a>
</li>
</ul>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
&#13;
答案 1 :(得分:0)
您遇到的问题是您列出的某个项目的结果:
<li><a href="#">Web & User Interface Design</a></li>
尝试将其剪切为仅用户界面设计并且它不会扩展太多,如果您阻止导航栏延伸然后文本将被切断,那是您的意图吗?你想要的结果有点不清楚。并且,感谢您对问题进行改进