Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 58 ยท Forms and Buttons

Buttons

Style buttons with btn and the color variants, outline buttons, sizes, disabled and active states, button groups, toolbars, link buttons and toggle buttons.

Buttons are where users act, so they deserve care. Bootstrap's button classes give you a consistent set of colors, sizes and states that already handle hover, focus and disabled styling. You add a base class, btn, plus a variant class, and the browser's plain gray button becomes a polished control.

This lesson covers the variants, outline buttons, sizes, disabled and active states, button groups and toolbars, using links as buttons, and toggle buttons. It continues from the utilities chapter; if spacing classes such as me-2 look unfamiliar, review Spacing Utilities.

The btn classes

Every Bootstrap button has two classes: btn for the shared shape (padding, border, radius, focus ring) and a variant for the colors.

Variant Typical meaning
btn-primary the main action
btn-secondary a quieter alternative
btn-success confirm, save, positive
btn-danger delete, destructive
btn-warning caution
btn-info informational
btn-light, btn-dark neutral light or dark
btn-link looks like a link, behaves like a button

Choose the color by meaning, not by taste. A page should normally have one clear primary button; everything else steps back.

Button variants

The second row uses outline buttons: btn-outline-* swaps the solid fill for a colored border and text, which fills in on hover. Use them for secondary actions next to a solid primary.

Always give a <button> a type. Inside a form, a button without one acts as a submit button; type="button" makes it do nothing until your script handles it.

Sizes

The default size suits most pages. Add btn-lg for a larger button (hero sections, calls to action) or btn-sm for a compact one (tables, toolbars). To make a button fill its container, add w-100, or put buttons in a d-grid wrapper so each stretches full width with a gap.

Button sizes

Disabled and active states

A disabled button is dimmed and does not react to the mouse. For a <button>, use the real disabled attribute: the browser then also blocks clicks and removes it from keyboard focus.

An active button looks pressed. Bootstrap adds the look when you put the class active on it, and it also appears automatically while the mouse is held down. If a button represents an on/off state, add aria-pressed="true" so assistive technology knows.

Links need extra care. An <a> has no disabled attribute, so use the class disabled plus aria-disabled="true", and add tabindex="-1" so keyboard users cannot reach it. The class only removes pointer events and dims the link; it does not stop keyboard activation by itself, which is why tabindex is needed.

Disabled and active

Use the right element for the job. A button performs an action on the current page (open a dialog, submit a form). A link goes somewhere. Bootstrap lets you style either one with btn, so a "Read more" link can look like a button while remaining an <a href>.

HTML
<a class="btn btn-primary" href="/bootstrap/cards" role="button">Go to cards</a>

Adding role="button" is only right when the link really acts like a button (for example with a JavaScript handler and href="#"). For plain navigation, leave the role off.

Button groups and toolbars

btn-group joins buttons into one segmented control with shared borders. Give the wrapper a role="group" and an aria-label so screen readers describe it. Use btn-group-vertical to stack them. Size the whole group with btn-group-lg or btn-group-sm.

A toolbar (btn-toolbar) holds several groups with space between them. Use margin classes such as me-2 on the groups.

Groups and a toolbar
flowchart TD
  T["btn-toolbar<br/>role=toolbar"] --> G1["btn-group<br/>History"]
  T --> G2["btn-group<br/>Format"]
  G1 --> B1["Undo"]
  G1 --> B2["Redo"]
  G2 --> B3["Bold"]
  G2 --> B4["Italic"]

A toolbar contains groups, and each group contains buttons. Labels on toolbar and groups tell screen readers what they are.

Toggle buttons

A toggle button switches between two states. There are two approaches.

With JavaScript: add data-bs-toggle="button" and Bootstrap flips the active class and aria-pressed on each click. This needs the Bootstrap bundle script, which the next example loads.

With a checkbox: hide a checkbox with the class btn-check and put a label styled as a button next to it. The checked state styles the label, and it works even without JavaScript. The next lesson shows the same trick for radios in Selects, Checks, Switches and Ranges.

Toggle buttons

Close buttons. Bootstrap also ships a ready-made close icon: <button type="button" class="btn-close" aria-label="Close"></button>. You will see it in alerts, modals and toasts later.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which pair of classes makes a standard blue main-action button?

    btn gives the shared shape and btn-primary the color. Without btn the variant class does nothing useful.
  2. 2. How should you disable a link that is styled as a button?

    Links have no disabled attribute, so Bootstrap uses a class; the ARIA attribute and tabindex handle assistive technology and keyboard users.
  3. 3. What does btn-outline-danger look like at rest?

    Outline variants swap the fill for a colored border and text, and fill in on hover.
  4. 4. Why give a button group role="group" and an aria-label?

    The role and label describe the set of related buttons to assistive technology.
  5. 5. Which is a no-JavaScript way to build a toggle button?

    The checked state of the hidden checkbox styles the label. The data-bs-toggle attribute needs the Bootstrap script.

Summary

  • A button needs btn plus a variant (btn-primary, btn-outline-secondary, and so on); choose by meaning.
  • Sizes: btn-lg, btn-sm, w-100 or d-grid for full width. Always set type on <button>.
  • Disable a <button> with disabled; disable a link with .disabled, aria-disabled and tabindex="-1".
  • btn-group, btn-group-vertical and btn-toolbar join buttons; label them for accessibility.
  • Toggle with data-bs-toggle="button" or a btn-check checkbox.

Next lesson: Form Controls.