Bootstrap Buttons

You will discover in this lesson how to use Bootstrap to design and alter buttons.

Creating Buttons with Bootstrap

Buttons are an essential component of websites and applications, serving various purposes such as submitting or resetting an HTML form, triggering interactive actions like showing or hiding elements on a web page, redirecting users to another page, and more. Bootstrap offers a simple and efficient way to create and customize buttons.

Bootstrap Button Styles

Bootstrap provides different classes for styling buttons and indicating different states or semantics. While button styles can be applied to any element, they are commonly used with <a>, <input>, and <button> elements for optimal rendering.

The following example demonstrates how to create various button styles in Bootstrap:

<div class="m-4">
    <button type="button" class="btn btn-primary">Primary</button>
    <button type="button" class="btn btn-secondary">Secondary</button>
    <button type="button" class="btn btn-success">Success</button>
    <button type="button" class="btn btn-danger">Danger</button>
    <button type="button" class="btn btn-warning">Warning</button>
    <button type="button" class="btn btn-info">Info</button>
    <button type="button" class="btn btn-dark">Dark</button>
    <button type="button" class="btn btn-light">Light</button>
    <button type="button" class="btn btn-link">Link</button>
</div>

Bootstrap Outline Buttons

Additionally, you can create outline buttons by replacing the button modifier classes, as shown below:

<div class="m-4">
    <button type="button" class="btn btn-outline-primary">Primary</button>
    <button type="button" class="btn btn-outline-secondary">Secondary</button>
</div>

Changing the Sizes of Buttons

Bootstrap also allows you to scale buttons up or down.

To make buttons larger, add the .btn-lg class

<div class="m-4">
    <button type="button" class="btn btn-primary btn-lg">Large button</button>
    <button type="button" class="btn btn-secondary btn-lg">Large button</button>
</div>

To make them smaller, add the .btn-sm class:

<div class="m-4">
    <button type="button" class="btn btn-primary btn-sm">Small button</button>
    <button type="button" class="btn btn-secondary btn-sm">Small button</button>
</div>

If you want to create full-width or block buttons that cover the entire width of their parent elements, you can use Bootstrap's display and gap utility classes. These utilities provide greater control over spacing, alignment, and responsive behavior.

<div class="m-4">
    <div class="d-grid gap-2">
        <button type="button" class="btn btn-primary">Block button</button>
        <button type="button" class="btn btn-secondary">Block button</button>
    </div>
</div>

You can also make these buttons responsive using the .d-{breakpoint}-block classes. In the following example, buttons will be vertically stacked on small and extra-small devices (viewport width <768px), and from the medium (md) breakpoint and up, the .d-md-block class replaces the .d-grid class, nullifying the .gap-2 class. Let's see how it works:

<div class="m-4">
    <div class="d-grid gap-2 d-md-block">
        <button type="button" class="btn btn-primary">Button</button>
        <button type="button" class="btn btn-secondary">Button</button>
    </div>
</div>

Bootstrap Disabled Buttons

Sometimes, you may need to disable a button for various reasons, such as when a user is not eligible to perform a particular action or when you want to ensure that a user completes all required actions before proceeding with a specific action.

Creating Disabled Buttons Using Button and Input Element

You can disable buttons created using <button> or <input> tags by adding the disabled attribute to the respective element, as demonstrated in the following example:

<div class="m-4">
    <button type="button" class="btn btn-primary" disabled>Primary button</button>
    <button type="button" class="btn btn-secondary" disabled>Secondary button</button>
</div>

Creating Disabled Buttons Using Anchor Elements

Buttons created using the <a> tag can be visually disabled by adding the class .disabled, as shown below:

<div class="m-4">
    <a href="#" class="btn btn-primary disabled">Primary link</a>
    <a href="#" class="btn btn-secondary disabled">Secondary link</a>
</div>

Note: Please note that using the .disabled class only gives the appearance of a disabled button for links. However, the link will still remain clickable unless you remove the href attribute from it. If you want to completely prevent clicks on a disabled link, you may need to implement custom JavaScript code.


Bootstrap Active Buttons

Additionally, you can apply the class .active to make buttons appear as active or pressed. Usually, there is no need to manually add this class, as Bootstrap automatically styles buttons in their active state using the CSS :active pseudo-class. Here's an example:

<div class="m-4">
    <button type="button" class="btn btn-primary active">Primary button</button>
    <button type="button" class="btn btn-secondary active">Secondary button</button>
</div>

