Skip to main contentSkip to docs navigation

Chassis

Floating Labels

View on GitHub

Animate a label from placeholder text to a floating position above the input on focus or when the field holds a value.

@layer components
Depends onForm Input
Component tokens

Introduction

Floating labels wrap a <label> and a form control inside .form-floating. The label sits in the placeholder position until the control gains focus or holds a value, then transitions to a floating position above it.

Implementation details. The label uses the :has() relational selector to react to its sibling .form-input's :focus and :placeholder-shown state, applying the floated position without extra classes or JavaScript.

Basic structure

Wrap a <label> and a form control with the .form-input class in a .form-floating container. The structure has three requirements:

  1. The <label> must come before the form control in the markup — the label detects its sibling's state through the subsequent-sibling combinator inside :has()
  2. Each <input> and <textarea> requires a placeholder attribute — without one, :placeholder-shown never matches and the label stays floated even when the field is empty
  3. Each label must be properly associated with its form control using the for attribute

Sizing. .form-floating does not respond to the .sm and .lg size modifiers. Floating inputs always render at the height of $form-floating-height.

Example

An email and a password field, each with its label before the input and a placeholder on the input.

HTML
<div class="form-floating mb-md">
  <label for="floatingEmail">Email address</label>
  <input type="email" class="form-input" id="floatingEmail" placeholder="name@example.com">
</div>
<div class="form-floating">
  <label for="floatingPassword">Password</label>
  <input type="password" autocomplete="current-password" class="form-input" id="floatingPassword" placeholder="Password">
</div>

Pre-filled values

An input with a value shows its label in the floated position from the start.

HTML
<div class="form-floating">
  <label for="floatingInputValue">Input with value</label>
  <input type="email" class="form-input" id="floatingInputValue" placeholder="name@example.com" value="test@example.com">
</div>

Form validation

Validation classes like .is-invalid style floating label inputs the same way they style regular inputs.

HTML
<div class="form-floating">
  <label for="floatingInputInvalid">Invalid input</label>
  <input type="email" class="form-input is-invalid" id="floatingInputInvalid" placeholder="name@example.com" value="test@example.com">
</div>

Input types

Floating labels apply the same markup pattern to textareas, selects, and file inputs, with small adjustments for each control type.

Textarea

Without a rows attribute, a <textarea> defaults to two native rows and renders taller than an <input>.

HTML
<div class="form-floating">
  <label for="floatingTextarea">Comments</label>
  <textarea class="form-input" placeholder="Share your thoughts here" id="floatingTextarea"></textarea>
</div>

Set either the rows attribute or an explicit height (inline style or custom CSS) for a custom <textarea> height; rows="1" matches an <input>'s height.

HTML
<div class="form-floating">
  <label for="floatingTextareaRows1">Comments</label>
  <textarea class="form-input" rows="1" placeholder="Share your thoughts here" id="floatingTextareaRows1"></textarea>
</div>
<div class="form-floating">
  <label for="floatingTextareaHeight">Comments</label>
  <textarea class="form-input" style="height: 100px" placeholder="Share your thoughts here" id="floatingTextareaHeight"></textarea>
</div>

Select

A <select> with the .form-input class works inside .form-floating. Unlike an input, a select always shows the label in its floated position, since :placeholder-shown never matches a <select>.

HTML
<div class="form-floating">
  <label for="floatingSelect">Preferences</label>
  <select class="form-input" id="floatingSelect">
    <option selected>Choose an option</option>
    <option value="1">Option One</option>
    <option value="2">Option Two</option>
    <option value="3">Option Three</option>
  </select>
</div>

Limitations. Select elements using the size or multiple attribute render as multi-row list boxes, which do not fit .form-floating's fixed single-line height and padding.

File inputs

A file input works inside .form-floating too. <input type="file"> does not support the placeholder attribute, so :placeholder-shown never matches it and, as with selects, the label always renders in its floated position.

Browser differences. Browsers render the file selector button and the file name differently, so a floating file input does not look the same in every browser.

HTML
<div class="form-floating">
  <label for="floatingFileInput">Upload document</label>
  <input type="file" class="form-input" id="floatingFileInput">
</div>

Disabled inputs

Add the disabled attribute to an input, a textarea, or a select inside .form-floating. The control and its label take the disabled form colors, and the browser removes the control from the tab order.

HTML
<div class="form-floating mb-md">
  <label for="floatingInputDisabled">Email address</label>
  <input type="email" class="form-input" id="floatingInputDisabled" placeholder="name@example.com" disabled>
</div>
<div class="form-floating mb-md">
  <label for="floatingTextareaDisabled">Comments</label>
  <textarea class="form-input" placeholder="Share your thoughts here" id="floatingTextareaDisabled" disabled></textarea>
</div>
<div class="form-floating mb-md">
  <label for="floatingTextarea2Disabled">Comments</label>
  <textarea class="form-input" placeholder="Share your thoughts here" id="floatingTextarea2Disabled" style="height: 100px" disabled>This textarea is disabled with existing content</textarea>
</div>
<div class="form-floating">
  <label for="floatingSelectDisabled">Preferences</label>
  <select class="form-input" id="floatingSelectDisabled" disabled>
    <option selected>Choose an option</option>
    <option value="1">Option One</option>
    <option value="2">Option Two</option>
    <option value="3">Option Three</option>
  </select>
</div>

Plaintext inputs

