CSS :hover ONLY when there are two classes

标签 css hover

当两个不同的类附加到一个 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/

相关文章:

CSS & HTML5 : why does my body have a spacing at the top?

javascript - 当用户点击内容 JS 时 Div 改变高度

html - 左侧菜单的悬停效果不起作用

jquery - IE6 和 Apache Logs 的 Superfish 悬停问题

css - Webpack 的生活风格指南?

css - jQuery Mobile,可折叠键值网格 : sizing header 50/50 with icon

javascript - Bootstrap 日期时间选择器不工作

html - 定位 href 内的特定 div 以及所有用 main div 包裹的内容,以便在 css 中悬停

css - 如何在样式组件中禁用 material-ui 按钮的悬停效果

javascript - 如何处理触摸设备的多级下拉菜单