|

Floating Labels

Create beautifully simple form labels that float over your input fields.

Example #

To allow floating labels with UX4G's textual form fields, wrap a pair of input class="form-control"> and label> components in .form-floating. Each <input> must have a placeholder because our technique for CSS-only floating labels employs the:placeholder-shown pseudo-element. Also keep in mind that the <input> must come first so that we can use a sibling selector, like.

<div class="form-floating mb-3">
  <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com"/>
  <label for="floatingInput">Email address</label>
</div>
<div class="form-floating">
  <input type="password" class="form-control" id="floatingPassword" placeholder="Password"/>
  <label for="floatingPassword">Password</label>
</div>
RESULT

When there's a value already defined, <label>s will automatically adjust to their floated position.

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

Form validation styles also work as expected.

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

Textareas #

By default, <textarea>s with .form-control will be the same height as <input>s.

RESULT

To set a custom height on your <textarea>, do not use the rows attribute. Instead, set an explicit height (either inline or via custom CSS).

RESULT

Selects #

The only other form type besides .form-control that supports floating labels is .form-selects. The same principles apply to them, except unlike inputs, they always display the label in its floated position. Multiple and size-based selects are not supported.

<div class="form-floating">
  <select class="form-select" id="floatingSelect" aria-label="Floating label select example">
    <option selected>Open this select menu</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
  <label for="floatingSelect">Works with selects</label>
</div>
RESULT

Readonly plaintext #

Also supported are floating labels. When switching from an editable <input> to a plaintext value without changing the page layout, form-control-plaintext can be useful.

RESULT

Input groups #

Floating labels also support .input-group.

<div class="input-group mb-3">
  <span class="input-group-text">@</span>
  <div class="form-floating">
    <input type="text" class="form-control" id="floatingInputGroup1" placeholder="Username"/>
    <label for="floatingInputGroup1">Username</label>
  </div>
</div>
RESULT
@

When using .input-group, .form-floating, and form validation together, the -feedback should be positioned inside the .input-group but outside the .form-floating. This necessitates the use of javascript to display the feedback.

<div class="input-group has-validation">
  <span class="input-group-text">@</span>
  <div class="form-floating is-invalid">
    <input type="text" class="form-control is-invalid" id="floatingInputGroup2" placeholder="Username" required/>
    <label for="floatingInputGroup2">Username</label>
  </div>
  <div class="invalid-feedback">
    Please choose a username.
  </div>
</div>
RESULT
@
Please choose a username.

Layout #

When working with the UX4G grid system, be sure to place form elements within column classes.

<div class="row g-2">
  <div class="col-md">
    <div class="form-floating">
      <input type="email" class="form-control" id="floatingInputGrid" placeholder="name@example.com" value="mdo@example.com"/>
      <label for="floatingInputGrid">Email address</label>
    </div>
  </div>
  <div class="col-md">
    <div class="form-floating">
      <select class="form-select" id="floatingSelectGrid">
        <option selected>Open this select menu</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
      <label for="floatingSelectGrid">Works with selects</label>
    </div>
  </div>
</div>
RESULT

Sass #

Variables #

$form-floating-height:      add(3.5rem, $input-height-border);
$form-floating-line-height:       1.25;
$form-floating-padding-x:         $input-padding-x;
$form-floating-padding-y:         1rem;
$form-floating-input-padding-t:   1.625rem;
$form-floating-input-padding-b:   .625rem;
$form-floating-label-opacity:     .65;
$form-floating-label-transform:   scale(.85) translateY(-.5rem) translateX(.15rem);
$form-floating-transition:        opacity .1s ease-in-out, transform .1s ease-in-out;