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

Bootstrap4 Flex Layout

Bootstrap4 Flex Layout

Bootstrap4 controls page layout through flex classes.


Flexbox

The biggest difference between Bootstrap 3 and Bootstrap 4 is that Bootstrap 4 uses flexbox for layout instead of floats.

Flexbox is a new layout mode in CSS3, better suited to responsive design. If you don't know flex yet, you can read our CSS3 Flexible Box (Flex Box) Tutorial

Note: IE9 and below do not support flexbox, so if you need compatibility with IE8-9, please use Bootstrap 3。

The following example uses the d-flex class to create a flexbox container and sets three flex items:

Example

<div class=">

d-flex p-3 bg-secondary text-white

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

Try it »

To create a flexbox container displayed on the same line, you can use the d-inline-flex class:

Example

<div class=">

d-inline-flex p-3 bg-secondary text-white

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

Try it »


Horizontal Direction

.flex-row sets flex items to display horizontally, which is the default.

The .flex-row-reverse class is used to set right-aligned display, i.e. the opposite direction of .flex-row.

Example

<div class=">

d-flex flex-row bg-secondary

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

<div class=">

d-flex flex-row-reverse bg-secondary

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

Try it »


Vertical Direction

The .flex-column class sets flex items to display vertically, and .flex-column-reverse reverses the items:

Example

<div class=">

d-flex flex-column

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

<div class=">

d-flex flex-column-reverse

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

Try it »


Content Alignment

The .justify-content-* classes are used to change the arrangement of flex items; the allowed values for * are:start (default), end, center, between or around:

Example

<div class=">

d-flex justify-content-start

"

...

div

<div class=">

d-flex justify-content-end

"

...

div

<div class=">

d-flex justify-content-center

"

...

div

<div class=">

d-flex justify-content-between

"

...

div

<div class=">

d-flex justify-content-around

"

...

div

Try it »


Monospace

The .flex-fill class forces all flex items to have the same width:

Example

<div class="d-flex">
<div class=">

p-2 bg-info flex-fill

"

Flex item 1

div

<div class=">

p-2 bg-warning flex-fill

"

Flex item 2

div

<div class=">

p-2 bg-primary flex-fill

"

Flex item 3

div

div

Try it »


Extend

.flex-grow-1 is used to make an item use the remaining space. In the following example, the first two items only take the space they need, and the last one takes the remaining space.:

Example

<div class="d-flex">
<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary flex-grow-1

"

Flex item 3

div

div

Try it »

Tip: Use .flex-shrink-1 to set the shrink rule of an item.


Sorting

The .order class can set the ordering of flex items, from .order-1 to .order-12; the lower the number, the higher the priority (.order-1 comes before .order-2):

Example

<div class=">

d-flex bg-secondary

"

<div class=">

p-2 bg-info order-3

"

Flex item 1

div

<div class=">

p-2 bg-warning order-2

"

Flex item 2

div

<div class=">

p-2 bg-primary order-1

"

Flex item 3

div

div

Try it »


Margin

The .mr-auto class can set an item's right margin to auto, i.e. margin-right: auto!important;. The .ml-auto class can set an item's left margin to auto, i.e. margin-left: auto!important;:

Example

<div class=">

d-flex bg-secondary

"

<div class=">

p-2 mr-auto bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 bg-primary

"

Flex item 3

div

div

<div class=">

d-flex bg-secondary

"

<div class=">

p-2 bg-info

"

Flex item 1

div

<div class=">

p-2 bg-warning

"

Flex item 2

div

<div class=">

p-2 ml-auto bg-primary

"

Flex item 3

div

div

Try it »


Wrapping

To wrap items in a flex container, you can use the following three classes: .flex-nowrap (default), .flex-wrap or .flex-wrap-reverse. They set whether the flex container is single-line or multi-line.

Example

<div class=">

d-flex flex-wrap

"

..

div

<div class=">

d-flex flex-wrap-reverse

"

..

div

<div class=">

d-flex flex-nowrap

"

..

div

Try it »


Content Alignment

We can use .align-content-* to control how items are stacked in the vertical direction. The values include: .align-content-start (default), .align-content-end, .align-content-center, .align-content-between, .align-content-around and .align-content-stretch.

These classes have no effect when there is only a single line of flex items.

