div center align code example

Example 1: css align center

//HTML
<div class="parent">
	<span>Hello World</span>
</div>

//CSS
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

Example 2: css align items vertical center

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

Example 3: css vertical center

/*Remove comment to become a better programmer. */
/* No Flexbox */
.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

/* With Flexbox */

.parent {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

Example 4: div align center

.center {
  text-align: center;
}

// or

<div style="text-align:center">
</div>

Example 5: how to center text in css

.class {
	text-align: center;
}

Example 6: how to center a div

.container {
  ...
  display: flex;
  justify-content: center;
}

Tags: