View on GitHub

Components

Learn how and why we build nearly all our components responsively and with base and modifier classes.

Base classes

Bootstrap’s components are largely built with a base-modifier nomenclature. We group as many shared properties as possible into a base class, like .btn, and then group individual styles for each variant into modifier classes, like .btn-primary or .btn-success.

To build our modifier classes, we use Sass’s @each loops to iterate over a Sass map. This is especially helpful for generating variants of a component by our $theme-colors and creating responsive variants for each breakpoint. As you customize these Sass maps and recompile, you’ll automatically see your changes reflected in these loops.

Check out our Sass maps and loops docs for how to customize these loops and extend Bootstrap’s base-modifier approach to your own code.

Modifiers

Many of Bootstrap’s components are built with a base-modifier class approach. This means the bulk of the styling is contained to a base class (e.g., .btn) while style variations are confined to modifier classes (e.g., .btn-danger). These modifier classes are built from the $theme-colors map to make customizing the number and name of our modifier classes.

Here are two examples of how we loop over the $theme-colors map to generate modifiers to the .alert and .list-group components.

  1. // Generate contextual modifier classes for colorizing the alert.
  2. @each $state, $value in $theme-colors {
  3. $alert-background: shift-color($value, $alert-bg-scale);
  4. $alert-border: shift-color($value, $alert-border-scale);
  5. $alert-color: shift-color($value, $alert-color-scale);
  6. @if (contrast-ratio($alert-background, $alert-color) < $min-contrast-ratio) {
  7. $alert-color: mix($value, color-contrast($alert-background), abs($alert-color-scale));
  8. }
  9. .alert-#{$state} {
  10. @include alert-variant($alert-background, $alert-border, $alert-color);
  11. }
  12. }
  1. // List group contextual variants
  2. //
  3. // Add modifier classes to change text and background color on individual items.
  4. // Organizationally, this must come after the `:hover` states.
  5. @each $state, $value in $theme-colors {
  6. $list-group-variant-bg: shift-color($value, $list-group-item-bg-scale);
  7. $list-group-variant-color: shift-color($value, $list-group-item-color-scale);
  8. @if (contrast-ratio($list-group-variant-bg, $list-group-variant-color) < $min-contrast-ratio) {
  9. $list-group-variant-color: mix($value, color-contrast($list-group-variant-bg), abs($list-group-item-color-scale));
  10. }
  11. @include list-group-item-variant($state, $list-group-variant-bg, $list-group-variant-color);
  12. }

Responsive

These Sass loops aren’t limited to color maps, either. You can also generate responsive variations of your components. Take for example our responsive alignment of the dropdowns where we mix an @each loop for the $grid-breakpoints Sass map with a media query include.

  1. // We deliberately hardcode the `bs-` prefix because we check
  2. // this custom property in JS to determine Popper's positioning
  3. @each $breakpoint in map-keys($grid-breakpoints) {
  4. @include media-breakpoint-up($breakpoint) {
  5. $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
  6. .dropdown-menu#{$infix}-start {
  7. --bs-position: start;
  8. &[data-bs-popper] {
  9. right: auto;
  10. left: 0;
  11. }
  12. }
  13. .dropdown-menu#{$infix}-end {
  14. --bs-position: end;
  15. &[data-bs-popper] {
  16. right: 0;
  17. left: auto;
  18. }
  19. }
  20. }
  21. }

Should you modify your $grid-breakpoints, your changes will apply to all the loops iterating over that map.

  1. $grid-breakpoints: (
  2. xs: 0,
  3. sm: 576px,
  4. md: 768px,
  5. lg: 992px,
  6. xl: 1200px,
  7. xxl: 1400px
  8. );

For more information and examples on how to modify our Sass maps and variables, please refer to the Sass section of the Grid documentation.

Creating your own

We encourage you to adopt these guidelines when building with Bootstrap to create your own components. We’ve extended this approach ourselves to the custom components in our documentation and examples. Components like our callouts are built just like our provided components with base and modifier classes.

Components - 图1

  1. <div class="callout">...</div>

In your CSS, you’d have something like the following where the bulk of the styling is done via .callout. Then, the unique styles between each variant is controlled via modifier class.

  1. // Base class
  2. .callout {}
  3. // Modifier classes
  4. .callout-info {}
  5. .callout-warning {}
  6. .callout-danger {}

For the callouts, that unique styling is just a border-left-color. When you combine that base class with one of those modifier classes, you get your complete component family:

This is an info callout. Example text to show it in action.

This is a warning callout. Example text to show it in action.

This is a danger callout. Example text to show it in action.