Floating labels: place label before control for screen readers#42539
Open
mdo wants to merge 1 commit into
Open
Floating labels: place label before control for screen readers#42539mdo wants to merge 1 commit into
mdo wants to merge 1 commit into
Conversation
Floating labels required the control to precede the `<label>` in the DOM so the floating animation could use a sibling (`~`) selector. That order made screen readers (e.g. NVDA) announce the label after the field's value instead of before it. Switch the SCSS to look forward with `:has()` so the `<label>` can come first in the DOM while the CSS still reacts to the control's state (focus, value, disabled, autofill). Reorder all examples, docs, and the visual test to label-first markup to match. Also give `.form-control-plaintext` its own copy of the control tokens so its `var(--control-*)` references resolve, fixing phantom borders and label misalignment. Fixes #41362
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #41362.
Floating labels currently require the control to come before the
<label>in the DOM so the floating animation can use a sibling (~) selector. That ordering causes screen readers (e.g. NVDA) to announce the label after the field's value rather than before it — an accessibility concern flagged in #41362.This switches the SCSS to look forward with
:has()so the<label>can come first in the DOM (announced first by screen readers) while the CSS still reacts to the control's state (:focus,:not(:placeholder-shown),:disabled,:-webkit-autofill).:has()has been Baseline since late 2023, which is what makes this viable for v6.Changes
scss/forms/_floating-labels.scss— everycontrol ~ labelselector flipped tolabel:has(~ control).site/src/content/docs/forms/floating-labels.mdx(examples + explanatory note)site/src/content/docs/forms/validation.mdxsign-in,heroes,dialogs,cheatsheetjs/tests/visual/floating-label.htmlscss/forms/_form-control.scss— give.form-control-plaintextits own copy of the control tokens so itsvar(--control-*)references resolve, fixing phantom borders and label misalignment.Notes
position: absolute, so source order doesn't change positioning.:has(); fine for v6's browserslist.