Favorites

CSS Floating Labels

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
CSS Floating Labels

What are floating labels?

Floating labels are a popular form design where each label starts inside its field, like placeholder text. When you click into the field, or once it has a value, the label shrinks and moves up to the top edge, so it stays visible while you type. It gives forms a compact, tidy look.

Try it: click into a field, or press Tab to move through them.

Results

We'll only use this to reply to you.

In this post we'll build this form with HTML and CSS only, then weigh up when floating labels are a good idea.

The HTML

<form class="float-form">
  <div class="field">
    <label for="first-name">First name</label>
    <input type="text" id="first-name" name="first-name" autocomplete="given-name" placeholder=" ">
  </div>
  <div class="field">
    <label for="last-name">Last name</label>
    <input type="text" id="last-name" name="last-name" autocomplete="family-name" placeholder=" ">
  </div>
  <div class="field">
    <label for="email">Email address</label>
    <input type="email" id="email" name="email" autocomplete="email" placeholder=" ">
  </div>
</form>
  • Each field is wrapped in a .field element, so we can position the label against it.
  • Each <label> is linked to its input by matching for to the input's id. Screen readers (software that reads pages aloud) announce the label, and clicking it focuses the field.
  • The label comes before the input, in the natural reading order. The original version of this post had to put it after the input, because older CSS could only style elements that come later. The :has() selector removes that limit.
  • placeholder=" " (a single space) is a trick that lets CSS detect whether the field is empty, as we'll see shortly. A space is used rather than an empty value, as some browsers ignore an empty placeholder.
  • autocomplete lets browsers fill in saved details.

Positioning the label

First, the field wrapper becomes the reference point for positioning, and the label sits on top of the input:

.field {
  position: relative;
}

.field input {
  display: block;
  width: 100%;
  box-sizing: border-box;
  padding: 22px 12px 8px;      /* extra space at the top for the floated label */
  font: inherit;
  font-size: 1rem;             /* at least 16px stops iPhones zooming in */
  border: 1px solid #6b7280;
  border-radius: 8px;
}

.field label {
  position: absolute;
  top: 15px;
  left: 13px;
  color: #4b5563;
  pointer-events: none;        /* clicks go straight through to the input */
  transform-origin: left top;  /* shrink towards the top-left corner */
  transition: transform 0.2s ease, color 0.2s ease;
}

/* Hide the single-space placeholder */
.field input::placeholder {
  color: transparent;
}

The input has more padding at the top than the bottom, so there's room for the label once it floats up, without the field changing height.

Floating the label with :has()

The label should float in two situations: when the field has focus, and when it has a value. Here's the key rule:

.field:has(input:focus, input:not(:placeholder-shown)) label {
  transform: translateY(-9px) scale(0.75);
}
  • :has() styles the .field wrapper based on what's inside it. This rule reads: "a field that contains a focused input, or an input whose placeholder isn't showing".
  • :placeholder-shown matches an input while its placeholder is visible, which only happens when the input is empty. So :not(:placeholder-shown) means "the field has a value". This is why we gave every input a placeholder, even though it's only a space.
  • translateY() and scale() move the label up and shrink it to 75% of its size.

Smoother animation: the original version of this post animated top and font-size. Changing those makes the browser recalculate the layout on every frame. transform moves and scales the label without affecting anything else, so the animation stays smooth.

Focus styles and colours

.field input:focus {
  border-color: #1d4ed8;
  outline: 3px solid #1d4ed8;
  outline-offset: 2px;
}

.field:has(input:focus) label {
  color: #1d4ed8;
}

The colours matter too:

  • Field borders need a contrast ratio of at least 3:1 with the background, so people can see where to type. The original #ccc was only about 1.6:1 on white; #6b7280 is about 4.8:1.
  • Label text needs at least 4.5:1. The original #777 was just below that, and once a label shrinks, it's even harder to read. #4b5563 is about 7.6:1.

Text areas, selects and hints

The same technique works for a <textarea>: give it a placeholder=" " too, and include it in the selectors:

.field:has(:is(input, textarea):focus, :is(input, textarea):not(:placeholder-shown)) label {
  transform: translateY(-9px) scale(0.75);
}

A <select> always shows one of its options, so there's never an empty state for the label to sit in. Keep its label floated all the time:

.field:has(select) label {
  transform: translateY(-9px) scale(0.75);
}

And because the placeholder is taken, any extra guidance should go in a hint below the field, linked with aria-describedby so screen readers read it too:

<div class="field">
  <label for="email">Email address</label>
  <input type="email" id="email" placeholder=" " aria-describedby="email-hint">
  <p id="email-hint" class="hint">We'll only use this to reply to you.</p>
</div>

The demo at the top of this post includes all three.

Pros, cons and accessibility

Pros

  • Compact: each field takes up less vertical space than a field with a label above it.
  • The label stays visible: unlike a plain placeholder, which disappears as soon as you start typing, the label stays readable at the top of the field.
  • A clean look that suits minimal designs.

Cons

  • Small text: the floated label is smaller than normal text, which some people find hard to read.
  • Long labels don't fit: labels that wrap onto two lines, or are translated into longer languages, can overlap the input.
  • Empty fields can look filled in: some people mistake a label sitting inside a field for an answer that's already been entered, and skip it.
  • No room for examples: the placeholder is used up, so formatting help (like "dd/mm/yyyy") has to go in a hint below.

Tip: for long or complex forms, such as checkouts or applications, a plain label above each field is still the clearest and most accessible design. Floating labels work best for short, familiar forms, such as sign-in, newsletter or contact forms.

Accessibility checklist

  • Use a real <label> linked with for and id, never a placeholder on its own.
  • Keep labels short, so they fit when floated.
  • Make sure both the full-size and floated labels have strong contrast.
  • Keep a visible focus outline.
  • Put hints and error messages below the field, linked with aria-describedby.
  • Switch off the animation for people who prefer reduced motion:
    @media (prefers-reduced-motion: reduce) {
      .field label {
        transition: none;
      }
    }

Wrapping up

Floating labels combine the tidiness of a placeholder with the clarity of a real label. With :has() and :placeholder-shown, they need no JavaScript at all, and the label can stay in its natural place before the input. Animate with transform, keep strong contrast and a visible focus outline, and save the pattern for short forms, where it shines.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee