Bootstrap 4 tutorial

Utilities (3): Flex / Float / Image replacement

Flex

Bootstrap 4 fully utilizes CSS3 flexbox. About Flexbox, see A complete guide to Flexbox.

Notation summary

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

Horizontal/Vertical direction

.flex(-sm|-md|-lg|-xl)?-(row|column)(-reverse)?

div.d-flex.flex-row

Flex item 1
Flex item 2
Flex item 3

div.d-flex.flex-row-reverse

Flex item 1
Flex item 2
Flex item 3

div.d-flex.flex-column

Flex item 1
Flex item 2
Flex item 3

div.d-flex.flex-column-reverse

Flex item 1
Flex item 2
Flex item 3

Justify content

.justify-content(-sm|-md|-lg|-xl)?-(start|end|center|between|around)

.d-flex.justify-content-start

Flex item 1
Flex item 2
Flex item 3

.d-flex.justify-content-end

Flex item 1
Flex item 2
Flex item 3

.d-flex.justify-content-center

Flex item 1
Flex item 2
Flex item 3

.d-flex.justify-content-between

Flex item 1
Flex item 2
Flex item 3

.d-flex.justify-content-around

Flex item 1
Flex item 2
Flex item 3

Align items

.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

Flex item 1
Flex item 2
Flex item 3

.d-flex.align-items-end

Flex item 1
Flex item 2
Flex item 3

.d-flex.align-items-center

Flex item 1
Flex item 2
Flex item 3

.d-flex.align-items-baseline

Flex item 1
Flex item 2
Flex item 3

.d-flex.align-items-stretch

Flex item 1
Flex item 2
Flex item 3

Align self

.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.

Flex item
Aligned flex item
Flex item

.align-self-end to the central child.

Flex item
Aligned flex item
Flex item

.align-self-center to the central child.

Flex item
Aligned flex item
Flex item

.align-self-baseline to the central child.

Flex item
Aligned flex item
Flex item

.align-self-stretch to the central child.

Flex item
Aligned flex item
Flex item

Fill

.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

Short
Medium length text
This is a slightly longer text content

Grow and Shrink

.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.

Item (flex-grow: 0)
Item with .flex-grow-1
Item (flex-grow: 0)
Item (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.

Item with .w-100
Item with .flex-shrink-0 (does not shrink)
Item with .flex-shrink-1

Auto margins

.m(l|t|r|b)?-auto

Default

Flex item 1
Flex item 2
Flex item 3

.mr-auto to item 1

Flex item 1 .mr-auto
Flex item 2
Flex item 3

.ml-auto to item 3

Flex item 1
Flex item 2
Flex item 3 .ml-auto

Auto-margins + align-items

Example 1
Flex item 1 .mb-auto
Flex item 2
Flex item 3
Example 2
Flex item 1
Flex item 2
Flex item 3 .mt-auto

Wrapping

.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

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7

.d-flex.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7

.d-flex.flex-wrap-reverse

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7

Order

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

Item 1 .order-4
Item 2 .order-2
Item 3 .order-5
Item 4 .order-1
Item 5 .order-3

Align content

.align-content(-sm|-md|-lg|-xl)?-(start|end|center|between|around|stretch)

Combination examples with .flex-wrap .

.align-content-start.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8

.align-content-end.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8

.align-content-center.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8

.align-content-between.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8

.align-content-around.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8

.align-content-stretch.flex-wrap

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8

Float

Utility classes for floating an element to the left or right.

Classes

.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.

Responsive classes

.float-(sm|md|lg|xl)-(left|right|none)

Example

.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

Image replacement

Utility class for using a replacement image for text content (example setting as follows).

Example

Sample Logo