How to Use Bootstrap Buttons in HTML


Bootstrap is a popular front-end framework for building responsive websites and web applications. It was developed by Twitter and is now maintained by the open-source community. One of its key components is the button, which allows users to interact with the interface. In this article, we will explore how to effectively utilize Bootstrap buttons in HTML. We are going to fully break down the bootstrap button for you. We will be using the latest released bootstrap version i.e. v5.3 to keep things up-to-date.

Setting Up Bootstrap in HTML

Before we can use Bootstrap buttons, we need to include the Bootstrap CSS and JavaScript files in our HTML document. We can either download the files and host them locally or use a content delivery network (CDN) to link to the files remotely. For CSS, place the <link> in the head section, and for JavaScript, place the <script> before closing the body.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bootstrap Buttons</title>
    <!-- For CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
    <!-- For JS Bundle -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

Basic Button Syntax

Creating a basic Bootstrap button is straightforward. We use the <button> element and apply the .btn class to it.

<button type="button" class="btn">Button </button>

Button Styles and Variants

Bootstrap provides various styles and variants for buttons, allowing us to customize their appearance to suit our design requirements. Some commonly used styles include primary, secondary, success, danger, warning, info, light, and dark.

<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-light">Light</button>
<button type="button" class="btn btn-dark">Dark</button>

Outline Buttons

Bootstrap provides several predefined button classes that can be applied directly to buttons. If you are not into the heavy backdrop colors, then replace the default modifier class with .btn-outline to eliminate background colors. Here too commonly used styles include primary, secondary, success, danger, warning, info, light, and dark.

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

Button Sizes

Buttons in Bootstrap can also be resized using size classes such as .btn-lg and .btn-sm.

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

Block Buttons

You can create responsive stacks of full-width using display utilities.

<div class="d-grid">
  <button class="btn btn-primary" type="button">Button</button>
</div>

Disabled Buttons

To create a disabled button, simply add the disabled attribute to the <button> element. But the disabled attribute is not supported by <a>s, so to make something visually disabled, you must add the .disabled class.

<button type="button" class="btn btn-primary" disabled>Primary button</button>
<a class="btn btn-primary disabled" role="button" aria-disabled="true">Primary link</a>

Button with Icons

Bootstrap allows us to add icons to buttons using icon fonts such as Font Awesome.

<button type="button" class="btn btn-primary"><i class="fa-solid fa-magnifying-glass"></i> Search</button>

Button Groups

Button groups enable us to group multiple buttons on a single line. This can be done by wrapping multiple buttons in class .btn-group.

<div class="btn-group" role="group" aria-label="Button Group">
  <button type="button" class="btn btn-primary">Button 1</button>
  <button type="button" class="btn btn-primary">Button 2</button>
  <button type="button" class="btn btn-primary">Button 3</button>
</div>

Button Dropdowns

Buttons can be converted into dropdown buttons by combining buttons with dropdown menus, providing additional functionality.

<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown button
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
    <li><hr class="dropdown-divider"></li>
    <li><a class="dropdown-item" href="#">Separated link</a></li>
  </ul>
</div>

Button Tags

The .btn class is to be normally used with the <button> element. Nevertheless, these classes can also be applied to <a> or <input> elements. It should be applied to these links with role=”button” so that readers can understand their intended use.

<a href="#" class="btn btn-primary">Click Me</a>
<a class="btn btn-primary" href="#" role="button">Link</a>
<button class="btn btn-primary" type="submit">Button</button>
<input class="btn btn-primary" type="button" value="Input">
<input class="btn btn-primary" type="submit" value="Submit">
<input class="btn btn-primary" type="reset" value="Reset">

Customizing Buttons

We can customize Bootstrap buttons further by applying additional classes or CSS styles.

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

<!-- For CSS -->
.custom-button {
    font-size: 20px;
    font-weight: 800;
    color: black;
    border-color: black;
    background-color: beige;
    padding: 10px 20px;
}

.custom-button:hover,
.custom-button:active {
    color: white;
    border-color: black;
    background-color: black;
}

Button Events and JavaScript

JavaScript can be used to handle button click events and perform actions accordingly. You have to put <script> before the end of the body.

<button type="button" class="btn btn-primary" onclick="myFunction()">Click Me</button>

<script>
  function myFunction() {
    alert("Button is clicked!");
  }
</script>

Best Practices for Using Bootstrap Buttons

When using Bootstrap buttons, it’s essential to follow best practices to ensure a seamless user experience. Some tips include using consistent button styles throughout the website, providing clear button labels, and prioritizing accessibility.

Conclusion

Bootstrap buttons are versatile components that enhance the interactivity and functionality of web pages. By learning the various features and customization options available, developers can create visually appealing and user-friendly interfaces.

Frequently Asked Questions

  1. Can Bootstrap buttons be customized?
    Yes, Bootstrap buttons can be customized using CSS classes or additional styles.
  2. How do I add icons to Bootstrap buttons?
    Icons can be added to Bootstrap buttons using icon fonts like Font Awesome, and Iconify or by embedding SVG icons directly into the button markup.
  3. Are Bootstrap buttons responsive?
    Yes, Bootstrap buttons are responsive by default and adapt to different screen sizes.
  4. Can I use Bootstrap buttons without the Bootstrap framework?
    While it’s possible to style buttons similarly to Bootstrap buttons without using the framework, Bootstrap provides additional features and functionality that make button customization easier.
  5. What is the purpose of disabled buttons in Bootstrap?
    Disabled buttons are used to indicate that a button is not currently actionable or available for interaction.