Floating label inputs in pure CSS

The label sits inside the field until you tap it, then floats up out of the way and stays there. Three styles, no JavaScript.

UI loop

Labels that float up

We only use it to send your receipt.

Sundry Millsundrymill.com/library
Floating label inputs in pure CSS

Everything in the preview is live. Try it.

The code, one by one

Shared: the field, the label and the float
.fl { position: relative; width: min(100%, 300px); }
.fl input {
  box-sizing: border-box;
  width: 100%;
  height: 56px;
  margin: 0;
  padding: 22px 14px 6px;
  color: #17202C;
  font: inherit;
  font-size: 16px; /* 16px or more, so phones do not zoom in on focus */
}
.fl input:focus { outline: none; }
.fl label {
  position: absolute;
  left: 14px;
  top: 18px;
  color: #57606C;
  font-size: 16px;
  line-height: 20px;
  pointer-events: none;
  transform-origin: left top;
  transition: transform .2s cubic-bezier(.33, 1, .68, 1), color .2s;
}
/* the label floats while the field has focus, and stays up once it holds text */
.fl input:focus + label,
.fl input:not(:placeholder-shown) + label { transform: translateY(-11px) scale(.75); }
.fl input:focus + label { color: #2F55D4; }
@media (prefers-reduced-motion: reduce) {
  .fl label { transition: none; }
}

Outlined

The label rises onto the border and the border turns blue while you type. A hint sits underneath, linked to the field.

HTML
<div class="fl fl-outline">
  <input id="fl-email" type="email" placeholder=" " autocomplete="email" aria-describedby="fl-email-hint">
  <label for="fl-email">Email</label>
  <p class="fl-hint" id="fl-email-hint">We only use it to send your receipt.</p>
</div>
CSS
.fl-outline input {
  padding: 0 14px;
  border: 1.5px solid #6B7480;
  border-radius: 12px;
  background: #FFFFFF;
  transition: border-color .2s, box-shadow .2s;
}
.fl-outline input:focus { border-color: #2F55D4; box-shadow: 0 0 0 1px #2F55D4; }
.fl-outline input:focus + label,
.fl-outline input:not(:placeholder-shown) + label {
  transform: translateY(-27px) scale(.8);
  padding: 0 4px;
  margin-left: -4px;
  background: #FFFFFF;
}
.fl-hint { margin: 6px 0 0 14px; color: #57606C; font-size: .875rem; }

Filled

A soft tinted field with a line underneath. The label shrinks to the top corner.

HTML
<div class="fl fl-filled">
  <input id="fl-name" type="text" placeholder=" " autocomplete="name">
  <label for="fl-name">Full name</label>
</div>
CSS
.fl-filled input {
  border: 0;
  border-bottom: 2px solid #6B7480;
  border-radius: 10px 10px 0 0;
  background: #F0EBE3;
  transition: border-color .2s;
}
.fl-filled input:focus { border-bottom-color: #2F55D4; }

Underline

Just a line, for forms that should feel light. The line thickens on focus.

HTML
<div class="fl fl-line">
  <input id="fl-code" type="text" placeholder=" " autocomplete="off" spellcheck="false">
  <label for="fl-code">Promo code</label>
</div>
CSS
.fl-line input {
  padding-inline: 0;
  border: 0;
  border-bottom: 2px solid #6B7480;
  border-radius: 0;
  background: transparent;
  transition: box-shadow .2s, border-color .2s;
}
.fl-line input:focus { border-bottom-color: #2F55D4; box-shadow: 0 1px 0 #2F55D4; }
.fl-line label { left: 0; }

How to use it

  • Each field needs the shared CSS plus its own block. The label comes after the input in the HTML, because the CSS finds it with the next sibling selector.
  • The placeholder is a single space. The field shows it while empty, so :placeholder-shown tells the CSS whether there is text in the field. The words people read are always the real label.
  • Inputs are 16 pixels, because Safari on the iPhone zooms in on any field smaller than that and does not zoom back out.
  • The label text is #57606C, 6.4:1 on white, and the borders are #6B7480, 4.7:1, so both clear the contrast lines for text and for a control.

When to use it

  • Short forms on a phone, such as sign in, checkout and sign up, where a label above every field would push the button off the screen.
  • Forms with simple, familiar fields: email, name, phone, card number. The label is short enough to shrink and still be read.
  • Not for long or unusual questions. A label that needs a sentence belongs above the field at full size, where it never has to shrink.

A worked example: an email sign up with an error

The outlined field on a newsletter form, with a hint underneath that becomes an error message when the address is wrong.

  1. Keep type="email" and autocomplete="email", so phones show the right keyboard and offer the address they know.
  2. On submit, check field.validity.valid. If it fails, set aria-invalid="true" on the input and replace the hint text with the reason, such as "Add the part after the @".
  3. Colour the border and the hint red only while aria-invalid is set, and keep the words: colour on its own does not tell everyone what went wrong.
  4. A browser that fills in the address for you leaves the placeholder hidden, so the label floats up and the saved address never sits on top of it.
The error state
.fl-outline input[aria-invalid="true"] { border-color: #B42E3C; box-shadow: 0 0 0 1px #B42E3C; }
.fl-outline input[aria-invalid="true"] + label { color: #B42E3C; }
.fl-outline input[aria-invalid="true"] ~ .fl-hint { color: #B42E3C; }

Showing an interaction like this to a client or in a tutorial? Framok records your screen and zooms in on every click for you.

Source: W3C Web Accessibility Initiative, Form Instructions, on why placeholder text is not a replacement for a label.

Free to use in personal and commercial work. No credit needed, though a link back is always nice.

More from the library

See the whole library