简体   繁体   English

CSS继承:ul样式不适用于嵌套div

[英]CSS inheritance : ul style not applied to nested div

I ran into a problem trying to build a dropdown menu in a navbar, using nested lists. 我在尝试使用嵌套列表在导航栏中构建下拉菜单时遇到问题。

Here's a snippet of the code : 这是一段代码:

 .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; } .dropdown:hover + .dropdown-content { display: block; } .dropdown-content a { padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #f1f1f1 } 
  <nav class="navbar navbar-default" role="navigation" data-600='opacity:0' data-1000='opacity:1'> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul id=navbar class="nav navbar-nav"> <li class="dropdown"><a href=index.html>Jeux de vilains ▼</a></li> <div class="dropdown-content"> <ul class="nav navbar-nav navbar-right"> <li class=secondary><a href=/carto2016-jeuxvideos/article-1.html>Tomb Raider</a></li> <li class=secondary><a href=/carto2016-jeuxvideos/article-2.html>GTA</a></li> <li class=secondary><a href=/carto2016-jeuxvideos/article-3.html>SimCity</a></li> <li class=secondary><a href=/carto2016-jeuxvideos/article-4.html>Call of Duty</a></li> <li class=secondary><a href=/carto2016-jeuxvideos/article-5.html>Resident Evil 5</a></li> <li class=secondary><a href=/carto2016-jeuxvideos/article-6.html>Civilization</a></li> </ul> </div> </ul> </div> 

This part was solved : 此部分已解决:

Basically, my problem is that I want my "dropdown" div to have no style of its own, but to inherit the style of the navbar. 基本上,我的问题是我希望我的“下拉” div没有自己的样式,而要继承导航栏的样式。

The weird thing is that the nested navbar is properly styled, but the dropdown div follows the style of general a and hover:a elements. 奇怪的是嵌套导航栏的样式正确,但是下拉div遵循一般a和hover:a元素的样式。

If you look at the beginning of my HTML, you'll see the culprit: <div class="dropdown"> , which appears completely differently from the rest. 如果您查看我的HTML的开头,您会发现罪魁祸首: <div class="dropdown"> ,其外观与其余部分完全不同。 I don't understand why the class in which it is nested (the nav class) is not considered the parent, especially since the following list is properly styled. 我不明白为什么嵌套它的类(nav类)不被认为是父类,特别是因为以下列表的样式正确。

I attached a screenshot of what it looks like: "Jeux de vilains" looks weird, when it should look like "Règles du jeu". 我附上了它的屏幕截图 :“ Jeux de vilains”看起来很怪异,应该看起来像“Règlesdu jeu”。

This has been solved too 这也已经解决了

Well thank you all for you swift and kind help, I took out the unwanted div and put the dropdown class in the corresponding li. 好的,谢谢大家的帮助,我删除了不需要的div并将下拉菜单放到相应的li中。

However, while the list does appear on hover, it disappears as soon as the mouse is not hovered. 但是,尽管列表确实出现在悬停上,但只要不悬停鼠标,它就会消失。 I'd like to make it so the secondary menu appears as long as either the parent li or the secondary menu itself are hovered. 我想这样做,以便在父li或辅助菜单本身都悬停时出现辅助菜单。 How could I do that ? 我该怎么办?

Here's the website : http://medialab.github.io/carto2016-jeuxvideos/index.html 这是网站: http : //medialab.github.io/carto2016-jeuxvideos/index.html

Thanks in advance for everyone's help. 在此先感谢大家的帮助。

Question solved. 问题解决了。

Your first instinct would be to use JavaScript but for this but there's a slick way to accomplish this with what are essentially operators in CSS... 您的第一个直觉是使用JavaScript,但是为此,有一种巧妙的方法可以使用CSS中的本质运算符来完成此操作...

Use the :hover pseudo-class on a parent element and look for the instance of the dropdown. 在父元素上使用:hover伪类,然后查找下拉列表的实例。

.nav:hover .dropdown-content {
    display: block;
}

Because you're selecting the parent element, it applies to both the dropdown and what you hover over to show it over so it's displayed whenever it's within those realms. 因为您正在选择父元素,所以它既适用于下拉菜单,也适用于您悬停在其上的内容以显示出来,因此只要它在这些领域内,便会显示出来。 Make more and they'll work just the same respective to their individual dropdowns. 赚更多,他们的工作与各自的下拉菜单相同。

https://jsfiddle.net/ksxk33w4/1/ https://jsfiddle.net/ksxk33w4/1/

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

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