为什么悬停伪类会覆盖活动伪类
标题基本上说明了一切。
假设我有一个元素想要在 :hover
上更改颜色,但是在单击时,我希望它切换回原始颜色。所以,我尝试过:
a:link, a:visited, a:active {
background: red;
}
a:hover {
background: green;
}
事实证明,这是行不通的。经过一番绞尽脑汁后,我意识到 :hover
状态会覆盖 :active
状态。这很容易解决:(
a:link, a:visited {
background: green;
}
a:hover {
background: red;
}
a:active {
background: green;
}
我可以将第一条规则与第三条规则结合起来)。
这是小提琴: http://jsfiddle.net/V5FUy/
我的问题:这是预期的行为吗?据我了解, :active
状态应该始终覆盖 :hover
状态,因为 :active
状态几乎 始终伴随 :hover
状态。
The title basically says it all.
Suppose I have an element which I want to change color on :hover
, but while clicked, I want it to switch back to its original color. So, I've tried this:
a:link, a:visited, a:active {
background: red;
}
a:hover {
background: green;
}
As it turns out, this doesn't work. After a lot of head-scratching, I realized that the :hover
state was overriding the :active
state. This was easily solved by this:
a:link, a:visited {
background: green;
}
a:hover {
background: red;
}
a:active {
background: green;
}
(I could combine the 1st rule with the 3rd one).
Here's the fiddle: http://jsfiddle.net/V5FUy/
My question: is this the expected behavior? As far as I understand this, the :active
state should always override the :hover
state, since the :active
state will almost always be accompanied with the :hover
state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,这是预期的行为,
让我们看另一个例子。只需添加两个类,
这里类第一个也与类项一起出现。
但是,如果我们以错误的顺序声明 css,而不会给出
您所看到的所需行为,则将使用最后一个匹配的选择器。
和你的例子是一样的,不管怎样更符合逻辑,
2 个伪类被认为是相等的,因此适用相同的规则
最后匹配的防守者获胜。
edit
伪类是平等的,最后定义的伪类获胜!这是一个 jsFiddle 证明了我的观点 :link 在 :hover 之后定义, :link 获胜 (test) 所以,你的声明:hover 覆盖 :link 是错误的,它与 :active 一样,都是关于顺序的。
yes this is expected behavior,
lets take a look at another example. just adding two classes,
here the class first also comes together with the class item.
but if we declare our css in the wrong order that would not give the wanted behavior
as you can see, the last matching selector will be used.
it is the same as your example, no mather what is more logic,
the 2 pseudo-classes are concidered equal, thus the same rules apply
the last matching defenition wins.
edit
pseudoclasses are equals, it is the one defined last that wins! here is a jsFiddle that proves my point :link defined after :hover, :link wins (test) so, your statement of :hover overriding :link is wrong, its just the same like with :active, its all about the order.
活动状态必须在悬停状态之后声明,在CSS中,您将活动状态之前聚集在一起,这样它就不会被触发。
如果您指定正确的操作顺序,它就可以工作,如下所示,它就可以正常工作。
所以,回答你的问题,是的,这是预期的行为。
以下是操作顺序:
The active state must be declared after the hover state, in your CSS you're clumping together the active state before the active state so it's not being triggered.
If you state the proper order of operation it works, like below, it works fine.
So, to answer your question, yes this is the expected behavior.
Here is the order of operation:
因为在第一个代码中,您在定义
:active
之后定义了:hover
,所以:hover
“覆盖”了:active
>。在第二种情况下,情况正好相反,:active
覆盖:hover
。Because in the first code you defined
:hover
after you defined:active
, so:hover
"overwrote":active
. In the second, it's the other way around,:active
overwrites:hover
.编辑:
抱歉,我误解了这个问题。
基本上,当您处于活动状态(使用鼠标指针)时,您实际上也处于悬停状态。因此,根据 CSS 规则,它将读取样式表中的最后一个。
当您将鼠标悬停在链接上并按住鼠标键时,如果我们将伪类视为普通类,就会像这样:
因此,如果您的CSS是,
它将应用红色,
但如果您的CSS是,
它将应用绿色
来自 W3C
EDIT:
Sorry, I misunderstand the question.
Basically when you are in active state (with a mouse pointer) you are actually in hover state too. So based on CSS rules it would read the last one in stylesheet.
When you hover over a link and hold down the mouse key It's like this if we take pseud classes as normal classes :
So if your css was
it would apply red
but if your css was
It would apply green
From W3C
这就是它的工作原理,我将尝试解释原因。正如我们所知,CSS 在应用样式时会继续搜索文档并应用最特定于该元素的样式。
示例:
更具体,将覆盖
这些 Puesdo 选择器都被认为具有相同的特异性,因此最后一行将覆盖前一行。 W3Schools 上的注释证实了这一点
你也可以把这个 CSS 扔到你的 jsfidle 上并观察它的覆盖,因为它们具有相同的特异性
This is how it works, and I'll try to explain why. As we know CSS will continue searching the document when applying styles and apply the style that is most specific to the element.
Example:
Is more specific and will overwrite
And these Puesdo selectors are all considered the same specificity and thus the last line will overwrite the previous one. This is confirmed by the note on W3Schools
you can also throw this CSS on your jsfidle and watch it overwrite since they are the same specificity
这是预期的行为,因为大多数人总是将
:hover
伪类放在规则组的末尾。声明的顺序与伪类有关(请参阅此处的更多信息: http://reference.sitepoint.com/css/伪类),因此最终规则优先,就像 CSS 中的其他规则一样。
对于大多数人来说,我认为期望的行为:
由于
:active
不太有用,因此被忽略......或与a:link
和a 结合使用:visited
... 然后它被a:hover
覆盖W3C 在这里说明:
http://www.w3.org/TR/CSS2/selector .html#dynamic-pseudo-classes
甚至 W3schools 也正确地理解了这一点:
http://www.w3schools.com/css/css_pseudo_classes.asp
This is the expected behavior in so far as most people always place the
:hover
pseudo-class at the end of the group of rules.Order of declaration matters with pseudo-classes (see more here: http://reference.sitepoint.com/css/pseudoclasses), so the final rules get precedence, as with other rules in CSS.
For most people, I think the desired behavior:
Since the
:active
is not so useful it is left out... or combined witha:link
anda:visited
... and then it is overridden bya:hover
W3C spells it out here:
http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
Even W3schools gets this one right:
http://www.w3schools.com/css/css_pseudo_classes.asp
我认为您至少应该考虑链接(或按钮)上的用户交互流程。
通常,
:link
始终是默认的(未触及),:hover
就会出现进入游戏。:active
的用武之地。这是我们与链接(或按钮)交互的标准顺序。
:visited
除外,只有当用户先前按下该链接时,结果才会明显。如果您在处理链接时记住助记符:' L o V e H A te ',那将会非常有帮助(除了
:visited
,它不适用于按钮)。但是,如果您确实想要进行覆盖,例如,您想要更改已在活动状态下访问过的链接的颜色,您可以执行以下操作:
但底线是,如果不是,请避免更改标准序列必要的。
I think you should at least consider the flow of User Interaction on links (or buttons).
Usually,
:link
has always been the default (untouched),:hover
comes into play.:active
comes in.That is the standard sequence of how we interact with links (or buttons). With the exception of
:visited
, where the result is only obvious when the User has previously pressed the link.It would be really helpful if you keep in mind the mnemonic: ' L o V e H A t e ' when dealing on links (except for
:visited
, which doesn't work for buttons).However, if you really want to do an override, say, you want to change the color of a link that was already visited on active state, you can do something like:
But the bottom line is, avoid changing the standard sequence if it's not necessary.
您可以使用 :not() 选择器让 :active 伪类覆盖 :hover 伪类,无论声明顺序如何。
这样,只有当 :active 选择器未触发时, :hover 选择器才会被触发。
You can have the :active pseudo-class override the :hover pseudo-class regardless of the order of declaration by using the :not() selector.
This way, the :hover selector is triggered only when the :active selector is not.