Components
Learn how and why we build nearly all our components responsively and with base and modifier classes.
Base classes
Bootstrap components are typically built with composable classes: a shared base class, variant classes, and optional theme classes. For example, buttons can start from .btn and combine variant classes like .btn-solid or .btn-outline with theme classes like .theme-primary or .theme-success.
Many of these classes are generated with Sass @each loops over component maps (for things like variants and sizes) and theme maps. This keeps class APIs consistent across components and lets one map change update all related selectors at once, including responsive variants where applicable.
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.
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 navbar expand classes where we mix an @each loop for the $breakpoints Sass map with a media query include.
// Generate series of responsive `.navbar-expand` classes for configuring
// where your navbar collapses and expands. Uses container queries so the
// navbar responds to its own width, not the viewport width.
// Mixin for expanded state styles (applied to descendants)
@mixin navbar-expanded {
// Style the inner container since we can't style .navbar itself with container queries
> .container,
> .container-fluid,
%navbar-expand-container {
flex-wrap: nowrap;
justify-content: flex-start;
}
.navbar-nav {
--nav-link-padding-x: var(--navbar-nav-link-padding-x);
flex-direction: row;
}
.navbar-toggler {
display: none !important; // stylelint-disable-line declaration-no-important
}
[class*="drawer"] {
// stylelint-disable declaration-no-important
// Reset native <dialog> UA styles and below-breakpoint drawer styles.
// Must use !important to override both UA <dialog> defaults and the
// responsive drawer styles from media-breakpoint-down().
position: static !important;
inset: auto !important;
z-index: auto;
display: flex !important;
flex-grow: 1;
width: auto !important;
max-width: none !important;
height: auto !important;
max-height: none !important;
padding: 0;
margin: 0;
overflow: visible !important;
visibility: visible !important;
background-color: transparent !important;
border: 0 !important;
transform: none !important;
@include box-shadow(none);
@include transition(none);
// stylelint-enable declaration-no-important
.drawer-header {
display: none !important; // stylelint-disable-line declaration-no-important
}
.drawer-body {
display: flex;
flex-grow: 1;
flex-direction: row;
align-items: center;
padding: 0;
overflow-y: visible;
}
}
}
// Always expanded (no responsive behavior)
.navbar-expand {
@include navbar-expanded();
// Also set on navbar itself for non-responsive case
flex-wrap: nowrap;
justify-content: flex-start;
}
// Responsive navbar expand classes using container queries
@include loop-breakpoints-down($navbar-breakpoints) using ($breakpoint, $next, $prefix) {
@if $next {
.#{$prefix}navbar-expand {
@include container-breakpoint-up($next) {
@include navbar-expanded();
}
}
}
}
Should you modify your $breakpoints, your changes will apply to all the loops iterating over that map.
$breakpoints: (
xs: 0,
sm: 576px,
md: 768px,
lg: 1024px,
xl: 1280px,
2xl: 1536px
);
For more information and examples on how to modify our Sass maps and variables, please refer to the CSS 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.
<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.
// Base class
.callout {}
// Modifier classes
.callout-info {}
.callout-warning {}
.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.