how to center a div flexbox code example

Example 1: css flex center

display: flex;
align-items: center;
justify-content: center;

Example 2: css flex vertical align

/* Answer to: "css flex vertical align" */

.box {
  display: flex;
  align-items: center; /* Vertical */
  justify-content: center; /* Horizontal */
}

.box div {
  width: 100px;
  height: 100px;
}

/*
  For more information go to:
  https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
*/

Example 3: centering with flexbox

/* HORIZONTAL */
justify-content: center;

/* VERTICAL */
align-items: center;

Example 4: center div css flex

section {
  width: 200px;
  border: 1px solid #2d2d2d;
  display: flex;
  justify-content: center;
}

Example 5: how to align a flex container center

.table-wrapper {
  display: flex;
  flex-direction: column;
  padding: 2rem;
  margin: 0 auto;
}

Tags:

Misc Example