Bootstrap 5 Utilities / Helper Classes
There are numerous utility and helper classes in Bootstrap 5 that can be used to quickly style elements without writing any CSS.
Below are some of the ones that I've found to work in World Anvil.
Note: This article is part of a Bootstrap 5 series.
Borders
To add or remove borders from an element, use the border classes:
[section:border-0] [/section]
[section:border-top-0] [/section]
[section:border-end-0] [/section]
[section:border-bottom-0] [/section]
[section:border-start-0] [/section]
[section:y border-top] [/section]
[section:y border-end] [/section]
[section:y border-bottom] [/section]
[section:y border-start] [/section]
Border Width
Use .border-1 to .border-5 to change the width of the border:
[section:border-2] [/section]
[section:border-3] [/section]
[section:border-4] [/section]
[section:border-5] [/section]
Border Color
Any of the contextual color classes can be used to add color to the border:
[section:border-secondary] [/section]
[section:border-success] [/section]
[section:border-danger] [/section]
[section:border-warning] [/section][br]
[section:border-info] [/section]
[section:border-light] [/section]
[section:border-dark] [/section]
[section:border-white] [/section]
Border Radius
Add rounded corners to an element with the rounded classes:
[section:rounded-top] [/section]
[section:rounded-end] [/section]
[section:rounded-bottom] [/section]
[section:rounded-start] [/section]
[section:rounded-circle] [/section]
[section:rounded-pill] [/section]
[section:rounded-0] [/section]
[section:rounded-1] [/section]
[section:rounded-2] [/section]
[section:rounded-3] [/section]
[section:rounded-4] [/section]
[section:rounded-5] [/section]
Float and Clearfix
Use the .float-end or .float-start classes to float an element to the right or left, respectively, and the .clearfix class to remove any floating points:
Responsive Floats
Note: Float an element to the left or to the right depending on screen width with the responsive float classes: .float-*-start|end - where * is:
sm (≥ 576px), md (≥ 768px), lg (≥ 992px), xl (≥ 1200px) or xxl (≥ 1400px)
[section:float-md-end].float-md-end - medium screens or wider[/section]
[section:float-lg-end].float-lg-end - large screens or wider[/section]
[section:float-xl-end].float-xl-end - extra large screens or wider[/section]
[section:float-xxl-end].float-xxl-end - XXL screens or wider[/section]
[section:float-none].float-none[/section]
Center Align
The .mx-auto class centers elements and adds margin-left and margin-right: auto
Vertical Align
Use the align- classes to change the alignment of elements (only works on inline, inline-block, inline-table and table cell elements):
[section:align-top]top[/section]
[section:align-middle]middle[/section]
[section:align-bottom]bottom[/section]
[section:align-text-top]text-top[/section]
[section:align-text-bottom]text-bottom[/section]
Width
Set the width of an element with the w-* classes
(.w-25, .w-50, .w-75, .w-100, .mw-auto, .mw-100):
Height
Set the height of an element with the h-* classes
(.h-25, .h-50, .h-75, .h-100, .mh-auto, .mh-100):
Spacing
Bootstrap 5 has a wide range of responsive margin and padding utility classes. They work for all breakpoints:
- xs (≤ 576px)
- sm (≥ 576px)
- md (≥ 768px)
- lg (≥ 992px)
- xl (≥ 1200px)
- xxl (≥ 1400px)
Note: The classes are used in the format:
{property}{sides}-{size} for xs and
{property}{sides}-{breakpoint}-{size} for sm, md, lg, xl and xxl.
Where property is one of:
- m - sets margin
- p - sets padding
Where sides is one of:
- t - sets margin-top or padding-top
- b - sets margin-bottom or padding-bottom
- s - sets margin-left or padding-left
- e - sets margin-right or padding-right
- x - sets both padding-left & padding-right or margin-left & margin-right
- y - sets both padding-top & padding-bottom or margin-top & margin-bottom
- blank - sets a margin or padding on all 4 sides of the element
Where size is one of:
- 0 - sets margin or padding to 0
- 1 - sets margin or padding to .25rem
- 2 - sets margin or padding to .5rem
- 3 - sets margin or padding to 1rem
- 4 - sets margin or padding to 1.5rem
- 5 - sets margin or padding to 3rem
- auto - sets margin to auto
Spacing Examples
.m-# / m-*-# | margin on all sides |
.mt-# / mt-*-# | margin top |
.mb-# / mb-*-# | margin bottom |
.ms-# / ms-*-# | margin left |
.me-# / me-*-# | margin right |
.mx-# / mx-*-# | margin left and right |
.my-# / my-*-# | margin top and bottom |
.p-# / p-*-# | padding on all sides |
.pt-# / pt-*-# | padding top |
.pb-# / pb-*-# | padding bottom |
.ps-# / ps-*-# | padding left |
.pe-# / pe-*-# | padding right |
.py-# / py-*-# | padding top and bottom |
.px-# / px-*-# | padding left and right |
Shadows
Use the shadow- classes to add shadows to an element:
Aspect Ratio
Create responsive video or slideshows based on the width of the parent.
Add the .ratio class together with an aspect ratio of your choice .ratio-* to a parent element, and add the embed (video or iframe) inside of it:
<!-- Aspect ratio 1:1 --> [container:ratio ratio-1x1] [/container] <!-- Aspect ratio 4:3 --> [container:ratio ratio-4x3] [/container] <!-- Aspect ratio 16:9 --> [container:ratio ratio-16x9] [/container] <!-- Aspect ratio 21:9 --> [container:ratio ratio-21x9] [/container]
Visibility
Use the .visible or .invisible classes to control the visibility of elements.
Note: These classes do not change the CSS display value. They only add visibility:visible or visibility:hidden:
[container:visible]I am visible[/container] [container:invisible]I am invisible[/container]
Background Colors
The classes for background colors are:
.bg-primary, .bg-success, .bg-info, .bg-warning, .bg-danger, .bg-secondary, .bg-dark and .bg-light.
Text Colors
The classes for text colors are:
.text-primary, .text-secondary, .text-success, .text-danger, .text-warning, .text-info, .text-light, .text-dark, .text-body (default), .text-muted, and .text-white.
The .bg-color classes above does not work well with text, or atleast then you have to specify a proper .text-color class to get the right text color for each background. You can also add 50% opacity for black or white text with the .text-black-50 or .text-white-50 classes:
Typography Classes
Bootstrap 5 utilizes a default font-size of 1rem, which typically equates to 16 pixels. Additionally, it employs a line-height value of 1.5 to provide spacing between lines of text. All paragraph [p] elements in Bootstrap 5 have margin-top: 0 and margin-bottom: 1rem (16px by default).
To apply any of the typography classes, use them in any [section] or [container] element.
The [p] element defines a regular paragraph.
Use the .lead class to make a paragraph stand out.[p]The p element defines a regular paragraph.[/p] [section:lead]Use the .lead class to make a paragraph stand out.[/section]
.text-uppercase uppers text case.
.text-capitalize capitalizes text.
[section:text-lowercase].text-lowercase lowers text case.[/section] [section:text-uppercase].text-uppercase uppers text case.[/section] [section:text-capitalize].text-capitalize capitalizes text.[/section]
[section:text-nowrap].text-nowrap prevents text wrapping. .text-nowrap prevents text wrapping. .text-nowrap prevents text wrapping.[/section]
.list-inline places all list items on a single line, when used together with the .list-inline-item.
[ul] [container:list-inline] [section:list-inline-item]Coffee[/section] [section:list-inline-item]Tea[/section] [section:list-inline-item]Milk[/section] [/container] [/ul]
[section:text-decoration-none]Use the .text-decoration-none class to remove the underline from a [url:#]link[/url].[/section]
.text-center centers text.
.text-end right-aligns text.
[section:text-start].text-start left-aligns text.[/section] [section:text-center].text-center centers text.[/section] [section:text-end].text-end right-aligns text.[/section]
.list-unstyled removes the default list-style and left margin on list items (immediate children only).
- Coffee
- Tea
- Black tea
- Green tea
- Milk
[ul] [container:list-unstyled] [li]Coffee[/li] [li]Tea [ul] [li]Black tea[/li] [li]Green tea[/li] [/ul] [/li] [li]Milk[/li] [/container] [/ul]
Material Design for Bootstrap, www.mdbootstrap.com.
I love this so much! I have a testing page where I've done a few of these things, but you've gone above and beyond! What a great resource for Bootstrap!
Take a look at my Institutions of Learning challenge article.
Learn about the World of Wizard's Peak and check out my award winning article about the Ghost Boy of Kirinal!
These utility classes are probably the #1 thing my designs rely on. And because i rely on them so much, if I find BS5 doesn't have a specific class I need, I make them! For instance, I've extended the margin/padding classes: .mb-10{margin-bottom:8rem!important} .mt-10{margin-top:8rem!important} .ms-10{margin-left:8rem!important} .me-10{margin-right:8rem!important} .my-10{margin-top:8rem!important;margin-bottom:8rem!important} .pb-10{padding-bottom:8rem!important} .pt-10{padding-top:8rem!important} .py-10{padding-bottom:8rem!important;padding-top:8rem!important} ... etc.