Form Layouts and Validation
Bootstrap forms look good with form-control, form-select and form-check. Use the grid for layouts with several boxes on one line.
Clear forms reduce mistakes and abandoned sign-ups. Good layout groups related fields, and good validation tells users exactly what to fix, next to the field, in plain language. Bootstrap styles both the layout and the messages, building on the browser's own validation.
Floating labels and input groups
Example
<!DOCTYPE html>
<html lang="en">
<head>
<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" style="max-width: 360px">
<input type="email" class="form-control" id="em" placeholder="you@example.com">
<label for="em">Email address</label>
</div>
<div class="input-group mb-3" style="max-width: 360px">
<span class="input-group-text">₹</span>
<input type="number" class="form-control" placeholder="Max price" aria-label="Max price">
<button class="btn btn-outline-secondary">Apply</button>
</div>
<div class="input-group" style="max-width: 360px">
<span class="input-group-text">+91</span>
<input type="tel" class="form-control" placeholder="Mobile number" aria-label="Mobile number">
</div>
</body>
</html>▶ Try it yourselfValidation styles
Add novalidate to the form and the class was-validated after the user presses submit. Bootstrap then shows green and red borders, and the messages in .invalid-feedback.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<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-3 needs-validation" novalidate style="max-width: 520px">
<div class="col-md-6">
<label for="name" class="form-label">Full name</label>
<input id="name" class="form-control" required>
<div class="invalid-feedback">Please enter your name.</div>
</div>
<div class="col-md-6">
<label for="pin" class="form-label">PIN code</label>
<input id="pin" class="form-control" pattern="[1-9][0-9]{5}" required>
<div class="invalid-feedback">Enter a 6-digit PIN code.</div>
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="terms" required>
<label class="form-check-label" for="terms">I agree to the terms</label>
<div class="invalid-feedback">You must agree before ordering.</div>
</div>
</div>
<div class="col-12"><button class="btn btn-primary">Place order</button></div>
</form>
<script>
document.querySelectorAll(".needs-validation").forEach(form => {
form.addEventListener("submit", e => {
if (!form.checkValidity()) { e.preventDefault(); e.stopPropagation(); }
form.classList.add("was-validated");
});
});
</script>
</body>
</html>▶ Try it yourselfis-valid and is-invalid classes let you mark one box yourself, for example after checking with your server.Key points
- Use form-control, form-select and form-check for consistent fields.
- Floating labels and input groups save space.
- novalidate + was-validated shows Bootstrap's validation styles.
📝 Test yourself
1. Which class goes on a form to show Bootstrap's validation colours?
2. Where do you put the error text for a box?
3. Fill in the class for a box with a ₹ sign joined to it:
<div class="input-">...</div>
