Floating Label (pure CSS) Playground

Label floats up when the field is filled or focused: :placeholder-shown + :focus + sibling selector, zero JS. The one-space placeholder trick. Type & see. An…

Why it matters

A lot of form UI that looks like it needs JavaScript only needs pseudo-classes. Knowing why the HTML puts the input before the label, and why the placeholder must be one space, keeps the trick from breaking in someone else’s hands, and a real <label> keeps the field named for screen readers.

The key idea

Two pseudo-classes, one combinator, no JS

Common mistakes

Try it in this playground