Categories
Bootstrap HTML

Bootstrap 5 — Flexbox Utilities

Spread the love

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to use flexbox classes with Bootstrap 5.

Flex

Bootstrap 5 provides us with classes to let us use flexbox easily.

For example, we can write:

<div class="d-flex p-2 bd-highlight">flexbox container</div>

to add the d-flex prop to make our div display: flex .

Other variations of the d-flex class includes:

  • .d-flex
  • .d-inline-flex
  • .d-sm-flex
  • .d-sm-inline-flex
  • .d-md-flex
  • .d-md-inline-flex
  • .d-lg-flex
  • .d-lg-inline-flex
  • .d-xl-flex
  • .d-xl-inline-flex
  • .d-xxl-flex
  • .d-xxl-inline-flex

Direction

We can change the direction of the flexbox layout by adding more classes.

For example, we can write .flex-row to flex horizontally.

And .flex-row-reverse will make the flex layout start from the right instead of the left.

So we can write:

<div class="d-flex flex-row bd-highlight mb-3">  
  <div class="p-2 bd-highlight">Flex item 1</div>  
  <div class="p-2 bd-highlight">Flex item 2</div>  
  <div class="p-2 bd-highlight">Flex item 3</div>  
</div>  
<div class="d-flex flex-row-reverse bd-highlight">  
  <div class="p-2 bd-highlight">Flex item 1</div>  
  <div class="p-2 bd-highlight">Flex item 2</div>  
  <div class="p-2 bd-highlight">Flex item 3</div>  
</div>

to add them.

The first div will stick to the left edge.

And the 2nd one will start from the right edge and displayed in reversed order.

We can use the .flex-column to set a vertical direction.

.flex-column-reverse starts the vertical direction from the opposite side.

We can add that with:

<div class="d-flex flex-column bd-highlight mb-3">  
  <div class="p-2 bd-highlight">Flex item 1</div>  
  <div class="p-2 bd-highlight">Flex item 2</div>  
  <div class="p-2 bd-highlight">Flex item 3</div>  
</div>  
<div class="d-flex flex-column-reverse bd-highlight">  
  <div class="p-2 bd-highlight">Flex item 1</div>  
  <div class="p-2 bd-highlight">Flex item 2</div>  
  <div class="p-2 bd-highlight">Flex item 3</div>  
</div>

The following are also available:

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

Justify Content

We can space out our content by using the justify-content classes.

The suffix can be end , center , between , around and evenly .

start is the default value. The items start from the left edge.

end makes items start from the right edge.

center make them centered.

between make them displayed with space in between.

around make them display with space on both sides.

evenly make them spread out evenly.

For example, we can write:

<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>  
<div class="d-flex justify-content-evenly">...</div>

to use those classes.

Other choices include:

  • .justify-content-start
  • .justify-content-end
  • .justify-content-center
  • .justify-content-between
  • .justify-content-around
  • .justify-content-evenly
  • .justify-content-sm-start
  • .justify-content-sm-end
  • .justify-content-sm-center
  • .justify-content-sm-between
  • .justify-content-sm-around
  • .justify-content-sm-evenly
  • .justify-content-md-start
  • .justify-content-md-end
  • .justify-content-md-center
  • .justify-content-md-between
  • .justify-content-md-around
  • .justify-content-md-evenly
  • .justify-content-lg-start
  • .justify-content-lg-end
  • .justify-content-lg-center
  • .justify-content-lg-between
  • .justify-content-lg-around
  • .justify-content-lg-evenly
  • .justify-content-xl-start
  • .justify-content-xl-end
  • .justify-content-xl-center
  • .justify-content-xl-between
  • .justify-content-xl-around
  • .justify-content-xl-evenly
  • .justify-content-xxl-start
  • .justify-content-xxl-end
  • .justify-content-xxl-center
  • .justify-content-xxl-between
  • .justify-content-xxl-around
  • .justify-content-xxl-evenly

Conclusion

We can use flexbox with Bootstrap 5’s provided classes.

Items can be aligned and justified our way with them.

Leave a Reply

Your email address will not be published. Required fields are marked *