0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-04 16:24:22 +01:00
Bootstrap/site/content/docs/5.2/components/spinners.md
2022-05-13 19:44:01 +03:00

214 lines
6.8 KiB
Markdown

---
layout: docs
title: Spinners
description: Indicate the loading state of a component or page with Bootstrap spinners, built entirely with HTML, CSS, and no JavaScript.
group: components
toc: true
---
## About
Bootstrap "spinners" can be used to show the loading state in your projects. They're built only with HTML and CSS, meaning you don't need any JavaScript to create them. You will, however, need some custom JavaScript to toggle their visibility. Their appearance, alignment, and sizing can be easily customized with our amazing utility classes.
For accessibility purposes, each loader here includes `role="status"` and a nested `<span class="visually-hidden">Loading...</span>`.
{{< callout info >}}
{{< partial "callout-info-prefersreducedmotion.md" >}}
{{< /callout >}}
## Border spinner
Use the border spinners for a lightweight loading indicator.
{{< example >}}
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{< /example >}}
### Colors
The border spinner uses `currentColor` for its `border-color`, meaning you can customize the color with [text color utilities][color]. You can use any of our text color utilities on the standard spinner.
{{< example >}}
{{< spinner.inline >}}
{{- range (index $.Site.Data "theme-colors") }}
<div class="spinner-border text-{{ .name }}" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{- end -}}
{{< /spinner.inline >}}
{{< /example >}}
{{< callout info >}}
**Why not use `border-color` utilities?** Each border spinner specifies a `transparent` border for at least one side, so `.border-{color}` utilities would override that.
{{< /callout >}}
## Growing spinner
If you don't fancy a border spinner, switch to the grow spinner. While it doesn't technically spin, it does repeatedly grow!
{{< example >}}
<div class="spinner-grow" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{< /example >}}
Once again, this spinner is built with `currentColor`, so you can easily change its appearance with [text color utilities][color]. Here it is in blue, along with the supported variants.
{{< example >}}
{{< spinner.inline >}}
{{- range (index $.Site.Data "theme-colors") }}
<div class="spinner-grow text-{{ .name }}" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{- end -}}
{{< /spinner.inline >}}
{{< /example >}}
## Alignment
Spinners in Bootstrap are built with `rem`s, `currentColor`, and `display: inline-flex`. This means they can easily be resized, recolored, and quickly aligned.
### Margin
Use [margin utilities][margin] like `.m-5` for easy spacing.
{{< example >}}
<div class="spinner-border m-5" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{< /example >}}
### Placement
Use [flexbox utilities][flex], [float utilities][float], or [text alignment][text] utilities to place spinners exactly where you need them in any situation.
#### Flex
{{< example >}}
<div class="d-flex justify-content-center">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
{{< /example >}}
{{< example >}}
<div class="d-flex align-items-center">
<strong>Loading...</strong>
<div class="spinner-border ms-auto" role="status" aria-hidden="true"></div>
</div>
{{< /example >}}
#### Floats
{{< example >}}
<div class="clearfix">
<div class="spinner-border float-end" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
{{< /example >}}
#### Text align
{{< example >}}
<div class="text-center">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
{{< /example >}}
## Size
Add `.spinner-border-sm` and `.spinner-grow-sm` to make a smaller spinner that can quickly be used within other components.
{{< example >}}
<div class="spinner-border spinner-border-sm" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow spinner-grow-sm" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{< /example >}}
Or, use custom CSS or inline styles to change the dimensions as needed.
{{< example >}}
<div class="spinner-border" style="width: 3rem; height: 3rem;" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow" style="width: 3rem; height: 3rem;" role="status">
<span class="visually-hidden">Loading...</span>
</div>
{{< /example >}}
## Buttons
Use spinners within buttons to indicate an action is currently processing or taking place. You may also swap the text out of the spinner element and utilize button text as needed.
{{< example >}}
<button class="btn btn-primary" type="button" disabled>
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
</button>
<button class="btn btn-primary" type="button" disabled>
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
Loading...
</button>
{{< /example >}}
{{< example >}}
<button class="btn btn-primary" type="button" disabled>
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
</button>
<button class="btn btn-primary" type="button" disabled>
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
Loading...
</button>
{{< /example >}}
## CSS
### Variables
{{< added-in "5.2.0" >}}
As part of Bootstrap's evolving CSS variables approach, spinners now use local CSS variables on `.spinner-border` and `.spinner-grow` for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
Border spinner variables:
{{< scss-docs name="spinner-border-css-vars" file="scss/_spinners.scss" >}}
Growing spinner variables:
{{< scss-docs name="spinner-grow-css-vars" file="scss/_spinners.scss" >}}
For both spinners, small spinner modifier classes are used to update the values of these CSS variables as needed. For example, the `.spinner-border-sm` class does the following:
{{< scss-docs name="spinner-border-sm-css-vars" file="scss/_spinners.scss" >}}
### Sass variables
{{< scss-docs name="spinner-variables" file="scss/_variables.scss" >}}
### Keyframes
Used for creating the CSS animations for our spinners. Included in `scss/_spinners.scss`.
{{< scss-docs name="spinner-border-keyframes" file="scss/_spinners.scss" >}}
{{< scss-docs name="spinner-grow-keyframes" file="scss/_spinners.scss" >}}
[color]: {{< docsref "/utilities/colors" >}}
[display]: {{< docsref "/utilities/display" >}}
[flex]: {{< docsref "/utilities/flex" >}}
[float]: {{< docsref "/utilities/float" >}}
[margin]: {{< docsref "/utilities/spacing" >}}
[sizing]: {{< docsref "/utilities/sizing" >}}
[text]: {{< docsref "/utilities/text" >}}