Floating Labels Form Template

Create a clean, modern, and interactive form with this slick floating label effect.

This popular design pattern saves space and adds a delightful micro-interaction by having the field's placeholder text animate upward and transform into a permanent label when the user begins typing.

Screenshot of a form where the label for an input field floats above it once the user types.

Get Source Code → Preview →

About this Floating Label Form

The floating label is a brilliant solution that combines the benefits of a placeholder (saves space) and a permanent label (user doesn't lose context after typing). This template achieves the effect with pure CSS, though a small bit of JavaScript can enhance it.

How it's done with CSS:

  1. Each form group div is set to position: relative;.
  2. The label is absolutely positioned inside the input area.
  3. When the user focuses on the input, or when the input contains text (using the :placeholder-shown pseudo-class), we use CSS selectors (like input:focus + label) to move the label up and shrink its font size.
  4. A CSS transition is applied to the label to create the smooth animation.

While CSS can handle most cases, a tiny bit of JS is sometimes added to handle edge cases, such as autofilled inputs, to ensure the label stays floated.

Features

Dependencies

None. Uses pure HTML and CSS for the core effect, with an optional but recommended small JavaScript enhancement for robustness.

Code

Here's the full code for the floating label form template:

View Output