Answer: How to center an element horizontally and vertically

rnagarajan96

Nagarajan R

Posted on September 2, 2020

Answer: How to center an element horizontally and vertically

CSS Grid: place-items

Finally, we have place-items: center for CSS Grid to make it easier.

HTML

<div class="parent"&gt
  <div class="to-center"></div>
</div>

CSS

.parent {
  display: grid;
  place-items: center;
}

Output:

html,
body {
  height: 100%;
}

.container {
  display: grid;
  place-items: center;
  height: 100%;
}

.center {
  background:
💖 💪 🙅 🚩
rnagarajan96
Nagarajan R

Posted on September 2, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related