Creating Spinner Buttons

Bootstrap provides a straightforward way to include a spinner icon in a button, indicating the loading state of your application.

<div class="m-4">
    <button type="button" class="btn btn-primary" disabled>
        <span class="spinner-border spinner-border-sm"></span>
    </button>
    <button type="button" class="btn btn-primary" disabled>
        <span class="spinner-border spinner-border-sm"></span> Loading...
    </button>
    <button type="button" class="btn btn-primary" disabled>
        <span class="spinner-grow spinner-grow-sm"></span> Loading...
    </button>
</div>

In the next chapter, you will learn how to combine multiple buttons horizontally or vertically in a line, creating toolbars using the Bootstrap button groups component. Moreover, in the advanced section, you will discover how to create toggle buttons without requiring any JavaScript code.


FAQ

What are Bootstrap buttons?

Bootstrap buttons are UI components designed to trigger actions or navigate to different parts of a web application when clicked or interacted with. They come in various styles, sizes, and colors, making them versatile for different design needs.

How do you create a basic Bootstrap button?

To create a basic Bootstrap button, you can use the following HTML structure:

<button class="btn btn-primary">Click Me</button>

In this example, the btn class is used to define a button, and btn-primary is used to apply the primary button style provided by Bootstrap.

How can you add different styles to Bootstrap buttons?

Bootstrap provides different contextual classes that can be used to apply various styles to buttons. Some common contextual classes include:

  • btn-primary: Provides a primary button with a blue color.
  • btn-secondary: Provides a secondary button with a gray color.
  • btn-success: Indicates a successful action with a green color.
  • btn-danger: Represents a potentially dangerous action with a red color.
  • btn-warning: Indicates a warning or caution with a yellow color.
  • btn-info: Provides informative content with a light blue color.
  • btn-light: Offers a light-colored button.
  • btn-dark: Provides a dark-colored button.
  • btn-link: Creates a button that looks like a link.

How can you create button sizes in Bootstrap?

Bootstrap buttons can be easily customized in terms of size using size classes. The available size classes are:

  • btn-lg: Large-sized button.
  • btn-md (default): Medium-sized button.
  • btn-sm: Small-sized button.
  • btn-xs: Extra small-sized button.

For example:

<button class="btn btn-primary btn-lg">Large Button</button>
<button class="btn btn-primary">Default Button</button>
<button class="btn btn-primary btn-sm">Small Button</button>

How can you create outline buttons in Bootstrap?

Outline buttons in Bootstrap have a transparent background with a colored border. You can create an outline button by using the btn-outline-* classes along with the contextual classes mentioned earlier. For instance:

<button class="btn btn-outline-primary">Outline Primary</button>
<button class="btn btn-outline-secondary">Outline Secondary</button>

How can you disable a Bootstrap button?

To disable a Bootstrap button, you can add the disabled attribute to the <button> element:

<button class="btn btn-primary" disabled>Disabled Button</button>

How do you create button groups in Bootstrap?

Button groups allow you to group multiple buttons together. You can create button groups using the .btn-group class. For example:

<div class="btn-group">
  <button class="btn btn-primary">Button 1</button>
  <button class="btn btn-primary">Button 2</button>
  <button class="btn btn-primary">Button 3</button>
</div>

How can you create button dropdowns in Bootstrap?

Button dropdowns combine a button with a dropdown menu. You can create them using the .btn-group class along with the .dropdown and .dropdown-menu classes. For example:

<div class="btn-group">
  <button class="btn btn-primary">Action</button>
  <button class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropdown</span>
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Option 1</a>
    <a class="dropdown-item" href="#">Option 2</a>
  </div>
</div>

How can you create button toolbars using Bootstrap?

Button toolbars are used to group button sets together. You can create a button toolbar using the .btn-toolbar class. For example:

<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
  <div class="btn-group me-2" role="group" aria-label="First group">
    <button class="btn btn-primary">Button 1</button>
    <button class="btn btn-primary">Button 2</button>
  </div>
  <div class="btn-group" role="group" aria-label="Second group">
    <button class="btn btn-secondary">Button 3</button>
  </div>
</div>

How can you create button dropdowns with split buttons?

Split buttons combine a regular button with a dropdown button. You can achieve this using the .dropdown-toggle-split class along with the .btn-group class. Here's an example:

<div class="btn-group">
  <button class="btn btn-primary">Action</button>
  <button class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropdown</span>
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Option 1</a>
    <a class="dropdown-item" href="#">Option 2</a>
  </div>
