Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap5 Utilities

Bootstrap5 Utilities

Bootstrap 5 provides many useful classes to help us quickly achieve effects without repeatedly writing CSS code.


Background Color

When setting the background color of different elements, you need to use the .text-* class to set the matching text color:

Example

<div class=">

p-3 mb-2 bg-primary text-white

"

.bg-primary

div

<div class=">

p-3 mb-2 bg-secondary text-white

"

.bg-secondary

div

<div class=">

p-3 mb-2 bg-success text-white

"

.bg-success

div

<div class=">

p-3 mb-2 bg-danger text-white

"

.bg-danger

div

<div class=">

p-3 mb-2 bg-warning text-dark

"

.bg-warning

div

<div class=">

p-3 mb-2 bg-info text-dark

"

.bg-info

div

<div class=">

p-3 mb-2 bg-light text-dark

"

.bg-light

div

<div class=">

p-3 mb-2 bg-dark text-white

"

.bg-dark

div

<div class=">

p-3 mb-2 bg-body text-dark

"

.bg-body

div

<div class=">

p-3 mb-2 bg-white text-dark

"

.bg-white

div

<div class=">

p-3 mb-2 bg-transparent text-dark

"

.bg-transparent

div

Try it »

The .bg-gradient class can set a background color gradient effect:

Example

<div class=">

p-3 mb-2 bg-primary bg-gradient text-white

"

.bg-primary

div

<div class=">

p-3 mb-2 bg-secondary bg-gradient text-white

"

.bg-secondary

div

<div class=">

p-3 mb-2 bg-success bg-gradient text-white

"

.bg-success

div

<div class=">

p-3 mb-2 bg-danger bg-gradient text-white

"

.bg-danger

div

<div class=">

p-3 mb-2 bg-warning bg-gradient text-dark

"

.bg-warning

div

<div class=">

p-3 mb-2 bg-info bg-gradient text-dark

"

.bg-info

div

<div class=">

p-3 mb-2 bg-light bg-gradient text-dark

"

.bg-light

div

<div class=">

p-3 mb-2 bg-dark bg-gradient text-white

"

.bg-dark

div

<div class=">

p-3 mb-2 bg-body bg-gradient text-dark

"

.bg-body

div

<div class=">

p-3 mb-2 bg-white bg-gradient text-dark

"

.bg-white

div

<div class=">

p-3 mb-2 bg-transparent bg-gradient text-dark

"

.bg-transparent

div

Try it »


Border

We can use border-related classes to show borders as needed:

Example

<span class="border" span>
<span class=">

border border-0

"

span

<span class=">

border border-top-0

"

span

<span class=">

border border-end-0

"

span

<span class=">

border border-bottom-0

"

span

<span class=">

border border-start-0

"

span

<br>
<span class="border-top" span>
<span class="border-end" span>
<span class="border-bottom" span>
<span class="border-start" span>

Try it »

The .border-1 to .border-5 classes are used to set the width of border lines:

Example

<span class=">

border border-1

"

span

<span class=">

border border-2

"

span

<span class=">

border border-3

"

span

<span class=">

border border-4

"

span

<span class=">

border border-5

"

span

Try it »

The following sets different colors of borders:

Example

<span class=">

border border-primary

"

span

<span class=">

border border-secondary

"

span

<span class=">

border border-success

"

span

<span class=">

border border-danger

"

span

<span class=">

border border-warning

"

span

<span class=">

border border-info

"

span

<span class=">

border border-light

"

span

<span class=">

border border-dark

"

span

<span class=">

border border-white

"

span

Try it »

rounded Related classes are used to set rounded corners:

Example

<img src=">

...

"

class

=

"

rounded

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-top

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-end

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-bottom

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-start

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-circle

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-pill

"

alt

=

"

...

"

Try it »

The rounded-0 to rounded-3 classes are used to set the size of rounded corners:

Example

<img src=">

...

"

class

=

"

rounded-0

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-1

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-2

"

alt

=

"

...

"

<img src=">

...

"

class

=

"

rounded-3

"

alt

=

"

...

"

Try it »


Floats and Clearing Floats

Use the .float-end class to float an element to the right, the .float-start class to float it to the left, and the .clearfix class to clear floats:

Example

<div class="clearfix">
<span class="float-start">

Float left

span

<span class="float-end">

Float right

span

div

Try it »

You can also set float effects based on screen size (.float-*-left|right - * means sm (>=576px), md (>=768px), lg (>=992px), xl (>=1200px) or xxl (>=1400px)):

Example

<div class="float-sm-end">

Float right on small screens

div

<br>
<div class="float-md-end">

Float right on medium screens

div

<br>
<div class="float-lg-end">

Float right on large screens

div

<br>
<div class="float-xl-end">

Float right on extra large screens

div

<br>
<div class="float-xxl-end">

Float right on super large screens

div

<br>
<div class="float-none">

No Float

div

Try it »


Center Alignment

Use the .mx-auto class to center an element (add margin-left and margin-right as auto):

Example

<div class=">

mx-auto bg-warning

"

style

=

"

width:150px

"

Center Alignment

div

Try it »


Width and Height

Width is set using the w-* classes (.w-25, .w-50, .w-75, .w-100, .mw-auto, .mw-100).

Example

<div class=">

w-25 bg-warning

"

Width is 25%

div

<div class=">

w-50 bg-warning

"

Width is 50%

div

<div class=">

w-75 bg-warning

"

Width is 75%

div

<div class=">

w-100 bg-warning

"

Width is 100%

div

<div class=">

w-auto bg-warning

"

Auto width

div

<div class=">

mw-100 bg-warning

"

Maximum width is 100%

div

Try it »

Height is set using the h-* classes (.h-25, .h-50, .h-75, .h-100, .mh-auto, .mh-100).

Example

<div style="height:200px;background-color:#ddd">
<div class=">

h-25 d-inline-block p-2 bg-warning

"

Height is 25%

div

<div class=">

h-50 d-inline-block p-2 bg-warning

"

Height is 50%

div

<div class=">

h-75 d-inline-block p-2 bg-warning

"

Height is 75%

div

<div class=">

h-100 d-inline-block p-2 bg-warning

"

Height is 100%

div

<div class=">

h-auto d-inline-block p-2 bg-warning

"

Auto height

div

<div class=">

mh-100 d-inline-block p-2 bg-warning

"

style

=

"

height:500px

"

Maximum height is 100%

div

div

Try it »


Spacing

The spacing setting syntax is as follows:



{property}{sides}-{size}  // 适用 xs(<=576px)

{property}{sides}-{breakpoint}-{size} // 适用 sm (>=576px), md (>=768px), lg (>=992px), xl (>=1200px) 或 xxl (>=1400px)

breakpoints refer to screen width: xs (<=576px), sm (>=576px), md (>=768px), lg (>=992px), xl (>=1200px) or xxl (>=1400px).

property represents the property, including:

sides mainly refers to direction:

size refers to the size of the margin:

Take a look at the source code settings for some of the margins:

.mt-0 {

  margin-top: 0 !important;

}



.ml-1 {

  margin-left: ($spacer * .25) !important;

}



.px-2 {

  padding-left: ($spacer * .5) !important;

  padding-right: ($spacer * .5) !important;

}



.p-3 {

  padding: $spacer !important;

}

Example

<

div

class

=

"

mx-auto

"

style

=

"

width: 200px;

"

Center an Element

div

<form>
<div class="form-row">
<div class="form-group">
<label for="text" class="mt-2">

Set margin-top:

label

<input type="text" class="form-control" id="text" placeholder="email">
<label for="color" class="mt-2">

Color:

label

<input type="color" id="color" class="form-control" style=">

width: 60px;padding: 4px;

"

autocomplete

=

"

off

"

value

=

"

#656565

"

div

div

form

Try it »

More examples

.m-# / m-*-# Set the margins on all sides Try it
.mt-# / mt-*-# margin top Try it
.mb-# / mb-*-# margin bottom Try it
.ms-# / ms-*-# margin left Try it
.me-# / me-*-# margin right Try it
.mx-# / mx-*-# margin left and right Try it
.my-# / my-*-# margin top and bottom Try it
.p-# / p-*-# Set padding using sides Try it
.pt-# / pt-*-# padding top Try it
.pb-# / pb-*-# padding bottom Try it
.ps-# / ps-*-# padding left Try it
.pe-# / pe-*-# padding right Try it
.py-# / py-*-# padding top and bottom Try it
.px-# / px-*-# padding left and right Try it