Forms
Floating labels
Labels that float over the field, driven by :placeholder-shown — no script.
How it works
Wrap a pair of <input class="form-control"> and <label> elements in
.form-floating. The label starts inside the field; when the field has focus or has a value, it
shrinks and floats to the top. Bootstrap does this with the :placeholder-shown pseudo-class,
so it has never needed JavaScript. The theme adds nothing — the colours are the ordinary
form control colours.
- A
placeholderattribute is required, even if it is just a space or the label text. Without it:placeholder-shownnever matches and the label never floats. - The
<input>must come before the<label>; the CSS uses the sibling combinator. - Floated labels render at 85 % scale with the field text staying full size.
Example
HTML
<div class="form-floating mb-4">
<input type="email" class="form-control" id="floating-email" placeholder="[email protected]" />
<label for="floating-email">Email address</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" id="floating-password" placeholder="Password" />
<label for="floating-password">Password</label>
</div>When there's a value already defined, <label>s will automatically adjust to their floated position.
HTML
<div class="form-floating">
<input type="email" class="form-control" id="floating-email-value" placeholder="[email protected]" value="[email protected]" />
<label for="floating-email-value">Input with value</label>
</div>Form validation styles also work as expected. Here the field has .is-invalid set server-side; a live field would colour itself through :user-invalid.
HTML
<div class="form-floating">
<input type="email" class="form-control is-invalid" id="floating-email-invalid" placeholder="[email protected]" value="not-an-email" />
<label for="floating-email-invalid">Invalid input</label>
</div>Textareas
By default, <textarea>s with .form-control will be the same height as
<input>s.
HTML
<div class="form-floating">
<textarea class="form-control" placeholder="Cut a promo here" id="floating-textarea"></textarea>
<label for="floating-textarea">Promo</label>
</div>
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). A floated label on a
textarea gets a cream backing (::after) so text scrolling under it stays legible.
HTML
<div class="form-floating">
<textarea class="form-control" placeholder="Cut a promo here" id="floating-textarea-tall" style="height: 100px"></textarea>
<label for="floating-textarea-tall">Promo</label>
</div>Selects
Other than .form-control, floating labels are only available on .form-selects.
They work in the same way, but unlike <input>s, they'll always show the
<label> in its floated state. Selects with size and
multiple are not supported.
HTML
<div class="form-floating">
<select class="form-select" id="floating-select" aria-label="Floating label select example">
<option selected>Open this select menu</option>
<option value="1">Singles</option>
<option value="2">Tag team</option>
<option value="3">Steel cage</option>
</select>
<label for="floating-select">Works with selects</label>
</div>Disabled
Add the disabled boolean attribute on an input, a textarea or a select to give it a greyed-out appearance, remove pointer events, and prevent focusing. The label dims to match.
HTML
<div class="form-floating mb-4">
<input type="email" class="form-control" id="floating-disabled" placeholder="[email protected]" disabled />
<label for="floating-disabled">Email address</label>
</div>
<div class="form-floating mb-4">
<textarea class="form-control" placeholder="Cut a promo here" id="floating-disabled-textarea" disabled></textarea>
<label for="floating-disabled-textarea">Promo</label>
</div>
<div class="form-floating mb-4">
<textarea class="form-control" placeholder="Cut a promo here" id="floating-disabled-textarea-tall" style="height: 100px" disabled>Disabled textarea with some text</textarea>
<label for="floating-disabled-textarea-tall">Promo</label>
</div>
<div class="form-floating">
<select class="form-select" id="floating-disabled-select" aria-label="Floating label disabled select example" disabled>
<option selected>Open this select menu</option>
<option value="1">Singles</option>
<option value="2">Tag team</option>
<option value="3">Steel cage</option>
</select>
<label for="floating-disabled-select">Works with selects</label>
</div>Readonly plaintext
Floating labels also support .form-control-plaintext, which can be helpful for toggling from an
editable <input> to a plaintext value without affecting the page layout.
HTML
<div class="form-floating mb-4">
<input type="email" readonly class="form-control-plaintext" id="floating-plaintext-empty" placeholder="[email protected]" />
<label for="floating-plaintext-empty">Empty input</label>
</div>
<div class="form-floating">
<input type="email" readonly class="form-control-plaintext" id="floating-plaintext" placeholder="[email protected]" value="[email protected]" />
<label for="floating-plaintext">Input with value</label>
</div>Input groups
Floating labels also support .input-group. The .form-floating wrapper is the flex
item, and the group's corner rules reach inside it.
HTML
<div class="input-group">
<span class="input-group-text">@</span>
<div class="form-floating">
<input type="text" class="form-control" id="floating-input-group-1" placeholder="Username" />
<label for="floating-input-group-1">Username</label>
</div>
</div>
When using .input-group and .form-floating along with form validation, the
-feedback should be placed outside of the .form-floating, but inside of the
.input-group. Add .has-validation to the group so the feedback wraps below.
HTML
<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="floating-input-group-2" placeholder="Username" required />
<label for="floating-input-group-2">Username</label>
</div>
<div class="invalid-feedback">Please choose a username.</div>
</div>Layout
When working with the Bootstrap grid system, be sure to place form elements within column classes. Use
.g-2 (.5rem) for a tight gap or .g-4 for the usual 1 rem.
HTML
<div class="row g-2">
<div class="col-md">
<div class="form-floating">
<input type="email" class="form-control" id="floating-layout-email" placeholder="[email protected]" value="[email protected]" />
<label for="floating-layout-email">Email address</label>
</div>
</div>
<div class="col-md">
<div class="form-floating">
<select class="form-select" id="floating-layout-select" aria-label="Floating label select example">
<option selected>Open this select menu</option>
<option value="1">Singles</option>
<option value="2">Tag team</option>
<option value="3">Steel cage</option>
</select>
<label for="floating-layout-select">Works with selects</label>
</div>
</div>
</div>No JavaScript
This is the one Bootstrap form feature people assume is scripted. It is not. The whole mechanism is:
.form-floating > .form-control:focus ~ label,
.form-floating > .form-control:not(:placeholder-shown) ~ label,
.form-floating > .form-control-plaintext ~ label,
.form-floating > .form-select ~ label {
transform: scale(.85) translateY(-.5rem) translateX(.15rem);
}
Autofilled fields float the label too (:-webkit-autofill). The transition is removed under
prefers-reduced-motion.
Sass variables
The theme does not override any $form-floating-* variable. Bootstrap's defaults:
$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-height: 1.5em;
$form-floating-label-opacity: .65;
$form-floating-label-transform: scale(.85) translateY(-.5rem) translateX(.15rem);
$form-floating-label-disabled-color: $gray-600; // navy-400 in this theme
$form-floating-transition: opacity .1s ease-in-out, transform .1s ease-in-out;