Lesson 32 of 60 – Bootstrap Spinners
53%

Bootstrap Spinners

Bootstrap Spinners are loading indicators used to show that a process is currently running. They are useful while a page is loading, data is being fetched, a form is being submitted, or an operation is being processed.

Note: Bootstrap provides ready-made spinner styles such as border spinners and growing spinners. Spinners can also be customized with colors, sizes, alignment utilities, and accessibility attributes.

1. What Is a Spinner?

A Spinner is a visual loading indicator. It tells the user that the application is currently performing an operation.

For example, a spinner can be displayed when:

  • Data is loading.
  • A form is being submitted.
  • A file is uploading.
  • An API request is processing.
  • A page is waiting for a response.

2. Basic Border Spinner

The basic Bootstrap Spinner is created using the .spinner-border class.

<div class="spinner-border">
</div>
Loading...

The spinner rotates continuously to indicate that an operation is in progress.

3. The .spinner-border Class

The .spinner-border class creates a circular border-style loading spinner.

<div class="spinner-border">
</div>
Loading...

This is one of the simplest ways to add a loading indicator using Bootstrap.

4. Spinner with Contextual Colors

Bootstrap contextual text color classes can be used to change the spinner color.

<div class="spinner-border text-primary">
</div>

<div class="spinner-border text-success">
</div>

<div class="spinner-border text-danger">
</div>
Loading...
Loading...
Loading...

5. Primary Spinner

Use text-primary with spinner-border to create a primary-colored spinner.

<div
class="spinner-border text-primary"
role="status">

</div>
Loading...

6. Success Spinner

The text-success class creates a success-colored spinner.

<div
class="spinner-border text-success"
role="status">

</div>
Loading...

7. Danger Spinner

The text-danger class creates a danger-colored spinner.

<div
class="spinner-border text-danger"
role="status">

</div>
Loading...

8. Warning Spinner

The text-warning class can be used to create a warning colored spinner.

<div
class="spinner-border text-warning"
role="status">

</div>
Loading...

9. Info Spinner

Use text-info to create an information-colored spinner.

<div
class="spinner-border text-info"
role="status">

</div>
Loading...

10. Secondary Spinner

The text-secondary class can be used for a secondary spinner.

<div
class="spinner-border text-secondary"
role="status">

</div>
Loading...

11. Growing Spinner

Bootstrap also provides a growing spinner using the .spinner-grow class.

<div class="spinner-grow">
</div>
Loading...

Instead of rotating a border, the growing spinner expands and contracts visually.

12. Colored Growing Spinner

Growing Spinners can also use Bootstrap text color classes.

<div
class="spinner-grow text-primary"
role="status">

</div>
Loading...
Loading...
Loading...

13. Small Spinner

Use .spinner-border-sm to create a smaller border spinner.

<div
class="spinner-border spinner-border-sm"
role="status">

</div>
Loading...

14. Small Growing Spinner

Use .spinner-grow-sm to create a smaller growing spinner.

<div
class="spinner-grow spinner-grow-sm"
role="status">

</div>
Loading...

15. Spinner Inside a Button

A Spinner can be placed inside a button to indicate that an action is currently processing.

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

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

    </span>

    Loading...

</button>

16. Growing Spinner Inside a Button

A growing spinner can also be placed inside a button.

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

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

    </span>

    Processing...

</button>

17. Spinner with Button Text

A spinner can be combined with meaningful text such as Loading..., Saving..., or Processing....

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

    <span
    class="spinner-border
    spinner-border-sm">

    </span>

    Saving...

</button>

The text helps users understand what operation is taking place.

18. Centering a Spinner

Bootstrap utility classes can be used to position a spinner in the center of a container.

<div
class="d-flex
justify-content-center">

    <div
    class="spinner-border"
    role="status">

    </div>

</div>
Loading...

19. Spinner with Margin Utilities

Spacing utilities such as m-*, mt-*, mb-*, and ms-* can be used to add spacing around spinners.

<div
class="spinner-border
text-primary m-3">

</div>
Loading...

20. Spinner in a Loading Message

A spinner can be combined with a message to create a complete loading area.

<div
class="text-center">

    <div
    class="spinner-border
    text-primary">

    </div>

    <p class="mt-2">
    Loading data...
    </p>

</div>
Loading...

Loading data...

21. Spinner for API Requests

Spinners are commonly displayed while a website waits for data from an API or server.

<div class="text-center">

    <div
    class="spinner-border text-info"
    role="status">

        <span
        class="visually-hidden">

        Loading...

        </span>

    </div>

    <p>
    Fetching data...
    </p>

