Bootstrap 5 Floating Labels
Floating labels sit inside the input like a placeholder, then shrink and "float" to the top edge when the field is focused or filled. They save vertical space and keep the label visible while the user types — unlike a plain placeholder, which disappears.
Basic floating labels
Wrap an input and its label in .form-floating. Two rules matter:
- The
<input>must come before the<label>(the CSS uses the sibling selector). - The input needs a
placeholderattribute — any text, it's never shown — because Bootstrap uses:placeholder-shownto detect whether the field is empty.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="form-floating mb-3">
<input type="email" class="form-control" id="fEmail" placeholder="name@example.com">
<label for="fEmail">Email address</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="fPw" placeholder="Password">
<label for="fPw">Password</label>
</div>
<div class="form-floating">
<input type="text" class="form-control" id="fName" placeholder="Name" value="Ana Silva">
<label for="fName">Name (pre-filled)</label>
</div>
</body>
</html>Click into the empty fields and watch the labels move up. The pre-filled one starts with its label already floated.
Textareas and selects
Textareas work the same way, with one catch: .form-floating gives every control the same fixed height as an input, so a textarea ignores rows — set its height with an inline style instead. Selects always show their label in the floated position (a select is never "empty").
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="form-floating mb-3">
<textarea class="form-control" placeholder="Leave a comment" id="comment" style="height: 90px"></textarea>
<label for="comment">Comment</label>
</div>
<div class="form-floating mb-3">
<select class="form-select" id="topic">
<option selected>General question</option>
<option>Bug report</option>
<option>Feature request</option>
</select>
<label for="topic">Topic</label>
</div>
<div class="form-floating">
<input type="text" readonly class="form-control-plaintext" id="ticket" placeholder="x" value="#TCK-2041">
<label for="ticket">Ticket number (plaintext)</label>
</div>
</body>
</html>Floating labels in layouts
Floating fields fit into the grid and into input groups. In an input group, put the .form-floating wrapper inside .input-group.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<form class="row g-2" id="ship">
<div class="col-md">
<div class="form-floating">
<input class="form-control" id="first" placeholder="First">
<label for="first">First name</label>
</div>
</div>
<div class="col-md">
<div class="form-floating">
<input class="form-control" id="last" placeholder="Last">
<label for="last">Last name</label>
</div>
</div>
<div class="col-12">
<div class="input-group">
<span class="input-group-text">🔗</span>
<div class="form-floating">
<input class="form-control" id="site" placeholder="Website">
<label for="site">Website</label>
</div>
</div>
</div>
</form>
</body>
</html>A floating-label login card
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3 bg-body-tertiary">
<form class="card mx-auto shadow-sm" style="max-width: 320px;" id="login">
<div class="card-body">
<h5 class="card-title text-center mb-3">🔐 Sign in</h5>
<div class="form-floating mb-2">
<input type="email" class="form-control" id="lEmail" placeholder="e">
<label for="lEmail">Email</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="lPw" placeholder="p">
<label for="lPw">Password</label>
</div>
<button class="btn btn-primary w-100" type="submit">Sign in</button>
</div>
</form>
<script>
document.getElementById('login').addEventListener('submit', (e) => {
e.preventDefault();
console.log('signing in', document.getElementById('lEmail').value);
});
</script>
</body>
</html>Reference
| Item | Rule |
|---|---|
.form-floating |
Wrapper around one control + label |
| Order | Control first, then <label> |
placeholder |
Required on inputs/textareas (text isn't shown) |
| Textarea height | Set with style="height: …" — rows is ignored |
.form-select |
Supported; label is always floated |
.form-control-plaintext |
Supported for read-only values |
Tip: Floating labels look great in short forms (login, newsletter). For long forms with help texts and many fields, classic labels above the inputs are often easier to scan.
Test yourself
In a .form-floating wrapper, which element must come first?
- The label
- The input
- Either order works
- A span
Why must the input have a placeholder attribute?
- It's displayed under the label
- Bootstrap uses :placeholder-shown to know if the field is empty
- It sets the label text
- For validation
How do you set the height of a floating textarea?
- With the rows attribute
- With an inline height style
- With .form-control-lg
- It always auto-grows