Bootstrap4 Utilities

Bootstrap4 provides some utilities that allow us to achieve the desired effects without writing CSS code.


Borders

Useborderthe class can 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 Color

Bootstrap4 provides some classes to set the border color:

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

Useroundedthe class can 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 »

Float

.float-rightthe class is used to set the element to float right,.float-leftset the element to float left,.clearfixthe class is used to clear floats:

Example

<div class="clearfix"> <span class="float-left">Left Float</span> <span class="float-right">Right Float</span> </div>

Try it »

Responsive Floats

We can set floats (.float-*-left|right- * can be sm, md, lg or xl) the direction depends on 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.mx-autoclass to set center alignment:

Example

<div class="mx-auto bg-warning" style="width:150px">Centered</div>

Try it »

Width

Use w-* classes on elements (.w-25, .w-50, .w-75, .w-100, .mw-100) 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 on elements (.h-25, .h-50, .h-75, .h-100, .mh-100) 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 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 widths: xs (<=576px), sm (>=576px), md (>=768px), lg (>=992px), xl (>=1200px).

propertyrepresents the property, including:

  • m- used to setmargin
  • p- used to setpadding

sidesmainly refers to direction:

  • t- used to setmargin-toporpadding-top
  • b- used to setmargin-bottomorpadding-bottom
  • l- used to setmargin-leftorpadding-left
  • r- used to setmargin-rightorpadding-right
  • x- used to set*-leftand*-right
  • y- used to set*-topand*-bottom
  • blank - used to set the element in four directionsmarginorpadding

sizerefers to the size of the spacing:

  • 0- setmarginorpaddingis0
  • 1- setmarginorpaddingis$spacer * .25
  • 2- setmarginorpaddingis$spacer * .5
  • 3- setmarginorpaddingis$spacer
  • 4- setmarginorpaddingis$spacer * 1.5
  • 5- setmarginorpaddingis$spacer * 3
  • auto- setmarginto auto

marginmarginYou 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)

See the source code settings for some 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 the 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">Colors:</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 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-*-# Use sides to set padding 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
Other extensions