</div>

How can you create button groups with vertical alignment?

To create vertically-aligned button groups, use the .btn-group-vertical class instead of .btn-group. Here's an example:

<div class="btn-group-vertical">
  <button class="btn btn-primary">Button 1</button>
  <button class="btn btn-primary">Button 2</button>
  <button class="btn btn-primary">Button 3</button>
</div>

How can you add icons to Bootstrap buttons?

You can add icons to Bootstrap buttons using the <i> element or an icon library like FontAwesome. Here's an example using FontAwesome:

<button class="btn btn-primary">
  <i class="fas fa-star"></i> Star
</button>

How can you create block-level buttons?

Block-level buttons span the full width of their parent container. You can achieve this using the .btn-block class:

<button class="btn btn-primary btn-block">Block Button</button>

How can you create toggle buttons in Bootstrap?

Toggle buttons can have different styles for active and inactive states. You can use the .active class to indicate the active state:

<button class="btn btn-primary active">Active Button</button>
<button class="btn btn-primary">Inactive Button</button>

How can you create button loading spinners?

You can add a loading spinner to a button to indicate that an action is in progress. Use the .spinner-border class along with the .visually-hidden class for accessibility:

<button class="btn btn-primary">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  <span class="visually-hidden">Loading...</span>
</button>

How can you create button badges in Bootstrap?

Button badges are used to display additional information. You can add badges using the .badge class:

<button class="btn btn-primary">
  Notifications <span class="badge bg-secondary">5</span>
</button>

How can you create button dropdowns with icons?

You can include icons in button dropdowns by using icon classes alongside text:

<div class="btn-group">
  <button class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <i class="fas fa-cog"></i> Options
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#"><i class="fas fa-pencil-alt"></i> Edit</a>
    <a class="dropdown-item" href="#"><i class="fas fa-trash"></i> Delete</a>
  </div>
</div>

How can you create responsive button sizing in Bootstrap?

You can control the size of buttons responsively using size classes combined with responsive utility classes. For example:

<button class="btn btn-primary btn-lg">Large (LG)</button>
<button class="btn btn-primary btn-sm">Small (SM)</button>
<button class="btn btn-primary btn-block">Block (Block)</button>

How can you create outline buttons in Bootstrap?

Outline buttons are buttons with transparent backgrounds and colored borders. You can create them using the btn-outline-* classes, like btn-outline-primary or btn-outline-secondary.

<button class="btn btn-outline-primary">Outline Primary</button>
<button class="btn btn-outline-secondary">Outline Secondary</button>

What is the purpose of block-level buttons in Bootstrap?

Block-level buttons span the entire width of their parent container. You can create a block-level button using the btn-block class:

<button class="btn btn-primary btn-block">Block Button</button>

How can you create split dropdown buttons in Bootstrap?

Split dropdown buttons combine a button with a dropdown menu and a separate trigger for the dropdown. Use the dropdown-toggle-split class in addition to the dropdown-toggle class:

<div class="btn-group">
  <button class="btn btn-primary">Action</button>
  <button class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown"></button>
  <div class="dropdown-menu">
    <!-- Dropdown options here -->
  </div>
</div>

How can you create a button that triggers a modal in Bootstrap?

To create a button that triggers a modal, set the data-bs-toggle attribute to "modal" and provide the data-bs-target attribute with the ID of the modal:

<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
  Open Modal
</button>

How can you disable a Bootstrap button?

To disable a Bootstrap button, add the disabled attribute to the button element:

<button class="btn btn-primary" disabled>Disabled Button</button>

How can you create a button with a loading spinner in Bootstrap?

You can create a button with a loading spinner using Bootstrap's spinner classes and visually hidden text for accessibility:

<button class="btn btn-primary">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  <span class="visually-hidden">Loading...</span>
</button>

How can you create buttons that expand and collapse content in Bootstrap?

You can create buttons that toggle the visibility of content using Bootstrap's collapse component. For example:

<button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#collapseExample">
  Toggle Content
</button>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Content to be collapsed
  </div>
</div>

How can you create a button with a badge in Bootstrap?

You can add a badge to a button using the badge class:

<button class="btn btn-primary">
  Notifications <span class="badge bg-secondary">5</span>
</button>

What are the default button styles in Bootstrap?

Bootstrap provides a default button style with a slightly rounded border and a subtle gradient background. It's clean and versatile, suitable for most use cases.

