Bootstrap 5 – Forms & Controls
Estimated reading: 4 minutes 11 views

🧷 Bootstrap 5 – Floating Labels: Stylish, Space-Saving Input Design

🧲 Introduction – What Are Floating Labels in Bootstrap 5?

Floating labels allow form labels to elegantly float above the input field when focused or filled—saving space and improving form clarity. In Bootstrap 5, this feature is built-in and easy to implement using the .form-floating class.

🎯 In this guide, you’ll learn:

  • How to create floating label inputs, textareas, and selects
  • Customize them with different input types
  • Combine with validation
  • Learn when to use (or avoid) floating labels

✅ Example 1 – Basic Floating Label Input

<div class="form-floating mb-3">
  <input type="email" class="form-control" id="floatingEmail" placeholder="name@example.com">
  <label for="floatingEmail">Email address</label>
</div>

🔍 Explanation:

Element/ClassPurpose
.form-floatingWraps the input + label for floating effect
.form-controlStyles the input field
placeholderRequired to trigger label float
<label for="...">Label floats when input is focused or filled

✅ Example 2 – Floating Password Input

<div class="form-floating mb-3">
  <input type="password" class="form-control" id="floatingPassword" placeholder="Password">
  <label for="floatingPassword">Password</label>
</div>
  • Works the same as email, but with type="password"

✅ Example 3 – Floating Textarea

<div class="form-floating mb-3">
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"></textarea>
  <label for="floatingTextarea">Comments</label>
</div>
  • Make sure to use placeholder even for textareas
  • Supports multi-line input

✅ Example 4 – Floating Select Dropdown

<div class="form-floating mb-3">
  <select class="form-select" id="floatingSelect" aria-label="Floating label select">
    <option selected>Open this select menu</option>
    <option value="1">One</option>
    <option value="2">Two</option>
  </select>
  <label for="floatingSelect">Select an option</label>
</div>
  • Use .form-select instead of .form-control for <select> elements

✅ Example 5 – Floating Inputs with Validation

<form class="needs-validation" novalidate>
  <div class="form-floating mb-3">
    <input type="text" class="form-control" id="floatingName" placeholder="Your Name" required>
    <label for="floatingName">Name</label>
    <div class="invalid-feedback">Name is required.</div>
  </div>
  <button class="btn btn-primary" type="submit">Submit</button>
</form>

<script>
  (() => {
    const forms = document.querySelectorAll('.needs-validation');
    forms.forEach(form => {
      form.addEventListener('submit', event => {
        if (!form.checkValidity()) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      });
    });
  })();
</script>
  • Adds native Bootstrap validation to floating inputs

🛠️ Best Practices for Floating Labels

TipWhy It Helps
Always include a placeholderRequired for proper label positioning
Avoid long label textMay overlap or truncate visually
Use form-select for dropdownsPrevents layout issues with native selects
Combine with .mb-3 or spacing classesAdds visual separation between fields
Don’t use with complex input groupsFloating labels don’t support input-group structure

📌 Summary – Modern, Clean Label Positioning with Bootstrap 5

Bootstrap 5 floating labels modernize form UIs by making labels float above fields—offering a sleek, minimal look. Great for login forms, registrations, settings panels, and mobile-first layouts.

🔍 Key Takeaways:

  • Wrap input + label in .form-floating
  • Works with input, textarea, and select (with .form-select)
  • Always include placeholder for activation
  • Compatible with Bootstrap validation features
  • Avoid using inside input groups or multi-column grids without testing

⚙️ Floating labels are perfect for saving space and simplifying dense forms.


❓ FAQs – Bootstrap 5 Floating Labels

Is placeholder required for floating labels?
✅ Yes. Even though the label floats, the placeholder is necessary to trigger layout behavior.


Can I use floating labels with checkboxes or radios?
❌ No. Floating labels are designed only for text-based inputs, selects, and textareas.


Are floating labels accessible?
✅ Yes—when using the label for and id attributes properly.


Can I style the floating label position or color?
✅ Yes. Use custom CSS targeting .form-floating label or .form-control:focus ~ label.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Bootstrap 5 – Form Control Types: Floating Labels

Or Copy Link

CONTENTS
Scroll to Top