Master Bootstrap From Scratch

Clear, interactive, and structured Bootstrap lessons designed for beginners and developers.

Bootstrap Spinners

Learn how to create loading indicators using Bootstrap spinners with different colors, sizes, and styles.

What are Spinners?

Spinners are animated loading indicators that show users that an operation is in progress.

They are commonly used while loading data, submitting forms, processing requests, or waiting for a server response.

Simple Example: When a user clicks Login, a spinner can appear while the application checks the login details.

1. Border Spinner

The .spinner-border class creates a rotating border spinner.

Program Code:
<div class="spinner-border" role="status">
    <span class="visually-hidden">Loading...</span>
</div>
Output:
Loading...
Loading...
Simple Explanation: The spinner rotates continuously to indicate that something is loading.

2. Spinner Colors

Bootstrap background-independent text color classes can be used to change the spinner color.

Program Code:
<div class="spinner-border text-primary"></div>
<div class="spinner-border text-success"></div>
<div class="spinner-border text-danger"></div>
<div class="spinner-border text-warning"></div>
<div class="spinner-border text-info"></div>
Output:
Loading...

Primary

Loading...

Success

Loading...

Danger

Loading...

Warning

Loading...

Info

Simple Explanation: Classes such as text-primary and text-success change the spinner's color.

3. Growing Spinner

The .spinner-grow class creates a spinner that repeatedly grows and fades.

Program Code:
<div class="spinner-grow text-success" role="status">
    <span class="visually-hidden">Loading...</span>
</div>
Output:
Loading...
Loading...
Simple Explanation: Unlike a border spinner, this spinner grows and fades instead of rotating its border.

4. Growing Spinner Colors

You can apply different text color classes to growing spinners.

Program Code:
<div class="spinner-grow text-primary"></div>
<div class="spinner-grow text-success"></div>
<div class="spinner-grow text-danger"></div>
Output:
Loading...

Primary

Loading...

Success

Loading...

Danger

5. Spinner Sizes

Use .spinner-border-sm to create a smaller border spinner. You can also set custom dimensions using CSS.

Program Code:
<div class="spinner-border spinner-border-sm"></div>

<div class="spinner-border"
     style="width: 3rem; height: 3rem;">
</div>

<div class="spinner-grow spinner-grow-sm"></div>

<div class="spinner-grow"
     style="width: 3rem; height: 3rem;">
</div>
Output:
Loading...

Small Border

Loading...

Large Border

Loading...

Small Grow

Loading...

Large Grow

Simple Explanation: The small classes create smaller spinners. CSS width and height allow you to customize their size.

6. Spinner Inside a Button

A spinner can be displayed inside a button to indicate that an action is being processed.

Program Code:
<button class="btn btn-primary" type="button" disabled>

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

    Loading...

</button>
Output:
Simple Explanation: The spinner appears inside the button while the action is in progress. The button is disabled in this example. Real applications should enable it again when processing ends.

7. Spinner with Loading Text

You can combine a spinner with a message to explain what the application is doing.

Program Code:
<div class="d-flex align-items-center gap-2">

    <div class="spinner-border text-primary"
         role="status">
        <span class="visually-hidden">Loading...</span>
    </div>

    <span>Please wait, loading data...</span>

</div>
Output:
Loading...
Please wait, loading data...

8. Real-World Example

Imagine that a student opens the course dashboard. The application can show a spinner while course data is being loaded from the server.

Program Code:
<div class="text-center p-4">

    <div class="spinner-border text-primary"
         role="status">
        <span class="visually-hidden">Loading...</span>
    </div>

    <p class="mt-3">
        Loading course details...
    </p>

</div>
Output:
Loading...

Loading course details...

Important: This example only displays the spinner. To show it while a real server request runs and hide it when the request finishes, you need JavaScript or application logic.

Important Bootstrap Spinner Classes

Class Purpose
.spinner-border Creates a rotating border spinner.
.spinner-grow Creates a growing and fading spinner.
.spinner-border-sm Creates a small border spinner.
.spinner-grow-sm Creates a small growing spinner.
.text-primary Applies the primary text color.
.visually-hidden Provides text for assistive technologies.
.disabled Not a spinner class; use the HTML disabled attribute on a button when needed.

Common Mistakes

  • Forgetting the .spinner-border or .spinner-grow class.
  • Forgetting to load Bootstrap CSS.
  • Expecting a spinner to load data automatically.
  • Assuming the spinner automatically disappears when a request finishes.
  • Forgetting accessible loading text for users who rely on assistive technologies.

Summary

Bootstrap Spinners indicate that an operation is in progress.

  • .spinner-border creates a rotating spinner.
  • .spinner-grow creates a growing spinner.
  • Color classes change the spinner color.
  • Small classes and CSS allow size customization.
  • Spinners can be placed inside buttons and loading messages.
  • JavaScript or application logic is required to control a spinner during a real operation.