为什么CSS中的相应标签会触发输入的悬停?

发布于 2025-01-01 14:30:16 字数 640 浏览 0 评论 0原文

我是否遗漏了某些内容,或者此示例的行为 - http://dabblet.com/result/gist/1716833 -在Webkits/Fx中比较奇怪?

有一个带有标签的输入和以下选择器:

input:hover + .target {
    background: red;
}

当您将鼠标悬停附加到 inputlabel 时,不仅会触发 input,还会触发此样式> 本身。更重要的是:带有 forlabel 和包裹在 label 中的 input 之间存在差异 - 如果您'首先将鼠标悬停在 input 上,然后将光标直接移动到 .target - 奇怪的悬停在包装版本中不会触发。

这只能在 Firefox 和 Safari/Chrome 中重现,但在 Opera 中没问题。

所以,问题是:规范中是否描述了这个问题?我找不到任何合适的地方来描述它并告诉我们什么行为是正确的。

Am I missing something, or the behavior of this example — http://dabblet.com/result/gist/1716833 — is rather strange in Webkits/Fx?

There is an input with label and the following selector:

input:hover + .target {
    background: red;
}

And this style is triggered when you hover the label attached to the input, not only the input itself. Even more: there is a difference between the label with for and input wrapped in a label — if you'd hover the input at first and then move the cursor straight to the .target — the strange hover won't trigger in wrapped version.

And this is only reproduces in Firefox and Safari/Chrome, but in Opera it's ok.

So, the questions is: if this issue is described somewhere in specs? I couldn't find any appropriate place that describes it and tells what behavior is right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

|煩躁 2025-01-08 14:30:16

现在这已包含在 HTML 规范中;直到2012 年 10 月 WD 它已添加到 W3C HTML5(强调我的):

:hover 伪类被定义为匹配“当用户使用指点设备指定元素时”的元素。仅出于定义 :hover 伪类的目的,HTML 用户代理必须将某个元素视为用户指定的元素,如果它是:

  • 用户使用指点设备指示的元素。

  • 具有用户使用指点设备指示的后代的元素。

  • 作为当前匹配 :hover 的 label 元素的带标签控件的元素。

相同的文本出现在 生活规范


几年前,我在网站联系表单的先前设计中发现了这种行为,其中 label:hover 还会在任何表单输入元素(无论是其表单输入元素)上触发 :hover后代或由其 for 属性引用。

此行为实际上已添加到此错误报告<的最新版本的 Gecko(Firefox 布局引擎)中/a> 以及 这个(相当短的)邮件列表线程,它是在 WebKit 中实现的 很多年前。正如您所注意到的,这种行为不会在 Opera 中重现; Opera Software 和微软似乎没有收到这份备忘录。

我在规范中可以找到的所有可能与此行为相关的内容是

  • :hover 伪类在用户用指点设备指定元素时应用,但不一定激活它。例如,当光标(鼠标指针)悬停在元素生成的框上时,可视用户代理可以应用此伪类。

[...]

选择器不定义“:active”或“:hover”元素的父元素是否也处于该状态。 [它似乎也没有为元素的子元素定义相同的内容。]

<块引用>

注意:如果“:hover”状态适用于某个元素,因为它的子元素是由指针设备指定的,那么“:hover”状态是可能的' 以应用于不在指点设备下方的元素。

但我可以得出的结论是,这种行为至少在 Gecko 和 WebKit 中是设计使然的。


关于你在这里所说的:

更重要的是:带有 forlabel 和包裹在 label 中的 input 之间存在差异 —如果您首先将鼠标悬停在 input 上,然后将光标直接移动到 .target - 奇怪的悬停在包装版本中不会触发。

鉴于上述行为,唯一的可能性是您只是被级联咬伤了。

基本上,这条规则:

/* 1 type, 1 pseudo-class, 1 class -> specificity = (0, 2, 1) */
input:hover + .target {
    background: red;
}

比这条规则更具体:

/* 1 class, 1 pseudo-class         -> specificity = (0, 2, 0) */
.target:hover {
    background: lime;
}

因此,在适用的浏览器中,第一个复选框的 label.target 在悬停时将始终为红色,因为更具体的规则始终优先。第二个复选框后面跟着一个 span.target,因此此行为均不适用;当光标位于 span.target 上时,只有第二条规则才能生效。

This is in the HTML spec now; it wasn't until the October 2012 WD that it was added to W3C HTML5 (emphasis mine):

The :hover pseudo-class is defined to match an element "while the user designates an element with a pointing device". For the purposes of defining the :hover pseudo-class only, an HTML user agent must consider an element as being one that the user designates if it is:

  • An element that the user indicates using a pointing device.

  • An element that has a descendant that the user indicates using a pointing device.

  • An element that is the labeled control of a label element that is currently matching :hover.

Identical text appears in the living spec.


I discovered this very behavior a few years ago on the previous design of my site's contact form, where label:hover also triggers :hover on any form input element that is either its descendant or referenced by its for attribute.

This behavior was actually added to a recent build of Gecko (Firefox's layout engine) in this bug report along with this (rather short) mailing list thread, and it was implemented in WebKit many years back. As you note, the behavior doesn't reproduce in Opera; it looks like Opera Software and Microsoft didn't get the memo.

All I can find in the spec that could relate to this behavior somehow is here, but I don't know for sure (italicized note by me):

  • The :hover pseudo-class applies while the user designates an element with a pointing device, but does not necessarily activate it. For example, a visual user agent could apply this pseudo-class when the cursor (mouse pointer) hovers over a box generated by the element.

[...]

Selectors doesn't define if the parent of an element that is ‘:active’ or ‘:hover’ is also in that state. [It does not appear to define the same for the child of an element either.]

Note: If the ‘:hover’ state applies to an element because its child is designated by a pointing device, then it's possible for ‘:hover’ to apply to an element that is not underneath the pointing device.

But what I can conclude is that this behavior is by design in at least Gecko and WebKit.


Regarding what you state here:

Even more: there is a difference between the label with for and input wrapped in a label — if you'd hover the input at first and then move the cursor straight to the .target — the strange hover won't trigger in wrapped version.

Given the above behavior, the only possibility left here is that you've simply been bitten by the cascade.

Basically, this rule:

/* 1 type, 1 pseudo-class, 1 class -> specificity = (0, 2, 1) */
input:hover + .target {
    background: red;
}

Is more specific than this rule:

/* 1 class, 1 pseudo-class         -> specificity = (0, 2, 0) */
.target:hover {
    background: lime;
}

So in applicable browsers, the label.target by your first checkbox will always be red on hover, because the more specific rule always takes precedence. The second checkbox is followed by a span.target, so none of this behavior applies; only the second rule can take effect while the cursor is over the span.target.

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