Example

<div class=">

d-flex flex-wrap align-content-start

"

..

div

<div class=">

d-flex flex-wrap align-content-end

"

..

div

<div class=">

d-flex flex-wrap align-content-center

"

..

div

<div class=">

d-flex flex-wrap align-content-around

"

..

div

<div class=">

d-flex flex-wrap align-content-stretch

"

..

div

Try it »


Item Alignment

To set the alignment of items on a single line, you can use the .align-items-* classes. The values include:.align-items-start, .align-items-end, .align-items-center, .align-items-baseline, and .align-items-stretch (default)。

Example

<div class=">

d-flex align-items-start

"

..

div

<div class=">

d-flex align-items-end

"

..

div

<div class=">

d-flex align-items-center

"

..

div

<div class=">

d-flex align-items-around

"

..

div

<div class=">

d-flex align-items-stretch

"

..

div

Try it »


Aligning a Specific Item

To set the alignment of a specific item, you can use the .align-self-* classes. The values include: .align-self-start, .align-self-end, .align-self-center, .align-self-baseline, and .align-self-stretch (default).

Example

<div class=">

d-flex bg-light

"

style

=

"

height:150px

"

<div class=">

p-2 border

"

Flex item 1

div

<div class=">

p-2 border align-self-start

"

Flex item 2

div

<div class=">

p-2 border

"

Flex item 3

div

div

Try it »


Responsive Flex Classes

We can set flex classes according to different devices to achieve responsive page layout. The possible values of * in the table below are: sm, md, lg or xl, corresponding to small, medium, large and extra-large devices.

class Example Create a class that implements
Flex Containers
.d-*-flex Create a flexbox container for different screen devices Try it
.d-*-inline-flex Create an inline flexbox container for different screen devices Try it
Direction
.flex-*-row Display flex items horizontally for different screen devices Try it
.flex-*-row-reverse Display flex items horizontally and right-aligned for different screen devices Try it
.flex-*-column Display flex items vertically for different screen devices Try it
.flex-*-column-reverse Display flex items vertically in reverse order for different screen devices Try it
Sorting
.order-*-0-12 Change ordering on small screen sizes Try it
Content Alignment
.justify-content-*-start Display flex items at the start (left-aligned) for different screen devices Try it
.justify-content-*-end Display flex items at the end (right-aligned) for different screen devices Try it
.justify-content-*-center Center items in a flex container for different screen devices Try it
.justify-content-*-between Display flex items using "between" for different screen devices Try it
.justify-content-*-around Display flex items using "around" for different screen devices Try it
Monospace
.flex-*-fill Force equal widths for different screen devices Try it
Extend
.flex-*-grow-0 Do not set growth for different screen devices  
.flex-*-grow-1 Set growth for different screen devices  
Shrinking
.flex-*-shrink-0 Do not set shrinking for different screen devices  
.flex-*-shrink-1 Set shrinking for different screen devices  
Wrapping
.flex-*-nowrap Do not wrap items for different screen devices Try it
.flex-*-wrap Wrap items for different screen devices Try it
.flex-*-wrap-reverse Reverse wrapping of items for different screen devices Try it
Content Alignment
.align-content-*-start Stack items at the start position for different screen devices Try it
.align-content-*-end Stack items at the end position for different screen devices Try it
.align-content-*-center Stack items in the middle for different screen devices Try it
.align-content-*-around Stack items using "around" for different screen devices Try it
.align-content-*-stretch Stack by stretching items for different screen devices Try it
Item Alignment
.align-items-*-start For different screen devices, display items at the start on the same line. Try it
.align-items-*-end For different screen devices, display items at the end on the same line. Try it
.align-items-*-center For different screen devices, display items in the middle on the same line. Try it
.align-items-*-baseline For different screen devices, display items on the baseline on the same line. Try it
.align-items-*-stretch For different screen devices, stretch the items' height and display them on the same line. Try it
Alignment of a Single Item
.align-self-*-start For different screen devices, display a single item at the start. Try it
.align-self-*-end For different screen devices, display a single item at the end Try it
.align-self-*-center For different screen devices, display a single item in the center Try it
.align-self-*-baseline For different screen devices, display a single item at the baseline position Try it
.align-self-*-stretch For different screen devices, stretch a single item Try it