DEV Community

Alex Carpenter
Alex Carpenter

Posted on

9 3

Center an element with CSS Grid

It's not uncommon to go a week without a developer complaining on Twitter about how hard it is to center an element using CSS.

Here it is, two lines of CSS to center an element within its parent.

.parent {
  display: grid;
  place-items: center;
}
Enter fullscreen mode Exit fullscreen mode

It's beautiful.

Top comments (5)

Collapse
 
equinusocio profile image
Mattia Astorino • Edited

Place-items is just a shorthand of justify-items and align-items which work both with flexbox and grid layout. You should give more details next time. 🖖🏻

Collapse
 
hybrid_alex profile image
Alex Carpenter

Sure, it wasn't the most comprehensive post, but it does what is described.

Collapse
 
equinusocio profile image
Mattia Astorino

Yes. But let others learn something :)

Collapse
 
hybrid_alex profile image
Alex Carpenter

Yup 🤷‍♂️

Collapse
 
ohbob profile image
Roberts Ozoliņš

Thanks!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay