Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 58 ยท Forms and Buttons

Form Controls

Style text inputs, textareas, file and color pickers with form-control, add labels and help text, and use readonly, plaintext and sizing classes in Bootstrap 5.

Browsers draw form fields differently, and the defaults are rarely attractive. Bootstrap normalizes them: one class, form-control, gives text inputs, textareas and file pickers the same border, padding, focus ring and width. Together with a label and an optional help line, you get a consistent field in three lines of HTML.

This lesson covers the basic field structure, textareas, file and color inputs, help text, readonly and plaintext fields, disabled fields and sizing. Selects, checkboxes and switches follow in Selects, Checks, Switches and Ranges. Buttons are in Buttons.

The basic field

A well-built field has three parts: a <label> linked to the input by for and id, the input itself with the class form-control, and spacing between fields. Bootstrap's convention is form-label on the label and mb-3 on a wrapper div.

HTML
<div class="mb-3">
  <label for="email" class="form-label">Email address</label>
  <input type="email" class="form-control" id="email" placeholder="[email protected]">
</div>

form-control makes the input display: block and 100% wide, so it fills its container. To narrow it, wrap the field in a grid column (see The Grid System) or give it a width utility.

Always keep the label. A placeholder is only a hint and disappears when the user types; it is not a replacement for a label, and many screen readers do not announce it reliably. The for attribute must match the input's id, because clicking the label then focuses the field.

Textarea and help text

A <textarea> uses the same class. Set the height with the rows attribute. For hints, use form-text under the field and link it to the input with aria-describedby so assistive technology reads it together with the label.

Text input, textarea and help text

The wrapper <form onsubmit="return false"> is there only because this is a sandboxed demo that must not post anywhere. In your own page the form would have an action or a script.

Use the right type. type="email", type="tel", type="number", type="password" and type="url" give users the right keyboard on phones and add free browser validation. form-control styles all of them the same way.

File and color inputs

A file input takes the same class. Bootstrap styles the native button and aligns the text with the other fields. Add multiple to allow several files.

A color input uses a different helper, form-control-color, in addition to form-control. It shrinks the field to a small swatch and gives it a title as a tooltip. Set a default with the value attribute.

File and color pickers

Sizing

Fields come in three sizes. The default fits most forms; form-control-lg and form-control-sm enlarge or shrink the input. Match the button next to it: a btn-lg button goes best with a form-control-lg field. The size classes work on text inputs, selects (form-select-lg, form-select-sm) and file inputs.

Disabled, readonly and plaintext

Three states look similar but mean different things:

State Attribute Behavior
Disabled disabled Dimmed, cannot be focused, not submitted with the form.
Readonly readonly Looks normal but cannot be edited; it can be focused and is submitted.
Plaintext form-control-plaintext + readonly Removes the box so the value looks like normal text.

Plaintext fields are useful for showing a saved value in a form layout, where the label and value line up with other editable rows. Remember that the markup stays an <input readonly>, so the value is still a real form value.

Sizes and states

Fields without a visible label, like the first five here, still need an accessible name; aria-label provides one. A visible label is better whenever there is room.

In the last two rows, col-form-label on the label aligns its text with the input's baseline inside a grid row. Horizontal layouts like this get their own lesson, Form Layout and Validation.

flowchart TD
  F["Field wrapper<br/>div.mb-3"] --> L["label.form-label<br/>for = input id"]
  F --> I["input.form-control<br/>id, type, aria-describedby"]
  F --> H["div.form-text<br/>hint, id referenced by aria-describedby"]

The three parts of a well-built field: label, control and optional help text, tied together with for/id and aria-describedby.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which class makes a text input fill its container and look consistent across browsers?

    form-control is the base class for text-like inputs, textareas and file inputs.
  2. 2. Why should you keep a label even when the input has a placeholder?

    A placeholder vanishes as soon as the user types and is not a reliable accessible name; a linked label solves both problems.
  3. 3. What is the difference between disabled and readonly?

    Disabled fields are skipped by submission and by the keyboard. Readonly fields behave normally except that they cannot be edited.
  4. 4. Which extra class turns an input of type color into a compact swatch?

    form-control-color is used together with form-control on <input type="color">.
  5. 5. How do you connect a help text to its input for screen readers?

    aria-describedby makes the help text read after the label when the field is focused.

Summary

  • A field is a form-label, a form-control input and optional form-text, wrapped in an mb-3 div.
  • Textareas use rows; file inputs take multiple; color inputs add form-control-color.
  • Keep labels linked with for/id; hints connect through aria-describedby.
  • form-control-lg and form-control-sm resize fields; disabled, readonly and form-control-plaintext change state and look.
  • Demo forms in this site must not post: the examples block submission.

Next lesson: Selects, Checks, Switches and Ranges.