How can you create rounded buttons in Bootstrap?

Rounded buttons have rounded corners, giving them a softer appearance. To create rounded buttons, add the rounded class:

<button class="btn btn-primary rounded">Rounded Button</button>

What is the purpose of pill-shaped buttons in Bootstrap?

Pill-shaped buttons have a rounded appearance on both ends, making them useful for emphasizing selected or active options. Use the rounded-pill class to create pill-shaped buttons:

<button class="btn btn-primary rounded-pill">Pill-shaped Button</button>

How can you create an outline button with the primary color in Bootstrap?

To create an outline button with the primary color, you can use the btn-outline-primary class:

<button class="btn btn-outline-primary">Primary Outline</button>

What are the different contextual colors available for outline buttons in Bootstrap?

Bootstrap provides contextual color classes for outline buttons, such as:

  • btn-outline-primary
  • btn-outline-secondary
  • btn-outline-success
  • btn-outline-danger
  • btn-outline-warning
  • btn-outline-info
  • btn-outline-light
  • btn-outline-dark

Why might you want to change the size of a button in Bootstrap?

Changing the size of a button in Bootstrap allows you to emphasize certain actions, control visual hierarchy, and create a more user-friendly layout by appropriately scaling buttons based on their importance and context.

How can you combine different size and style classes for a button in Bootstrap?

You can combine size and style classes by adding them together in the button element's class attribute. For example:

<button class="btn btn-outline-danger btn-lg">Large Red Outline</button>

How can you create a responsive button group with different sized buttons in Bootstrap?

To create a responsive button group with different sized buttons, combine the size classes with the responsive utility classes:

<div class="btn-group">
  <button class="btn btn-primary btn-lg">Large</button>
  <button class="btn btn-primary btn-sm d-none d-md-block">Small (Medium and Up)</button>
</div>

What is the role of the aria-disabled attribute in Bootstrap disabled buttons?

The aria-disabled attribute is used to communicate to screen readers and assistive technologies that a button is disabled. It helps users with disabilities understand the non-interactive state of the button.

Can disabled buttons still be focused using keyboard navigation?

No, disabled buttons cannot be focused using keyboard navigation. When navigating through interactive elements using the keyboard, disabled buttons are skipped.

Are disabled buttons visible to CSS styles?

Yes, disabled buttons are still visible to CSS styles. You can target disabled buttons with CSS selectors for further styling or adjustments if needed.

How do screen readers treat disabled buttons in Bootstrap?

Screen readers announce the disabled state of the button and the aria-disabled attribute. This helps users relying on screen readers understand that the button cannot be interacted with.

Can you use JavaScript to enable a disabled button dynamically?

Yes, you can use JavaScript to dynamically enable a disabled button based on certain conditions or user interactions. You can remove the disabled attribute using JavaScript.

How can you create a disabled link-style button in Bootstrap?

To create a disabled link-style button, use the btn btn-link classes along with the disabled attribute:

<button class="btn btn-link" disabled>Disabled Link-style Button</button>

What are active buttons in Bootstrap?

Active buttons in Bootstrap are buttons that appear pressed or in a "down" state when clicked, providing visual feedback to users that they've interacted with the button. They revert to their default appearance when the mouse button is released.

Can you create active outline buttons in Bootstrap?

Yes, you can create active outline buttons by combining the active class with the btn-outline-* class, such as btn-outline-primary:

<button class="btn btn-outline-primary active">Active Outline Button</button>

How do active buttons affect the appearance of the button?

Active buttons appear visually pressed or in a "down" state, which typically includes a darker background and a lighter border. They give users feedback that their click has been registered.

How can you use active buttons to indicate the current page in navigation menus?

Active buttons can be used to highlight the current page or section in navigation menus. By applying the active class to the appropriate navigation link, you visually indicate the user's location.

Can you create active buttons within a tabbed navigation component?

Yes, you can create active buttons within a tabbed navigation component by using the nav-tabs class and applying the active class to the desired tab link:

<ul class="nav nav-tabs">
  <li class="nav-item">
    <a class="nav-link active" href="#">Active Tab</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Regular Tab</a>
  </li>
</ul>

How do active buttons interact with responsive behaviors in Bootstrap?

Active buttons maintain their active appearance regardless of the responsive behavior. They visually indicate the clicked state consistently across different screen sizes.

Can you use JavaScript to dynamically toggle the active state of a button?

Yes, you can use JavaScript to toggle the active class on a button dynamically. By adding or removing the active class, you can control the visual appearance of the button.

