Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 58 ยท Forms and Buttons

Input Groups and Floating Labels

Attach text, icons and buttons to inputs with input-group, size and combine groups, and build modern floating-label fields with form-floating in Bootstrap 5.

Some fields need company. A price field wants a currency symbol in front, a username field wants an "@", a search box wants a button on its right edge. Bootstrap's input group attaches such add-ons to a control so that they share borders and look like one piece. The other popular pattern, the floating label, saves vertical space: the label sits inside the field like a placeholder, then slides up when you type.

This lesson covers both. It assumes you know the basic field markup from Form Controls and the buttons from Buttons.

Input groups

Wrap the controls in <div class="input-group">. Inside, put your input with form-control and add-ons next to it. A text add-on uses <span class="input-group-text">; a button add-on is simply a btn. The add-ons can be before the input, after it, or on both sides.

Add-ons before and after

A few details matter:

  • The add-on text is not a label. Screen readers may skip it, so each input still needs a name: an aria-label, a <label>, or an aria-describedby pointing at the add-on (as the first field does).
  • Inputs in a group are not 100% wide on their own; the group is a flex container and form-control grows (flex: 1 1 auto) to take the remaining space.
  • Put the input-group on the outer element and keep mb-3 on it, not on the inner input.

Checkboxes, radios and selects in a group

An add-on is not limited to text. Put a checkbox or radio inside an input-group-text to attach an "enable this field" option to an input. You can also place a form-select or a form-control of type file in a group, so a select can sit next to a text field or a file picker can have a button.

Several inputs in one group

A group can hold more than one input. Each gets form-control, and the borders join. Use this for ranges, names or date parts.

Multiple inputs and a checkbox add-on

In the last group the add-on is a real <label> tied to the select, which is better than a bare <span> when the text names the field.

Sizing a group

Put input-group-lg or input-group-sm on the group itself (not on each child). Everything inside, the inputs, the add-ons and the buttons, scales together. Individual form-control-lg classes should not be mixed in; the group already handles it.

One size per group. Mixing a small input with a large button inside the same group makes the control look broken, because the heights do not match. Use the single group-level class.

Floating labels

For a floating label, wrap an input and its label in <div class="form-floating">. The order is important: the <input> comes first, then the <label>, so CSS can use the sibling selector to move the label when the field is focused or filled. The input also needs a placeholder attribute, even if it is a single space or text you never see, because the effect depends on :placeholder-shown.

Floating label fields

Notes on the markup above:

  • A <textarea> ignores rows in a floating field. Give it a height with a style or a utility such as h-auto; here an inline style="height: 100px" does it.
  • A <select> shows its label already floated, because a select always has a value. Its <label> also comes after the control.
  • Floating fields are slightly taller than normal ones, so use them where there is room.
  • Because the label replaces the placeholder text, you still have an accessible name without extra work. That is one accessibility advantage over placeholder-only fields.

Floating labels inside input groups

A floating field is allowed in an input group, but the form-floating wrapper becomes the flexible child of the group:

HTML
<div class="input-group">
  <span class="input-group-text">@</span>
  <div class="form-floating">
    <input type="text" class="form-control" id="handle" placeholder="Username">
    <label for="handle">Username</label>
  </div>
</div>

This keeps the add-on and the floating field the same height. Validation feedback inside input groups needs the helper class has-validation, which comes up in Form Layout and Validation.

flowchart TD
  FG["div.form-floating"] --> IN["input.form-control<br/>placeholder required"]
  FG --> LB["label for=input id<br/>comes AFTER the input"]
  IN -. "empty and unfocused" .-> S1["label sits inside the box"]
  IN -. "focused or filled" .-> S2["label floats above the value"]

Why order matters: the label is styled by its relationship to the input before it and the input's placeholder state.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which element holds the add-on text in an input group?

    input-group-text is the class for text, icon, checkbox or radio add-ons. Buttons use plain btn classes.
  2. 2. Why does an input with only a $ add-on still need aria-label or a label?

    The currency symbol is decoration; a real name keeps the field understandable for screen reader users.
  3. 3. In a form-floating field, what is the correct child order?

    The CSS reaches the label with a sibling selector, so it has to come after the control.
  4. 4. How should you make an input group larger?

    The group-level class resizes the inputs, add-ons and buttons together.
  5. 5. Why does a form-floating input need a placeholder attribute?

    The placeholder text itself is hidden. Its presence lets CSS tell an empty field from a filled one.

Summary

  • input-group joins inputs with input-group-text add-ons, buttons, selects and more; give each control an accessible name.
  • Several form-control inputs can share one group; use input-group-lg or -sm on the group to resize it all.
  • form-floating puts the label inside the field; the input must come first and carry a placeholder.
  • Textareas in floating fields need an explicit height.
  • Validation inside input groups needs has-validation, explained with the other validation tools.

Next lesson: Form Layout and Validation.