r/WebdevTutorials • • Mar 01 '21

Frontend 5 Simple ways to center a div using CSS

https://www.codingflicks.com/2021/02/5-ways-to-center-div-using-css.html
35 Upvotes

4 comments sorted by

6

u/og-at Mar 01 '21

The CSS grid version can be way simpler. On the parent

display: grid;
place-items: center;

and no requirements on the child.

On css-tricks.

1

u/webdep Mar 02 '21

Thanks

5

u/icewaterJS Mar 01 '21

Idk if you made this but the very first method (Flexbox) shows the code snippet from the second method (Tables).

Assuming empty divs it should be...

.parent {
  width: 400px;
  height: 400px;
  background: black;
  display: flex;
  justify-content: center;
  align-items: center;  
}

.child {
  width: 100px;
  height: 100px;  
  background: deepskyblue;
}

2

u/webdep Mar 02 '21

thanks