</div>
Loading...

Fetching data...

22. Spinner for Form Submission

A Spinner can indicate that a form is being submitted.

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

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

    </span>

    Submitting...

</button>

In a real application, JavaScript can show the spinner when the form starts submitting and hide it after the server responds.

23. Spinner for File Upload

A Spinner can be displayed while a file is being uploaded to a server.

<button
class="btn btn-success"
disabled>

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

    </span>

    Uploading...

</button>

24. Spinner Accessibility

Loading indicators should be accessible to users who cannot see the animation.

For a standalone spinner, Bootstrap examples commonly use role="status" together with visually hidden text.

<div
class="spinner-border"
role="status">

    <span
    class="visually-hidden">

    Loading...

    </span>

</div>
Loading...
Important: The visible animation alone should not be the only way users receive information about a loading state.

25. Spinner with Different Sizes

Bootstrap provides small spinner variants using .spinner-border-sm and .spinner-grow-sm.

<div
class="spinner-border spinner-border-sm">

</div>

<div
class="spinner-border">

</div>

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

</div>

<div
class="spinner-grow">

</div>
Loading...
Loading...
Loading...
Loading...

26. Spinner in a Card

Spinners can be placed inside cards when card content is still loading.

<div class="card">

    <div class="card-body text-center">

        <div
        class="spinner-border text-primary">

        </div>

        <p class="mt-2">
        Loading student data...
        </p>

    </div>

</div>
Loading...

Loading student data...

27. Common Spinner Mistakes

Avoid these common mistakes when using Bootstrap Spinners:

  • Using a spinner without explaining what is loading.
  • Forgetting accessibility information.
  • Using a spinner when the operation is nearly instant.
  • Showing a spinner indefinitely because the request failed.
  • Using too many animated spinners on one page.
  • Using animation as the only way to communicate status.
  • Forgetting to remove the spinner after an operation completes.
  • Using inappropriate spinner sizes for the interface.
Tip: A spinner should communicate a temporary loading state. It should not remain visible after the operation has finished.

28. Spinner with JavaScript

JavaScript can show and hide a spinner dynamically when an operation starts or finishes.

<div id="loader"
class="spinner-border text-primary">

</div>

<script>

function hideLoader(){

    document
    .getElementById("loader")
    .style.display = "none";

}

</script>

For example, an application can display the spinner before making an AJAX or Fetch request and hide it after receiving the response.

29. Practical Loading Section

The following example creates a simple loading section that can be used in a dashboard or learning website.

<div
class="text-center p-4">

    <div
    class="spinner-border text-primary"
    role="status">

        <span
        class="visually-hidden">

        Loading...

        </span>

    </div>

    <h5 class="mt-3">
    Please Wait
    </h5>

    <p class="text-body-secondary">
    Loading your dashboard...
    </p>

</div>
Loading...
Please Wait

Loading your dashboard...

30. Complete Bootstrap Spinner Example

This example combines a spinner, loading message, button, contextual color, and Bootstrap utilities.

<div
class="card">

    <div
    class="card-body text-center">

        <div
        class="spinner-border text-primary"
        role="status">

            <span
            class="visually-hidden">

            Loading...

            </span>

        </div>

        <h4 class="mt-3">
        Loading Student Dashboard
        </h4>

        <p class="text-body-secondary">
        Please wait while your data is being loaded.
        </p>

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

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

            </span>

            Loading...

        </button>

    </div>

</div>
Loading...

Loading Student Dashboard

Please wait while your data is being loaded.

📌 Key Points

  • .spinner-border creates a border-style Spinner.
  • .spinner-grow creates a growing Spinner.
  • text-primary, text-success, and other contextual classes can change Spinner colors.
  • .spinner-border-sm creates a small border Spinner.
  • .spinner-grow-sm creates a small growing Spinner.
  • Spinners can be placed inside buttons.
  • Spinners can be centered using Bootstrap flex utilities.
  • Spinners are useful for API requests and data loading.
  • Spinners can indicate form submission and file uploads.
  • Spinners can be placed inside cards and dashboard sections.
  • Meaningful loading text improves user understanding.
  • role="status" can be used for standalone status indicators.
  • .visually-hidden can provide accessible loading text.
  • JavaScript can dynamically show and hide Spinners.
  • A Spinner should disappear when the loading operation finishes.

🧠 Quick Quiz

Question: Which Bootstrap class creates a basic border Spinner?