Utilities (3): Flex / Float / Image replacement
Bootstrap 4 fully utilizes CSS3 flexbox. About Flexbox, see A complete guide to Flexbox.
Each notation maps to a corresponding CSS property name/value/value. See Flexbox (W3C) or Flexbox (MDN Glossary) for detail.
Sets display
property (⇒ Display utility)
.d(-sm|-md|-lg|-xl)?-(flex|inline-flex)
Sets flex-direction
property
.flex(-sm|-md|-lg|-xl)?-(row|column)(-reverse)?
Sets justify-content
property
.justify-content(-sm|-md|-lg|-xl)?-(start|end|center|between|around)
Sets align-items
property
.align-items(-sm|-md|-lg|-xl)?-(start|end|center|baseline|stretch)
Sets align-self
property
.align-self(-sm|-md|-lg|-xl)?-(start|end|center|baseline|stretch)
Sets flex
property (to flex: 1 1 auto;
)
.flex(-sm|-md|-lg|-xl)?-fill
Sets flex-(grow|shrink)
property
.flex(-sm|-md|-lg|-xl)?-(grow|shrink)-(0|1)
Sets flex-wrap
property
.flex(-sm|-md|-lg|-xl)?-(nowrap|wrap|wrap-reverse)
Sets order
property
.order(-sm|-md|-lg|-xl)?-(0|1|2|3|4|5|6|7|8|9|10|11|12|first|last)
Sets align-content
property
.align-content(-sm|-md|-lg|-xl)?-(start|end|center|between|around|stretch)
Auto margin
.m(l|t|r|b)?-auto
auto
(⇒ Spacing , not a part of flexbox utility). But it is explained here as it is frequently used in combination with flex utility..flex(-sm|-md|-lg|-xl)?-(row|column)(-reverse)?
div.d-flex.flex-row
div.d-flex.flex-row-reverse
div.d-flex.flex-column
div.d-flex.flex-column-reverse
.justify-content(-sm|-md|-lg|-xl)?-(start|end|center|between|around)
.d-flex.justify-content-start
.d-flex.justify-content-end
.d-flex.justify-content-center
.d-flex.justify-content-between
.d-flex.justify-content-around
.align-items(-sm|-md|-lg|-xl)?-(start|end|center|baseline|stretch)
In examples below, parent box heights are fixed to 5rem
.
.d-flex.align-items-start
.d-flex.align-items-end
.d-flex.align-items-center
.d-flex.align-items-baseline
.d-flex.align-items-stretch
.align-self(-sm|-md|-lg|-xl)?-(start|end|center|baseline|stretch)
In examples below, parent box heights are fixed to 5rem
.
.align-self-start
to the central child.
.align-self-end
to the central child.
.align-self-center
to the central child.
.align-self-baseline
to the central child.
.align-self-stretch
to the central child.
.flex(-sm|-md|-lg|-xl)?-fill
Use .flex-fill
to fill flex items into the parent. Each item's width (or height) is grown by its content width.
Example: .flex-fill
to each flex item
.flex(-sm|-md|-lg|-xl)?-(grow|shrink)-(0|1)
CSS flex-grow
property is set to 0
as default (i.e. does not grow). You can use .flex-grow-1
(sets to flex-grow: 1
) to fill available width (or height) to the specific item.
flex-grow: 0
).flex-grow-1
flex-grow: 0
)flex-grow: 0
)CSS flex-shrink
property is set to 1
as default (i.e. shrinks with browser-default ratio if needed). You can use .flex-shrink-0
(sets to flex-shrink: 0
) to set the specific item that does not shrink.
.w-100
.flex-shrink-0
(does not shrink).flex-shrink-1
.m(l|t|r|b)?-auto
Default
.mr-auto
to item 1
.mr-auto
.ml-auto
to item 3
.ml-auto
.flex-column.align-items-start
+ style="height:10rem"
to parent box.mb-auto
to item 1.mb-auto
.flex-column.align-items-end
+ style="height:10rem"
to parent box.mt-auto
to item 3.mt-auto
.flex(-sm|-md|-lg|-xl)?-(nowrap|wrap|wrap-reverse)
In examples blow, parent box width is fixed to 15rem
with lightgray background.
.d-flex.flex-nowrap
.d-flex.flex-wrap
.d-flex.flex-wrap-reverse
.order(-sm|-md|-lg|-xl)?-(0|1|2|3|4|5|6|7|8|9|10|11|12|first|last)
Example: DOM order is item-(1,2,3,4,5), .order-*
is (4,2,5,1,3).
.order-4
.order-2
.order-5
.order-1
.order-3
.order-(first|last)
(not explaind here) are also available..align-content(-sm|-md|-lg|-xl)?-(start|end|center|between|around|stretch)
Combination examples with .flex-wrap
.
div.flex-wrap
.align-content-(start|end|center|...)
style="width: 300px; height: 150px;"
(fix width and height)div
(×8).align-content-start.flex-wrap
.align-content-end.flex-wrap
.align-content-center.flex-wrap
.align-content-between.flex-wrap
.align-content-around.flex-wrap
.align-content-stretch.flex-wrap
Utility classes for floating an element to the left or right.
.float-(left|right|none)
img.float-left
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
img.float-right
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
img.float-none
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
.float-(sm|md|lg|xl)-(left|right|none)
.float-sm-left
Floats left on width≧576px
Plain text
.float-sm-right
Floats right on width≧576px
Plain text
.float-md-left
Floats left on width≧768px
Plain text
.float-md-right
Floats right on width≧768px
Plain text
.float-lg-left
Floats left on width≧992px
Plain text
.float-lg-right
Floats right on width≧992px
Plain text
.float-xl-left
Floats left on width≧1200px
Plain text
.float-xl-right
Floats right on width≧1200px
Plain text
Utility class for using a replacement image for text content (example setting as follows).
h1.text.hide
background-image: url("../img/sample-logo.png");
width: 224px;
height: 48px;
Sample Logo
Example