Bootstrap, the popular CSS framework, has evolved significantly with its fifth iteration, Bootstrap 5. One of the key components of any web application is user authentication, and Bootstrap 5 provides robust tools to create responsive and visually appealing login forms. Let's explore a comprehensive example of a Bootstrap 5 login form.

Before diving into the code, it's essential to understand that Bootstrap 5 uses a different approach to forms compared to its predecessors. It now relies on custom form validation and input groups, making the process more flexible and customizable.

Setting Up the Bootstrap 5 Environment
To get started with Bootstrap 5, you'll first need to include the necessary CSS and JavaScript files in your project. You can either download the files or use a CDN. For this example, we'll use the CDN version:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
Creating the Basic Login Form Structure

Now that we have the Bootstrap environment set up, let's create the basic structure of our login form. We'll use Bootstrap's grid system and form controls to build a responsive layout:
<div class="container my-5">
<div class="row justify-content-center">
<div class="col-md-6">
<form>
<h2 class="text-center mb-4">Login</h2>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" placeholder="name@example.com">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password">
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
</div>
</div>
</div>
Adding Custom Form Validation

Bootstrap 5 introduces a new way of handling form validation using JavaScript. We'll use the `form-check` class to add validation to our form:
<script>
(function () {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
const forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.from(forms).forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
</script>
Now, add the `needs-validation` class to our form and update the input fields with the appropriate validation attributes:

<form class="needs-validation" novalidate><input type="email" class="form-control" id="email" placeholder="name@example.com" required><input type="password" class="form-control" id="password" required>
Styling the Login Form




















To make our login form more visually appealing, we can add some custom styles and use Bootstrap's utility classes:
.container { max-width: 720px; }.form-label { font-weight: 500; }.btn-primary { font-size: 1rem; }.was-validated .form-control:invalid { border-color: #dc3545; }.was-validated .form-control:valid { border-color: #198754; }
Adding a Forgot Password Link
Let's add a forgotten password link to our form using Bootstrap's inline text elements:
<div class="d-flex justify-content-between align-items-center mb-3">
<a href="#" class="text-decoration-none text-muted">Forgot password?</a>
<a href="#" class="text-decoration-none text-muted">Create account</a>
</div>
With these steps, you now have a fully functional and visually appealing login form using Bootstrap 5. You can further customize the form by adding more input fields, using different form controls, or incorporating custom JavaScript functionality.
As the web development landscape continues to evolve, so does Bootstrap. By keeping up with the latest updates and best practices, you can ensure that your login forms remain responsive, accessible, and visually appealing across various devices and platforms.