Advertisement

Google Ad Slot: content-top

Bootstrap 5 Flex Container

~1 min read · Bootstrap
On this page

flex container enables a flexbox layout, which provides powerful and efficient ways to align and distribute space among items in the container, even when their sizes are dynamic or unknown.

Warning

Some HTML elements have no content (like the <br→ element). These elements are called empty elements. Empty elements do not have an end tag!

Flex Direction:

Control the direction of flex items:


Syntax:

flex-{size?}-{direction}
  • flex: Indicates flex.
  • size (optional): Responsive breakpoint (e.g., sm : ≥576pxmd : ≥ 768pxlg : ≥992pxxl : ≥1200pxxxl : ≥1400px).
  • direction : Defines the direction of the margin (e.g., rowrow-reversecolumncolumn-reverse).


Example:

  • .flex-row
  • .flex-sm-row
  • .flex-md-row
  • .flex-lg-row
  • .flex-xl-row
  • .flex-xxl-row

Class

Description

flex-row

Horizontal (default).

flex-row-reverse

Reverse horizontal.

flex-column

Vertical direction.

flex-column-reverse

Reverse vertical.

Example
Row Item 1
Row Item 2
Try it yourself

Flex Wrap:

Control whether flex items wrap onto multiple lines.


Syntax:

flex-{size?}-{type}
  • flex: Indicates flex.
  • size (optional): Responsive breakpoint (e.g., sm : ≥576pxmd : ≥ 768pxlg : ≥992pxxl : ≥1200pxxxl : ≥1400px).
  • type : Defines the direction of the margin (e.g., wrapwrap-reversenowrap).


Example:

  • .flex-wrap
  • .flex-sm-wrap
  • .flex-md-wrap
  • .flex-lg-wrap
  • .flex-xl-wrap
  • .flex-xxl-wrap

Class

Description

flex-wrap

Wrap items onto a new line.

flex-wrap-reverse 

Wrap in reverse.

flex-nowrap

Prevent wrapping.

Example
Item 1
Item 2
Item 3
Try it yourself

Justify Content:

Align flex items horizontally along the main axis.


Syntax:

justify-content-{size?}-{type}
  • justify-content: Indicates justify-content.
  • size (optional): Responsive breakpoint (e.g., sm : ≥576pxmd : ≥ 768pxlg : ≥992pxxl : ≥1200pxxxl : ≥1400px).
  • type : Defines the direction of the margin (e.g., startendcenterbetween,around,evenly).


Example:

  • .justify-content-start
  • .justify-content-sm-start
  • .justify-content-md-start
  • .justify-content-lg-start
  • .justify-content-xl-start
  • .justify-content-xxl-start

Class

Description

justify-content-start

Align items to the start.

justify-content-end

Align items to the end.

justify-content-center

Align items to the center.

justify-content-between

Space between items.

justify-content-around

Space around items.

justify-content-evenly

Space evenly items.

Example
Item 1
Item 2
Try it yourself

Align Items:

Align items vertically along the cross-axis.


Syntax:

align-items-{size?}-{type}
  • align-items: Indicates align-items.
  • size (optional): Responsive breakpoint (e.g., sm : ≥576pxmd : ≥ 768pxlg : ≥992pxxl : ≥1200pxxxl : ≥1400px).
  • type : Defines the direction of the margin (e.g., startendcenterbaseline,stretch).


Example:

  • .align-items-start
  • .align-items-sm-start
  • .align-items-md-start
  • .align-items-lg-start
  • .align-items-xl-start
  • .align-items-xxl-start

Class

Description

align-items-start

Align to the start.

align-items-end

Align to the end.

align-items-center

Align to the center.

align-items-baseline

Align to the baseline.

align-items-stretch

Stretch items (default).

Example
Item 1
Item 2
Try it yourself

Align Content:

Align items vertically along the cross-axis.


Syntax:

align-content-{size?}-{type}
  • align-content: Indicates align-content.
  • size (optional): Responsive breakpoint (e.g., sm : ≥576pxmd : ≥ 768pxlg : ≥992pxxl : ≥1200pxxxl : ≥1400px).
  • type : Defines the direction of the margin (e.g., startendcenteraround,stretch).


Example:

  • .align-content-start
  • .align-content-sm-start
  • .align-content-md-start
  • .align-content-lg-start
  • .align-content-xl-start
  • .align-content-xxl-start

Class

Description

align-content-start

Align rows at start.

align-content-end

Align rows at end.

align-content-center

Align rows at center.

align-content-between

Space between rows.

align-content-around

Space around rows.

align-content-stretch

Stretch rows.

Example
Item 1
Item 2
Try it yourself

Gap:

Add spacing between flex items using gap classes:


Syntax:

gap-{size}
  • gap: Indicates gap.
  • size (optional): Responsive breakpoint (e.g., 01234):

Class

Description

gap-0

0px

gap-1

4px

gap-2

8px

gap-3

16px

gap-4

24px

gap-5

32px

Example
Item 1
Item 2
Try it yourself