Combine the readonly attribute with the .plaintext class on .form-input to display a value as static text without changing the surrounding layout. Because .plaintext is part of the label's :has() selector, the label always renders in its floated position, even when the control is empty.

HTML
<div class="form-floating mb-md">
  <label for="floatingEmptyPlaintextInput">Empty input</label>
  <input type="email" readonly class="form-input plaintext" id="floatingEmptyPlaintextInput" placeholder="name@example.com">
</div>
<div class="form-floating mb-md">
  <label for="floatingPlaintextInput">Input with value</label>
  <input type="email" readonly class="form-input plaintext" id="floatingPlaintextInput" placeholder="name@example.com" value="user@chassis.css">
</div>

Layout options

Floating labels compose with .input-group and the grid system for more complex form layouts.

Input groups

Place a .form-floating container inside an .input-group, next to its .input-addon elements.

@
HTML
<div class="input-group mb-md">
  <span class="input-addon">@</span>
  <div class="form-floating">
    <label for="floatingInputGroup">Username</label>
    <input type="text" class="form-input" id="floatingInputGroup" placeholder="Username">
  </div>
</div>

Form feedback placement. Place .form-help and validation feedback elements outside the .input-group container, so they do not become items of the group.

@
Please choose a username.
HTML
<div>
  <div class="input-group">
    <span class="input-addon">@</span>
    <div class="form-floating">
      <label for="floatingInputGroupInvalid">Username</label>
      <input type="text" class="form-input is-invalid" id="floatingInputGroupInvalid" placeholder="Username" required>
    </div>
  </div>
  <div class="invalid-feedback">
    Please choose a username.
  </div>
</div>

Icons alongside floating labels use .input-addon elements, the same as any other input group.

HTML
<div class="input-group">
  <div class="form-floating">
    <label for="floatingInputGroupUsername">Username</label>
    <input type="text" class="form-input" id="floatingInputGroupUsername" placeholder="Username" required>
  </div>
  <div class="form-floating">
    <label for="floatingInputGroupPassword">Password</label>
    <input type="password" autocomplete="current-password" class="form-input" id="floatingInputGroupPassword" placeholder="Password" required>
  </div>
  <button type="button" class="input-addon" aria-label="Show password">
    <svg class="icon" aria-hidden="true"><use href="#eye-solid"></use></svg>
  </button>
</div>

See the input group page for the full set of addon types and layout options.

Grid layout

Place .form-floating containers in the items of a grid to set them side by side:

HTML
<div class="grid grid-cols-1 md:grid-cols-2 gap-md">
  <div>
    <div class="form-floating">
      <label for="floatingInputGrid">Email address</label>
      <input type="email" class="form-input" id="floatingInputGrid" placeholder="name@example.com" value="user@chassis.css">
    </div>
  </div>
  <div>
    <div class="form-floating">
      <label for="floatingSelectGrid">Preferences</label>
      <select class="form-input" id="floatingSelectGrid">
        <option selected>Choose an option</option>
        <option value="1">Option One</option>
        <option value="2">Option Two</option>
        <option value="3">Option Three</option>
      </select>
    </div>
  </div>
</div>

See the forms layout page for the full grid, horizontal, and inline form patterns.

CSS

The Floating label component can be customized at both runtime (via custom properties) and compile time (via Sass variables).

Custom properties

The Floating label component exposes CSS custom properties to control its appearance at runtime.

--height: var(--form-floating-height, #{$form-floating-height});
--inactive-padding-y: var(--form-floating-inactive-padding-y, #{$form-floating-inactive-padding-y});
--active-padding-t: var(--form-floating-active-padding-t, #{$form-floating-active-padding-t});
--active-padding-b: var(--form-floating-active-padding-b, #{$form-floating-active-padding-b});
--input-padding-x: var(--form-floating-input-padding-x, #{$form-floating-input-padding-x});
--border-bottom-width: var(--form-floating-border-width, #{$form-floating-border-width});
--border-radius-t: var(--form-floating-border-radius-t, #{$form-floating-border-radius-t});
--border-radius-b: var(--form-floating-border-radius-b, #{$form-floating-border-radius-b});
--label-height: var(--form-floating-label-height, #{$form-floating-label-height});
--label-padding-y: var(--form-floating-label-padding-y, #{$form-floating-label-padding-y});
--label-transform: var(--form-floating-label-transform, #{$form-floating-label-transform});
--transition: var(--form-floating-transition, #{$form-floating-transition});

The forms overview lists the custom properties that all form elements share.

Sass variables

The Floating label component uses Sass variables in scss/config/_defaults.scss to define its defaults; they are also exposed as CSS custom properties using the --cx- prefix for runtime override — $variable-name becomes --cx-variable-name. See the component anatomy page.

$form-floating-height:                      3.5rem;
$form-floating-input-padding-x:             $form-md-padding-x;
$form-floating-inactive-padding-y:          1rem;
$form-floating-active-padding-t:            1.5rem;
$form-floating-active-padding-b:            .5rem;
$form-floating-label-padding-y:             $form-floating-inactive-padding-y;
$form-floating-label-height:                1.5em;
$form-floating-label-transform:             scale(.85) translateY(-.5rem) translateX(.15rem);
$form-floating-transition:                  opacity .1s ease-in-out, transform .1s ease-in-out;

The forms overview lists the Sass variables that all form elements share.

Design tokens

Floating labels have no tokens of their own: they use the design tokens that all form elements share, listed in the forms overview.