Bootstrap 5 introduced Floating Labels, providing a massively engaging, pure CSS animated alternative to traditional stacked labels. Often synonymous with Google's Material Design, these seamlessly scale down and float above the input value whenever the user types or focuses on the area!
Building floating labels completely flawlessly requires three exceptionally strict rules regarding syntax arrangement:
.form-floating utility class.<input> must come first. The <label> element must physically be placed after the input inside the HTML.<!-- Notice the Input is placed ABOVE the Label! -->
<div class="form-floating mb-3">
<input type="email" class="form-control" id="floatEmail" placeholder="name@example.com">
<label for="floatEmail">Email address</label>
</div>
Floating labels look equally fantastic on standard <textarea> components! Keep in mind, you cannot use the HTML rows="..." property natively inside a floating form because the Bootstrap heights forcefully override it natively to stabilize the label bounding box. If you want a taller textarea box, simply utilize raw CSS or a `style` attribute (e.g., `style="height: 150px"`).
<!-- Do not use 'rows', use CSS 'height' for sizing -->
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment" id="floatingTA" style="height: 150px"></textarea>
<label for="floatingTA">Comments</label>
</div>
Beyond traditional typing text boxes, Floating Labels remarkably support native <select> dropdown menus seamlessly too! The `
<!-- Standard Select Dropdown -->
<div class="form-floating">
<select class="form-select" id="floatSelect">
<option selected>Choose Your State</option>
<option value="1">Ohio</option>
<option value="2">Maine</option>
</select>
<label for="floatSelect">State Residence</label>
</div>