By Tim King · Updated
Floating Label CSS
Outlined, Filled and Underline Floating Labels Without JavaScript
OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.
How the Floating Label Form works
This floating label CSS builder writes the markup and styles for you. Floating labels give you the clean look of placeholder-only inputs without losing the label once someone types. Pick outlined, filled or underline, list your fields, tune the colours and copy a form where the label lifts on focus using nothing but CSS — click a field in the preview to see it move.
Floating label CSS: how the label moves
The trick is the :placeholder-shown pseudo-class. Each input has a placeholder attribute
whose text is made transparent, so it is never visible — but the browser still knows whether it
is "shown", which is only when the field is empty. The label sits over the input, and two
selectors move it: input:focus + label lifts it while the field is active, and
input:not(:placeholder-shown) + label keeps it lifted whenever there is a value. No script,
no state.
Source order matters. The label comes after the input in the markup so the + sibling
combinator can reach it. Visually it is positioned absolutely over the input, so the order is
invisible to users, and <label for> still links the two for clicks and screen readers.
Outlined. The classic Material outline: a bordered box, and the label shrinks and moves up to sit on the border. A background colour behind the label masks the border line beneath it, which is why the label background matches the field background. On focus the border takes the accent colour with a soft ring.
Filled. A tinted block with a bottom border only. The input gets extra top padding so the shrunk label fits inside the box above the text, rather than crossing a border.
Underline. The most minimal: no box, just a bottom rule that changes colour on focus. The label behaves as in the filled style, sliding up inside the field's top padding.
Transitions. The label animates transform and top, both cheap for the browser. The scale
is 0.8 for outlined and 0.75 for the other two, chosen so the shrunk label stays legible at a
16px base size.
Autofill. Browsers that autofill a field give it a value, so :placeholder-shown becomes
false and the label lifts correctly. Chrome also applies its own autofill background; override
:-webkit-autofill if that clashes with your field colour.
FAQ
Does this work without JavaScript?
Yes. Both the lift on focus and the "stay lifted when filled" behaviour come from CSS
pseudo-classes. :placeholder-shown is supported in every current browser, including Safari
and mobile browsers.
Why is there a placeholder attribute if it is invisible?
Because :placeholder-shown only matches when the input has a placeholder to show. The text is
made transparent so it never appears, but its presence lets CSS detect an empty field. Use the
label text as the placeholder value so nothing odd is read out if the styling fails.
Are floating labels accessible?
They are, as long as the label is a real <label for> element and stays visible once the field
is filled, which this pattern guarantees. Check the resting label colour against the field
background for 4.5:1 contrast; grey-on-white defaults often fall short.
Can I use this on a textarea or select?
Textareas work with the same CSS, but the label should rest near the top rather than vertically
centred, so adjust top for .fl textarea + label. Selects do not support placeholders, so use
:valid with a required attribute and an empty first option instead of :placeholder-shown.
The label overlaps the border in outlined mode. Why?
The label needs a background that matches the field to mask the border where they cross. If your field background is transparent or the page behind it differs, set the label background to the page colour. The generator uses the field background for both.
See also: all Forms & Inputs tools, Layout & Spacing, Shadows & Motion, Shapes & SVG, UI Components, Colour.
Related tools
Multi-Step Form Builder
Wizard forms with step indicators, per-step validation and Back/Next buttons.
Input Mask Generator
Auto-formatting inputs for phone, date, card and custom patterns.
Password Strength Meter
Colour-coded password strength bar with a small scoring script.
Password Visibility Toggle
Show/hide password button with eye icon or text and correct ARIA state.
Select Dropdown Builder
Custom-styled native select with your own arrow, no JavaScript.