-->

Bootstrap - Buttons

An essential component of every website or online application is its button. With the abundance of button styles, sizes, states, and modifications that Bootstrap 5 allows, developers can create buttons that are both aesthetically pleasing and useful with more ease. We'll go over all you need to know about Bootstrap 5 buttons in this blog post.

1. Standard Buttons

Several predefined button styles employing different contextual classes are provided by Bootstrap. The <button> or <a> tag is used to build buttons, and the.btn class and a certain contextual class are used to specify the button's look.

Contextual Classes:

.btn-primary: Represents primary action buttons.

.btn-secondary: Neutral, secondary actions.

.btn-success: Indicates successful actions.

.btn-danger: For dangerous or negative actions.

.btn-warning: Warning or caution.

.btn-info: Informational actions.

.btn-light: Light-themed buttons.

.btn-dark: Dark-themed buttons.

.btn-link: Buttons that look like links.

Example:

<button type="button" class="btn btn-primary">Primary Button</button>

<button type="button" class="btn btn-success">Success Button</button>

<button type="button" class="btn btn-danger">Danger Button</button>

Output:

Primary buttons are generally used for the most important action on the page.

Success buttons indicate positive actions (e.g., form submission).

Danger buttons are used for actions that might cause harm or deletion.

2. Button Sizes

Bootstrap 5 provides options to adjust the size of buttons to fit your design:

.btn-lg: For large buttons.

.btn-sm: For small buttons.

Default: The standard button size (no additional class needed).

Example:

<button type="button" class="btn btn-primary btn-lg">Large Button</button>

<button type="button" class="btn btn-secondary btn-sm">Small Button</button>

<button type="button" class="btn btn-warning">Default Button</button>

You can use large buttons for call-to-actions and small buttons for secondary actions or in compact spaces.

3. Block-Level Buttons

To create a button that takes up the full width of its parent container, use the .d-block and .w-100 classes together.

Example:

<button type="button" class="btn btn-info d-block w-100">Block-Level Button</button>

This is useful for mobile layouts where you want buttons to span the entire width of the screen.

4. Outline Buttons

Outline buttons offer a less prominent, more subtle style, showing only a border with no background. This is achieved using the .btn-outline-* class.

Example:

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

<button type="button" class="btn btn-outline-danger">Danger Outline</button>

<button type="button" class="btn btn-outline-success">Success Outline</button>

These buttons maintain the same interaction and behavior as regular buttons but appear less heavy visually.

5. Button States: Active, Disabled

Active Button:

You can toggle the active state of a button by adding the .active class.

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

Disabled Button:

To disable a button, simply add the disabled attribute to the button element or the .disabled class to an anchor button.

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

<a href="#" class="btn btn-light disabled" tabindex="-1" aria-disabled="true">Disabled Link</a>

Disabled buttons are visually distinct and cannot be interacted with.

6. Button Grouping

If you need to group buttons together for actions like toolbars or segmented controls, you can use the .btn-group class. All buttons inside a group are aligned next to each other.

Example:

<div class="btn-group" role="group" aria-label="Button group example">

  <button type="button" class="btn btn-primary">Left</button>

  <button type="button" class="btn btn-primary">Middle</button>

  <button type="button" class="btn btn-primary">Right</button>

</div>

Vertical Button Group:

For vertical alignment, add the .btn-group-vertical class.

<div class="btn-group-vertical" role="group" aria-label="Vertical button group">

  <button type="button" class="btn btn-success">Top</button>

  <button type="button" class="btn btn-success">Middle</button>

  <button type="button" class="btn btn-success">Bottom</button>

</div>

7. Buttons with Icons

You can easily add icons to buttons using icon libraries like Font Awesome or Bootstrap Icons.

Example:

<button type="button" class="btn btn-warning">

  <i class="bi bi-exclamation-triangle-fill"></i> Warning

</button>

This combination improves the clarity of the button’s purpose, adding a visual cue.

8. Button Loading States

Bootstrap doesn’t provide built-in button loading states, but you can easily implement them using JavaScript to show a spinner or loading text.

Example:

<button class="btn btn-primary" type="button" disabled>

  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>

  Loading...

</button>

The spinner inside the button indicates a loading state, which can be toggled via JavaScript.

9. Buttons in Forms

Buttons are frequently used within forms for submitting or resetting data. You can specify the button type as submit or reset.

Example:

<form>

  <button type="submit" class="btn btn-primary">Submit</button>

  <button type="reset" class="btn btn-secondary">Reset</button>

</form>

Using the appropriate button type ensures proper form behavior.

10. Custom Buttons

If you want more control over the appearance of your buttons, you can create custom buttons using Bootstrap's utility classes or your custom CSS.

Example:

<button type="button" class="btn btn-custom">Custom Button</button>

<style>

  .btn-custom {

    background-color: #ff6347;

    color: white;

    border-radius: 25px;

    padding: 10px 20px;

  }

</style>

In this example, we customize the button’s color, border-radius, and padding to create a unique button style.

Conclusion

Because of their adaptability, Bootstrap 5 buttons may be styled and customized in a variety of ways for a range of purposes. With Bootstrap's button classes and utilities, you can easily incorporate responsive and effective buttons into your web projects, regardless of whether you're developing basic buttons, group buttons, or loading states.

You can make the most of Bootstrap's button system and design engaging, interactive user interfaces (UI) for your website or web application by utilizing the examples and advice provided above.