为什么悬停伪类会覆盖活动伪类

发布于 2024-12-05 17:38:21 字数 742 浏览 0 评论 0原文

标题基本上说明了一切。

假设我有一个元素想要在 :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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我要还你自由 2024-12-12 17:38:21

是的,这是预期的行为,

让我们看另一个例子。只需添加两个类,

<ul>
<li class="item first">item</li>
<li class="item">item</li>
<li class="item">item</li>
<li class="item">item</li>
<li class="item last">item</li>
</ul>

这里​​类第一个也与类项一起出现。
但是,如果我们以错误的顺序声明 css,而不会给出

.first { background: blue; }
.item { background: red; }

您所看到的所需行为,则将使用最后一个匹配的选择器。
和你的例子是一样的,不管怎样更符合逻辑,
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,

<ul>
<li class="item first">item</li>
<li class="item">item</li>
<li class="item">item</li>
<li class="item">item</li>
<li class="item last">item</li>
</ul>

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

.first { background: blue; }
.item { background: red; }

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.

秋叶绚丽 2024-12-12 17:38:21

活动状态必须在悬停状态之后声明,在CSS中,您将活动状态之前聚集在一起,这样它就不会被触发。

如果您指定正确的操作顺序,它就可以工作,如下所示,它就可以正常工作。

a.noworks:link, a.noworks:visited {
    background: red;
}

a.noworks:hover {
    background: green;
}

a.noworks:active {
    background: red;
}

所以,回答你的问题,是的,这是预期的行为。

以下是操作顺序:

a:link
a:visited
a:hover
a:active

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.

a.noworks:link, a.noworks:visited {
    background: red;
}

a.noworks:hover {
    background: green;
}

a.noworks:active {
    background: red;
}

So, to answer your question, yes this is the expected behavior.

Here is the order of operation:

a:link
a:visited
a:hover
a:active
巾帼英雄 2024-12-12 17:38:21

因为在第一个代码中,您在定义 :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.

淡忘如思 2024-12-12 17:38:21

编辑:

抱歉,我误解了这个问题。

基本上,当您处于活动状态(使用鼠标指针)时,您实际上也处于悬停状态。因此,根据 CSS 规则,它将读取样式表中的最后一个。

当您将鼠标悬停在链接上并按住鼠标键时,如果我们将伪类视为普通类,就会像这样:

<a class="active hover"></a>

因此,如果您的CSS是,

.active{color:green}
.hover{color:red}

它将应用红色,

但如果您的CSS是,

.hover{color:red}
.active{color:green}

它将应用绿色

来自 W3C

a:link { color: red } /* 未访问的链接 */
a:visited { color: blue } /* 访问过的链接 */
a:hover { color: Yellow } /* 用户悬停 */
a:active { color: Lime } /* 活动链接 */

请注意,A:hover 必须放置在 A:link 和 A:visited 之后
规则,否则级联规则将隐藏“颜色”
A:hover 规则的属性。同样,因为 A:active 被放置
A:悬停后,当用户同时使用时,将应用活动颜色(石灰)
激活并悬停在 A 元素上。

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 :

<a class="active hover"></a>

So if your css was

.active{color:green}
.hover{color:red}

it would apply red

but if your css was

.hover{color:red}
.active{color:green}

It would apply green

From W3C

a:link    { color: red }    /* unvisited links */
a:visited { color: blue }   /* visited links   */
a:hover   { color: yellow } /* user hovers     */
a:active  { color: lime }   /* active links    */

Note that the A:hover must be placed after the A:link and A:visited
rules, since otherwise the cascading rules will hide the 'color'
property of the A:hover rule. Similarly, because A:active is placed
after A:hover, the active color (lime) will apply when the user both
activates and hovers over the A element.

为你鎻心 2024-12-12 17:38:21

这就是它的工作原理,我将尝试解释原因。正如我们所知,CSS 在应用样式时会继续搜索文档并应用最特定于该元素的样式。

示例:

li.betterList { better list styling here }

更具体,将覆盖

li { list styling here }

这些 Puesdo 选择器都被认为具有相同的特异性,因此最后一行将覆盖前一行。 W3Schools 上的注释证实了这一点

注意: 在 CSS 定义中,:active 必须位于 :hover(如果存在)之后才能生效!

