相同级别的 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10442336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS for same level
提问by user1365010
If I have 3 divs at the same level ( not one in another ) . How can I change the color of the other div when hover one without using IDs and classes. I would like somthing like :
如果我在同一级别有 3 个 div(而不是另一个)。在不使用 ID 和类的情况下悬停一个 div 时,如何更改另一个 div 的颜色。我想要类似的东西:
<div id="1" ></div>
<div></div>
<div></div>
And CSS :
和 CSS :
#1 :hover < body > div
{
//Here I change the things
}
回答by SupremeDud
Use the general sibling combinator
使用一般的兄弟组合器
#yourId:hover ~ div
{
color:red;
}
Also note that Id's must begin with a letter. W3 ID Attribute
另请注意,Id 必须以字母开头。W3 ID 属性
回答by Petah
Put a wrapper around them, then put the hover on the wrapper.
在它们周围放一个包装纸,然后将鼠标悬停在包装纸上。
<div class="wrapper">
<div class="element">foo</div>
<div class="element">bar</div>
<div class="element">baz</div>
</div>
.wrapper:hover .element {
color: red;
}
Example: http://jsfiddle.net/EB92r/