How do I align form controls in a grid with variable label widths?
Asked on Jul 13, 2025
1 Answer
To align form controls in a grid with variable label widths using Bootstrap 5, you can utilize the grid system along with form classes to ensure proper alignment and responsiveness.
<!-- BEGIN COPY / PASTE -->
<div class="container">
<form>
<div class="row mb-3">
<label for="inputEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail">
</div>
</div>
<div class="row mb-3">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword">
</div>
</div>
<div class="row mb-3">
<label for="inputAddress" class="col-sm-2 col-form-label">Address</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputAddress">
</div>
</div>
</form>
</div>
<!-- END COPY / PASTE -->
Additional Comment:
✅ Answered with Bootstrap 5 best practices.- Use Bootstrap's grid system by wrapping each form group in a `.row` and using `.col-sm-*` classes to define the width of labels and inputs.
- The `.col-form-label` class ensures that labels are aligned properly with form controls.
- Adjust the column sizes (`col-sm-2`, `col-sm-10`) as needed to fit your design requirements.
- The `.mb-3` class adds spacing between form groups for better visual separation.