简体   繁体   English

CSS下拉菜单需要整个页面宽度吗?

[英]CSS dropdown menu takes entire page width?

I have a simple nav menu at the top of a page I'm working on. 我正在处理的页面顶部有一个简单的导航菜单。 I want one of the links on the nav menu to have a dropdown menu pop up underneath it when you hover over it. 我希望将鼠标悬停在导航菜单上的链接之一下,然后在其下方弹出一个下拉菜单。

I have the drop down menu appearing just fine. 我的下拉菜单似乎很好。 The only problem is that the drop down menu items are the width of the entire page instead of the width of a nav menu item. 唯一的问题是,下拉菜单项是整个页面的宽度,而不是导航菜单项的宽度。 I'm not sure what's causing it... 我不确定是什么原因造成的...

Here is the html and css for the nav menu and drop down menu: 这是导航菜单和下拉菜单的html和CSS:

 .dropdown { float: left; background-color: #FFF0F5; } .dropDiv { position: relative; display: inline-block; width: 100%; } .dropdownContent { display: none; background-color: #FFF0F5; z-index: 1; width: 300px; overflow-x: hidden; } .dropdownContent a { display: block; text-align: center; padding: 14px 16px; text-decoration: none; width: 300px; } .dropdownContent a:hover { background-color: #fff8dc; } .dropDiv:hover .dropdownContent { display: block; z-index: 1; height: 200px; width: 300px; overflow-x: hidden; } .dropDiv:hover .dropdown { background-color: #fff8dc; } 
 <nav> <ul> <li><a href="eiffel.shtml">Eiffel Tower</a></li> <li><a href="fashion.shtml">Fashion</a></li> <li><a href="food.shtml">Food</a></li> <li><a href="museums.shtml">Museums</a></li> <div class="dropDiv"> <li class="dropdown"><a href="history.shtml">History</a></li> <div class="dropdownContent"> <a href=leaders.shtml>Leaders of Paris</a> <a href=future.shtml>Future of Paris</a> </div> </div> <li><a href="language.shtml">Language</a></li> <li><a href="works.shtml">Works Cited</a></li> </ul> </nav> 

As you can see It's a bit of a mess since I've been adding lots of things trying to think of something to fix this width issue. 如您所见,这有点混乱,因为我一直在尝试尝试解决此宽度问题的东西,因此添加了很多东西。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

Your code worked fine for me. 您的代码对我来说很好。 Looks like some other CSS in your code is making it full width. 看起来您代码中的其他CSS正在使其全宽。 Have you tried fixing it with "!important" or "max-width"? 您是否尝试过使用“!important”或“ max-width”修复它?

You should create dropDiv in the li tag and make it absolute position to get the parent widht 您应该在li标签中创建dropDiv并将其设置为绝对位置以获取父窗口

 nav ul { padding:0; margin:0; } nav ul li { list-style: none; float: left; position: relative; margin: 0 5px; } nav ul li a{ color: #000; text-decoration: none; } .dropdown { float: left; background-color: #FFF0F5; } nav ul li:hover .dropDiv { display: block; z-index: 1; height: 200px; width: 300px; overflow-x: hidden; } .dropDiv:hover .dropdown { background-color: #fff8dc; } .dropDiv { position: absolute; width: 100%; display: none; background-color: #FFF0F5; z-index: 1; width: 300px; overflow-x: hidden; } .dropDiv a { display: block; text-align: center; padding: 14px 16px; text-decoration: none; } .dropDiv a:hover { background-color: #fff8dc; } 
 <nav> <ul> <li><a href="eiffel.shtml">Eiffel Tower</a></li> <li><a href="fashion.shtml">Fashion</a></li> <li><a href="food.shtml">Food</a></li> <li><a href="museums.shtml">Museums</a></li> <li class="dropdown"><a href="history.shtml">History</a> <div class="dropDiv"> <a href=leaders.shtml>Leaders of Paris</a> <a href=future.shtml>Future of Paris</a> </div> </li> <li><a href="language.shtml">Language</a></li> <li><a href="works.shtml">Works Cited</a></li> </ul> </nav> 

Hello just define width for nav bar as you want I've defined 700px. 您好,只要为导航栏定义宽度,就可以定义700px。 and add some css . 并添加一些CSS。 check this code 检查此代码

 .dropdown { float: left; background-color: #FFF0F5; } .dropDiv { position: relative; display: inline-block; list-style: none; padding: 9px; float: left; max-width: 100%; } .dropdownContent { display: none; background-color: #FFF0F5; z-index: 1; width: 300px; overflow-x: hidden; } .dropdownContent a { display: block; text-align: center; padding: 14px 16px; text-decoration: none; } .dropdownContent a:hover {background-color: #fff8dc;} .dropDiv:hover .dropdownContent { display: block; z-index: 1; overflow-x: hidden; top: 30px; left: 10px; position: absolute; } .dropDiv:hover .dropdown { background-color: #fff8dc; } nav {width:700px;float: left;} nav > ul > li { float: left; padding: 9px;list-style: none} ul{float:left;} 
 <nav> <ul> <li><a href="eiffel.shtml">Eiffel Tower</a></li> <li><a href="fashion.shtml">Fashion</a></li> <li><a href="food.shtml">Food</a></li> <li><a href="museums.shtml">Museums</a></li> <div class="dropDiv"> <li class="dropdown"><a href="history.shtml">History</a></li> <div class="dropdownContent"> <a href=leaders.shtml>Leaders of Paris</a> <a href=future.shtml>Future of Paris</a> </div> </div> </li> <li><a href="language.shtml">Language</a></li> <li><a href="works.shtml">Works Cited</a></li> </ul> </nav> 

I think this is what you want. 我想这就是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM