Bootstrap4 Utilities
Bootstrap4 provides some utilities that let us achieve the desired effects without writing CSS code.
Border
Use border classes to add or remove borders:
Example
<span class="border" span>
<span class=">border border-0
"
span
<span class=">border border-top-0
"
span
<span class=">border border-right-0
"
span
<span class=">border border-bottom-0
"
span
<span class=">border border-left-0
"
span
Try it »
Border Colors
Bootstrap4 provides some classes to set border colors:
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 »
Border Radius Settings
Use the rounded class to add rounded borders:
Example
<span class="rounded" span>
<span class="rounded-top" span>
<span class="rounded-right" span>
<span class="rounded-bottom" span>
<span class="rounded-left" span>
<span class="rounded-circle" span>
<span class="rounded-0" span>Try it »
Floats
The .float-right class is used to float an element right, .float-left floats it left, and the .clearfix class is used to clear floats:
Example
<div class="clearfix">
<span class="float-left">Float Left
span
<span class="float-right">Float Right
span
div
Try it »
Responsive Floats
We can set the float direction (.float-*-left|right - * is sm, md, lg or xl) to depend on the screen size:
Example
<div class="float-sm-right">Float right on screens larger than small
div
<br>
<div class="float-md-right">Float right on screens larger than medium
div
<br>
<div class="float-lg-right">Float right on screens larger than large
div
<br>
<div class="float-xl-right">Float right on screens larger than extra large
div
<br>
<div class="float-none">No Float
div
Try it »
Center Alignment
Use the .mx-auto class to set center alignment:
Example
<div class=">mx-auto bg-warning
"
style
=
"
width:150px
"
Center the display
div
Try it »
Width
Use w-* classes (.w-25, .w-50, .w-75, .w-100, .mw-100) on the element to set the width:
Example
<div class=">w-25 bg-warning
"
Width 25%
div
<div class=">w-50 bg-warning
"
Width 50%
div
<div class=">w-75 bg-warning
"
Width 75%
div
<div class=">w-100 bg-warning
"
Width 100%
div
<div class=">mw-100 bg-warning
"
Max Width 100%
div
Try it »
Height
Use h-* classes (.h-25, .h-50, .h-75, .h-100, .mh-100) on the element to set the height:
Example
<div style="height:200px;background-color:#ddd">
<div class=">h-25 bg-warning
"
Height 25%
div
<div class=">h-50 bg-warning
"
Height 50%
div
<div class=">h-75 bg-warning
"
Height 75%
div
<div class=">h-100 bg-warning
"
Height 100%
div
<div class=">mh-100 bg-warning
"
style
=
"
height:500px
"
Max Height 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)
breakpoints refer to screen width: xs (<=576px), sm (>=576px), md (>=768px), lg (>=992px), xl (>=1200px)。
property represents the property, including:
m- used to setmarginp- used to setpadding
sides mainly refers to direction:
t- used to setmargin-toporpadding-topb- used to setmargin-bottomorpadding-bottoml- used to setmargin-leftorpadding-leftr- used to setmargin-rightorpadding-rightx- used to set*-leftand*-righty- used to set*-topand*-bottom- blank - used to set the element in all four directions
marginorpadding
size refers to the size of the margin:
0- setmarginorpaddingis01- setmarginorpaddingis$spacer * .252- setmarginorpaddingis$spacer * .53- setmarginorpaddingis$spacer4- setmarginorpaddingis$spacer * 1.55- setmarginorpaddingis$spacer * 3auto- setmarginto auto
Margin margin You can set negative numbers by adding a letter before the number "n" :
n1- set margin to -.25rem (-4px if font-size is 16px)n2- set margin to -.5rem (-8px if font-size is 16px)n3- set margin to -1rem (-16px if font-size is 16px)n4- set margin to -1.5rem (-24px if font-size is 16px)n5- set margin to -3rem (-48px if font-size is 16px)
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 |
.ml-# / ml-*-# |
margin left | Try it |
.mr-# / mr-*-# |
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 |
.pl-# / pl-*-# |
padding left | Try it |
.pr-# / pr-*-# |
padding right | Try it |
.py-# / py-*-# |
padding top and bottom | Try it |
.px-# / px-*-# |
padding left and right | Try it |