Bootstrap 5 Forms
Browsers style form controls differently, and the defaults are rather plain. Bootstrap gives inputs, textareas and labels a consistent look with a handful of classes, and its layout tools make building multi-column forms straightforward.
Basic form controls
| Class | Used on |
|---|---|
.form-label |
<label> above a control |
.form-control |
<input> (text, email, password, number, date, file…) and <textarea> |
.form-text |
Small help text under a control |
.form-control-lg / .form-control-sm |
Larger / smaller controls |
.form-control-plaintext |
Read-only value styled as plain text |
.form-control-color |
<input type="color"> |
<!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 id="signup">
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="name@example.com">
<div id="emailHelp" class="form-text">We'll never share your email.</div>
</div>
<div class="mb-3">
<label for="pw" class="form-label">Password</label>
<input type="password" class="form-control" id="pw">
</div>
<div class="mb-3">
<label for="bio" class="form-label">About you</label>
<textarea class="form-control" id="bio" rows="2"></textarea>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
<script>
document.getElementById('signup').addEventListener('submit', (e) => {
e.preventDefault();
console.log('email:', document.getElementById('email').value);
});
</script>
</body>
</html>Always connect labels to their inputs with for / id — clicking the label then focuses the field, and screen readers announce it.
Sizes, disabled and read-only
<!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">
<input class="form-control form-control-lg mb-2" placeholder=".form-control-lg">
<input class="form-control mb-2" placeholder="Default">
<input class="form-control form-control-sm mb-2" placeholder=".form-control-sm">
<input class="form-control mb-2" placeholder="Disabled" disabled>
<input class="form-control mb-2" value="Read-only (can be selected and copied)" readonly>
<div class="d-flex align-items-center gap-3">
<input type="color" class="form-control form-control-color" value="#7952b3" title="Pick a colour">
<input type="file" class="form-control form-control-sm">
</div>
</body>
</html>Form layout with the grid
Forms use the same grid as the rest of the page. Wrap fields in .row and .col-* to place them side by side; g-3 adds gutters between them.
<!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">
<div class="col-md-6">
<label for="first" class="form-label">First name</label>
<input class="form-control" id="first">
</div>
<div class="col-md-6">
<label for="last" class="form-label">Last name</label>
<input class="form-control" id="last">
</div>
<div class="col-8">
<label for="city" class="form-label">City</label>
<input class="form-control" id="city">
</div>
<div class="col-4">
<label for="zip" class="form-label">ZIP</label>
<input class="form-control" id="zip">
</div>
<div class="col-12"><button class="btn btn-primary" type="button">Save</button></div>
</form>
</body>
</html>Horizontal and inline forms
For a horizontal form (label beside the input), make each field a .row and give the label .col-form-label plus a column class. For a compact inline form, use .row with .col-auto columns.
<!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>
<div class="row mb-2">
<label for="hEmail" class="col-3 col-form-label">Email</label>
<div class="col-9"><input type="email" class="form-control" id="hEmail"></div>
</div>
<div class="row mb-3">
<label for="hUser" class="col-3 col-form-label">User</label>
<div class="col-9"><input class="form-control-plaintext" id="hUser" value="ana_silva" readonly></div>
</div>
</form>
<form class="row g-2 align-items-center">
<div class="col-auto"><label for="q" class="visually-hidden">Search</label><input class="form-control" id="q" placeholder="Search…"></div>
<div class="col-auto"><button type="button" class="btn btn-dark">Go</button></div>
</form>
</body>
</html>Tip: Group related fields in a
<fieldset>with a<legend>(Bootstrap styles it like a label with.col-form-labelor.form-label). Adddisabledto the fieldset to disable every control inside at once.
The next lessons cover selects, checkboxes and radios, ranges, input groups, floating labels and validation.
Test yourself
Which class styles text inputs and textareas?
- .form-input
- .form-control
- .input
- .form-field
What does .form-text do?
- Makes an input text-only
- Styles small help text below a control
- Turns an input into plain text
- Sets the font of labels
Which class vertically aligns a label with its input in a horizontal form?
- .form-label
- .col-form-label
- .align-middle
- .label-inline