Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-selectors] Make <label> elements reflect CSS pseudoclasses on associated form element #397

gregwhitworth opened this issue Aug 10, 2016 · 9 comments


Copy link

@gregwhitworth gregwhitworth commented Aug 10, 2016

This discussion was taking place in the WHATWG, moving this over to the CSSWG repo so all CSS members see it and are able to engage. For reference, here is the issue for more context: whatwg/html#1632 (comment)

Copy link
Contributor Author

@gregwhitworth gregwhitworth commented Aug 10, 2016

@frivoal To answer your question, it is still in Edge but off by default behind a flag as no other browser currently implements it this way.

Copy link

@bkardell bkardell commented Aug 11, 2016

@gregwhitworth do all of them work in edge with the flag? :checked etc?

Copy link
Contributor Author

@gregwhitworth gregwhitworth commented Mar 2, 2017

@bkardell No it doesn't propagate those, I've updated the testcase to include those,css,output

Copy link

@fantasai fantasai commented Jan 1, 2018

Related discussion on www-style. (This issue keeps coming up.)

Copy link

@therealglazou therealglazou commented Jan 30, 2018

This is a 18 years old discussion in the CSS WG (see Reference Combinator in and we already discussed it back in '97...) : I think we should not solve the individual <label> case directly but have a generic mechanism for ID/IDREF references. There are many other ID/IDREF cases in dozens of XML dialects that could benefit from such a generic mechanism, including in EPUB.

Copy link

@tigt tigt commented Apr 8, 2018

@therealglazou That would be nice, but <label>s can also be associated with descendant form elements without ID/IDREFs, so this seems to have a slightly larger scope.

As an author, this is a somewhat silly example of what I would love to use this functionality for:

<label>2 + 2 = ?
  <svg><use href="#correct" /><use href="#incorrect" /></svg>
  <input name="answer" pattern="4" required />
use {
  display: none;

label:valid > [href="#correct"],
label:invalid > [href="#incorrect"] {
  display: inline;

It’s sort of possible today to do this by placing the elements after the form element in question and using something like input:valid ~ foo, but that can be impossible with certain markup output — it’s common to wrap <select> inside a <span> for styling purposes, for example.

Copy link

@TUSF TUSF commented Feb 10, 2019

As someone who's never worked on a layout engine, I have trouble understanding how this would add much more complexity. The argument against it I saw is that the engine would have to keep track of the element of the associated input. But UAs already do this to some extent—a <label> is almost treated as an extension of its referenced input, allowing you to click a label to tick a checkbox or radio button.

To me, this feature just seems implied from the already present behavior, but perhaps I'm misunderstanding something.

@fantasai fantasai added selectors-5 and removed selectors-4 labels Feb 25, 2019
Copy link

@css-meeting-bot css-meeting-bot commented Feb 25, 2019

The CSS Working Group just discussed <label> elements, and agreed to the following:

  • RESOLVED: Push this out to Level 5
The full IRC log of that discussion <presenter> Topic: <label> elements
<astearns> github:
<presenter> AmeliaBR: I brought this up on WHATWG because the relevant text is in HTML, but they threw it back to CSS.
<presenter> AmeliaBR: Often you want to do some styling on the label or on gencon that depends on :required or :invalid or :focus on the label's associated inputl
<presenter> AmeliaBR: Right now the only way to do that is by modifying your DOM structure so the label is a following sibling of the input.
<presenter> AmeliaBR: Having to modify your DOM to do styling is iffy, there are some cases where this can't work.
<presenter> AmeliaBR: And it also means you can't use the implicit association of label with child inputs; you have to use IDs, which can cause problems in dynamic content.
<presenter> AmeliaBR: So the suggestion was that the <label> should reflect the form-related pseudos, and :focus/:hover, of the associated form element.
<presenter> AmeliaBR: Last time this was discussed there were some perf concerns, but labels already have a DOM association (you can chase a DOM property to see the associated input)
<xfq> WHATWG issue:
<astearns> some concerns from bz:
<presenter> AmeliaBR: As I recall there is something in the HTML spec about how focus or hover already propagates in a certain way...
<presenter> myles: People use labels and form controls everywhere already, is this gonna break anything?
<presenter> AmeliaBR: If you've got `label:invalid` that currently does nothing, then it could be an issue.
<presenter> AmeliaBR: Or perhaps some of the more common pseudos
<presenter> TabAtkins: I think :hover is the most likely to see some new stuff
<presenter> emilio: Eh, reasonable to see `form :invalid`, would trigger more.
<presenter> emilio: And the fact that labels can be anywhere in the DOM (current state propagation, like to fieldset, is just parent/ancestor-based), talks to BZ's concern about this being one more thing to slow things down.
<bkardell_> I see
<AmeliaBR> `label:for(:required)`
<presenter> AmeliaBR: On the perf issue, it's worth remembering that labels and inputs already have DOM properties that link to each other. But they probably aren't used much, so they might be slow getters, I dunno.
<presenter> emilio: Also worth noting that CSS needs a 2-way link; label->input for the selector, but input->label for invalidation
<astearns> tab: suggests houdini
<bkardell_> will note that I just yay'ed to a thing that wasn't in the minutes :)
<AmeliaBR> Re DOM APIs, labels have a `control` property, while labelable elements have `labels` (node list)
<fantasai> TabAtkins: In general I'd say resolve to reject at this point, since still implementer resistence on perf grounds. Which makes me sad because I've run into these exact problems.
<bkardell_> can we note the "waves hands and invokes potential houdini solution" int he rejects notes
<fantasai> astearns: Not really a rejection, just no implementer interest yet
<fantasai> TabAtkins: Yeah, not going in as expected feature of the spec atm
<fantasai> AmeliaBR: If we do it in the way Tab proposes, to avoid any back-compat issues with :for() pseudo
<fantasai> AmeliaBR: So there's a CSS issue and a WHATWG issue
<fantasai> AmeliaBR: CSSWG says if we do this, this is how we do
<fantasai> astearns: And try it out iwht Houdini
<fantasai> TabAtkins: I still have to finish TypedOM, but custom selectors will rpobably be next
<fantasai> hober: There's the ? work to do association without IDREFs, using direct assignment. Should probably tie in with that
<hober> s/?/AOM/
<fantasai> TabAtkins: I recently had discussions with ppl about solving problems, can we have a selector associated with a map that I have created
<fantasai> TabAtkins: seems like would solve a lot of problems
<fantasai> TabAtkins: should be part of houdini work
<fantasai> RESOLVED: Push this out to Level 5
Copy link

@SebastianZ SebastianZ commented Dec 1, 2020

I am strongly opposed to let labels directly reflect the pseudo-classes of the associated form control.

  1. The state of the form control doesn't apply to the label. E.g. it's the form control that is focused, not the label.
  2. Letting pseudo-classes behave differently depending on whether they are prepended by label or not is confusing.
  3. Letting the pseudo-classes apply to labels now may break existing websites.

Therefore, solving this use case via a pseudo-class function like :for() as discussed in #1067 is much more logical, in my eyes. Furthermore a function like that has the advantage of being extensible and covering more than just form control states.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants
You can’t perform that action at this time.