问题描述
我的代码有一个背景,其字体颜色分配给一个类,然后有单独的类用于更改 :hover 或 :active 上的颜色,但除非我删除 :hover 特定类,否则不会触发 :active 状态.CODEPEN
I have code to have a background with a font color assigned with one class then have separate classes for changing the color on :hover or :active but the :active state does not trigger unless I remove the :hover specific class. CODEPEN
HTML:
<div class="backgroundRed backgroundGreenHover backgroundBlueActive" style="width: 100px; height: 100px;"></div>
CSS:
.backgroundRed, .backgroundRedHover:hover, .backgroundRedActive:active{background:red;}
.backgroundGreen, .backgroundGreenHover:hover, .backgroundGreenActive:active{background:green;}
.backgroundBlue, .backgroundBlueHover:hover, .backgroundBlueActive:active{background:blue;}
推荐答案
:active
伪类应该在 :hover
之后,否则 :hover
覆盖(链接相关的伪类的顺序是::link | :visited | :hover | :active
.).您的代码示例按预期工作,但如果您更改类 :active
伪类永远不会应用.
:active
pseudo-class should go after :hover
, otherwise the :hover
overwrites (The order for the link-relates pseudo-clasess is: :link | :visited | :hover | :active
.). You code example works as expected but if you change the classes the :active
pseudo-class never applies.
.backgroundRed,
.backgroundRedHover:hover,
.backgroundRedActive:active {
background: red;
}
.backgroundGreen,
.backgroundGreenHover:hover,
.backgroundGreenActive:active {
background: green;
}
.backgroundBlue,
.backgroundBlueHover:hover,
.backgroundBlueActive:active {
background: blue;
}
<div class="backgroundRed backgroundBlueHover backgroundGreenActive" style="width: 100px; height: 100px;"></div>
您需要更多的 CSS 行,但在 :hover
之后使用 :active
重新排序类可以正常工作.
You need more CSS lines but reordering the classes with :active
after the :hover
works fine.
.backgroundRed,
.backgroundRedHover:hover {
background: red;
}
.backgroundGreen,
.backgroundGreenHover:hover {
background: green;
}
.backgroundBlue,
.backgroundBlueHover:hover {
background: blue;
}
.backgroundRedActive:active {
background: red;
}
.backgroundGreenActive:active {
background: green;
}
.backgroundBlueActive:active {
background: blue;
}
<div class="backgroundRed backgroundBlueHover backgroundGreenActive" style="width: 100px; height: 100px;"></div>
这篇关于专门用于 :hover 的元素覆盖 :active 的等效类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!