你也可以把这个 CSS 扔到你的 jsfidle 上并观察它的覆盖,因为它们具有相同的特异性

.works {background: red}
.works {background: green}

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:

li.betterList { better list styling here }

Is more specific and will overwrite

li { list styling here }

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

Note: :active MUST come after :hover (if present) in the CSS definition in order to be effective!

you can also throw this CSS on your jsfidle and watch it overwrite since they are the same specificity

.works {background: red}
.works {background: green}
不寐倦长更 2024-12-12 17:38:21

这是预期的行为,因为大多数人总是将 :hover 伪类放在规则组的末尾。

声明的顺序与伪类有关(请参阅此处的更多信息: http://reference.sitepoint.com/css/伪类),因此最终规则优先,就像 CSS 中的其他规则一样。

对于大多数人来说,我认为期望的行为:

a:link {
  ⋮ declarations
}
a:visited {
  ⋮ declarations
}
a:hover {
  ⋮ declarations
}

由于 :active 不太有用,因此被忽略......或与 a:linka 结合使用:visited... 然后它被 a:hover 覆盖

W3C 在这里说明:

请注意,A:hover 必须放置在 A:link 和 A:visited 之后
规则,否则级联规则将隐藏“颜色”
A:hover 规则的属性。同样,因为 A:active 被放置
A:悬停后,当用户同时使用时,将应用活动颜色(石灰)
激活并悬停在 A 元素上。

http://www.w3.org/TR/CSS2/selector .html#dynamic-pseudo-classes

甚至 W3schools 也正确地理解了这一点:

注意:CSS 中 a:hover 必须位于 a:link 和 a:visited 之后
定义才能有效!!

注意:CSS 定义中 a:active 必须位于 a:hover 之后
才能有效!!

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:

a:link {
  ⋮ declarations
}
a:visited {
  ⋮ declarations
}
a:hover {
  ⋮ declarations
}

Since the :active is not so useful it is left out... or combined with a:link and a:visited... and then it is overridden by a:hover

W3C spells it out here:

Note that the A:hover must be placed after the A:link and A:visited
rules, since otherwise the cascading rules will hide the 'color'
property of the A:hover rule. Similarly, because A:active is placed
after A:hover, the active color (lime) will apply when the user both
activates and hovers over the A element.

http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes

Even W3schools gets this one right:

Note: a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective!!

Note: a:active MUST come after a:hover in the CSS definition in order
to be effective!!

http://www.w3schools.com/css/css_pseudo_classes.asp

凉墨 2024-12-12 17:38:21

我认为您至少应该考虑链接(或按钮)上的用户交互流程

通常,

  1. :link 始终是默认的(未触及)
  2. 然后当用户指向按钮时,:hover 就会出现进入游戏。
  3. 一旦用户指向链接或按钮,他/她就会点击,这就是 :active 的用武之地。

这是我们与链接(或按钮)交互的标准顺序。 :visited 除外,只有当用户先前按下该链接时,结果才会明显。

如果您在处理链接时记住助记符:' L o V e H A te ',那将会非常有帮助(除了 :visited,它不适用于按钮)

但是,如果您确实想要进行覆盖,例如,您想要更改已在活动状态下访问过的链接的颜色,您可以执行以下操作:

a:visited:active {
     color: red;   
}

但底线是,如果不是,请避免更改标准序列必要的。

I think you should at least consider the flow of User Interaction on links (or buttons).

Usually,

  1. :link has always been the default (untouched),
  2. Then when a User points to the button, then that is where :hover comes into play.
  3. Once a User points to the link or button, then he/she will click, that is where :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:

a:visited:active {
     color: red;   
}

But the bottom line is, avoid changing the standard sequence if it's not necessary.

遮了一弯 2024-12-12 17:38:21

您可以使用 :not() 选择器让 :active 伪类覆盖 :hover 伪类,无论声明顺序如何。

a:link, a:visited, a:active {
    background: red;
}

a:hover:not(:active) {
    background: green;
}

这样,只有当 :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.

a:link, a:visited, a:active {
    background: red;
}

a:hover:not(:active) {
    background: green;
}

This way, the :hover selector is triggered only when the :active selector is not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文