What are spinner buttons in Bootstrap?

Spinner buttons in Bootstrap are buttons that display a loading spinner (also known as a "spinner animation" or "loading icon") when clicked. They indicate to users that an action is being processed and that they should wait for the operation to complete.

How can you create a spinner button in Bootstrap?

To create a spinner button in Bootstrap, you can use the spinner-border class along with a button's existing classes:

<button class="btn btn-primary">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  Loading...
</button>

How does the spinner-border class work in Bootstrap spinner buttons?

The spinner-border class adds a small spinner animation to the button. It's typically combined with the spinner-border-sm class to control the spinner's size and the button's text to indicate that the button is in a loading state.

How does the role="status" attribute in the spinner element contribute to accessibility?

The role="status" attribute informs screen readers that the spinner animation conveys the loading status of the button. This helps users relying on assistive technologies understand the context of the spinner.

How can you use spinner buttons with different button sizes in Bootstrap?

To use spinner buttons with different button sizes, combine the spinner-border class with the appropriate button size class (btn-lg, btn-sm, or btn-xs):

<button class="btn btn-primary btn-lg">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  Loading...
</button>

How can you use spinner buttons in combination with form submissions?

To use spinner buttons with form submissions, place the spinner button within a <form> element and use JavaScript to trigger the submission when the button is clicked. The spinner indicates that the form is being submitted.

Are spinner buttons still interactive while the spinner animation is displayed?

No, spinner buttons are typically non-interactive while the spinner animation is displayed. They are designed to indicate that an action is ongoing and to prevent users from clicking the button multiple times.

How can you create a spinner button with a centered spinner animation?

To create a spinner button with a centered spinner animation, you can use the d-flex and justify-content-center classes to horizontally center the spinner within the button:

<button class="btn btn-primary">
  <div class="d-flex justify-content-center">
    <span class="spinner-border" role="status" aria-hidden="true"></span>
  </div>
  Loading...
</button>

Can you use the btn-block class to create a full-width spinner button?

Yes, you can use the btn-block class to create a full-width spinner button, which spans the entire width of its container:

<button class="btn btn-primary btn-block">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  Loading...
</button>

How can you create a spinner button with a loading message using an anchor (<a>) element in Bootstrap?

To create a spinner button with a loading message using an anchor (<a>) element, combine the btn class with the spinner and loading text:

<a href="#" class="btn btn-primary">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  Loading...
</a>

Can you use a spinner button to show the loading state of asynchronous operations in JavaScript?

Yes, you can use a spinner button to indicate the loading state of asynchronous operations in JavaScript. For example, you can trigger the spinner animation before making an AJAX request and stop it after the response is received.

How can you use spinner buttons in conjunction with Bootstrap's progress bars?

You can use spinner buttons in conjunction with Bootstrap's progress bars to create a more dynamic loading experience. Start the progress bar when the spinner button is clicked and complete it when the action is done.

How can you create a spinner button with a custom loading text color?

To create a spinner button with a custom loading text color, use CSS to style the loading text within the button:

<button class="btn btn-primary">
  <span class="spinner-border spinner-border-sm text-danger" role="status" aria-hidden="true"></span>
  Loading...
</button>

Conclusion

Bootstrap empowers web designers and developers with a versatile toolkit for creating call-to-action elements that encourage user engagement and interaction. These interactive buttons in Bootstrap offer a wide range of styles and customization options, making it easy to tailor their appearance to suit the unique needs of each project.

Through the use of button classes and the flexibility of customizing button appearance, designers have the freedom to craft visually appealing and effective call-to-action elements. Whether it's achieving a cohesive look with toolbar buttons, combining various button styles, or ensuring uniformity in height-sized buttons, Bootstrap provides the tools needed to create compelling user interfaces.

Responsive button design ensures that call-to-action elements adapt seamlessly to different devices, enhancing user experiences across the spectrum of screen sizes. The inclusion of active state buttons allows for enhanced user feedback, while the management of disabled button elements is made straightforward.

Custom toolbar buttons offer additional possibilities for organizing and presenting call-to-action elements. By mixing button styles, designers can create unique and visually engaging user interfaces, and height-sized buttons enable precise control over button dimensions.

Responsive layouts are made achievable through button responsiveness, ensuring that user interactions remain effective on all devices. Enabling user interactions with active buttons enhances the user experience, while the capability to manage disabled buttons ensures accessibility and user-focused design.