gengns ES

An entry from the gengns log, written by Génesis García Morilla · · CSS3

Horizontal and vertical center made easy

Forget about any complicated way, just use Flexbox and set up horizontal center and vertical center in the container.

HTML

<div></div>

CSS

body {
  background-color: #ccc;
  /* You need to define an area */
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  /* Horizontal and vertical center with Flexbox */
  display: flex;
  justify-content: center; /* horizontal center */
  align-items: center; /* vertical center */
}

div {
  background-color: #555;
  /* You need to define an area or at least put something within your item */
  width: 100px;
  height: 100px;
}
Open in JSFiddle

Tags: center, flexbox, horizontal, vertical