Bs5 Floating Labels in Bootstrap
Absolutely! Here's a concise guide to Bootstrap 5 Floating Labels — a modern way to show labels inside input fields that float above the field when you type or focus.
? What are Floating Labels?
Floating labels combine inputs and labels so the label starts inside the input as a placeholder, then smoothly moves above the input when focused or filled. It improves UI clarity and aesthetics.
? Basic Floating Label Example
<form> <div class="form-floating mb-3"> <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com"> <label for="floatingInput">Email address</label> </div> <div class="form-floating"> <input type="password" class="form-control" id="floatingPassword" placeholder="Password"> <label for="floatingPassword">Password</label> </div></form>The
placeholderattribute must be present but can be an empty string if you prefer.The label is placed after the input.
? Key Points
Wrap the input and label inside a
.form-floatingcontainer.Use standard input types (
text,email,password, etc.).Placeholder text is necessary for the floating effect, even if empty.
Works with
textareaandselectas well.
? Floating Labels with Textarea
<div class="form-floating"> <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"></textarea> <label for="floatingTextarea">Comments</label></div>? Floating Labels with Select
<div class="form-floating"> <select class="form-select" id="floatingSelect" aria-label="Floating label select example"> <option selected>Open this select menu</option> <option value="1">One</option> <option value="2">Two</option> </select> <label for="floatingSelect">Works with select</label></div>?? Important Notes
Avoid using
.form-control-lgor.form-control-smwith floating labels because of sizing issues; custom styling might be required.Floating labels require Bootstrap 5 CSS and JavaScript for proper transitions.
Would you like a full form example with floating labels, or tips on customizing the floating label styles?