How to Center in CSS with flex?

October 08, 2020

To wrap an item using div, just set display: flex; and justify-content: center;.

<div class="parent">

</div>
.parent {
  display: flex;
  justify-content: center;
}

If you’re using TailwindCSS, just add flex and justify-center classes:

<div class="flex justify-center">

</div>

or using Bootstrap, just add d-flex and justify-content-center classes:

<div class="d-flex justify-content-center">

</div>

Written by Murtaja Ziad, a full-stack web developer.
My website - Follow me on Twitter