作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当两个不同的类附加到一个 div 时,我试图只应用 CSS :hover 选择器。我已经试过了,但这摆脱了悬停效果。
.accordionButton .cyan:hover{
color: cyan;
}
我也不能这样做:
.accordionButton:hover, .cyan:hover{
color: cyan;
}
因为我还有其他两种颜色,所以我也想用它来做这件事。
基本上,有什么方法可以使 CSS 仅在将鼠标悬停在既是 .accordionButton 又是 .cyan 的 div 上时应用?
最佳答案
您可以通过不带空格的链接来组合类选择器。
在下面的示例中,:hover
声明仅应用于具有两个 类的元素:
.accordionButton.cyan:hover {
color: cyan;
}
<div class="accordionButton">Only Button</div>
<div class="cyan">Only Cyan</div>
<div class="accordionButton cyan">Both Classes</div>
有关引用,请参阅 MDN:
Target an element if it has more than one class applied .
You can apply multiple classes to an element and ... only select the element when all of the classes in the selector are present.
We can tell the browser that we only want to match the element if it has all of these classes by chaining them together with no white space between them.
关于CSS :hover ONLY when there are two classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14759402/
我是一名优秀的程序员,十分优秀!