oren — 5/13/2022, 8:16:42 PM

Spent 30 minutes trying to center a div vertically and i finally gave up

♥ 3 ↩ 1 💬 13 comments

comments

codelikecraze:

you can center both at the same time with position : fixed; left : 50%; top : 50%; transform : translate(-50%,-50%);

5/13/2022, 9:08:38 PM
oren:

yes tysm this works!

5/13/2022, 9:18:05 PM
micahlt:

Please don't do this in most cases - it gets really messy! Instead make the parent element have 100% width and 100vw height, set the display to grid, and use place-items: center

5/14/2022, 4:02:21 AM
riverbox:

No, the best solution is just `margin: auto;`

5/14/2022, 11:36:42 AM
micahlt:

Margin: auto will only center horizontally in 90% of cases.

5/14/2022, 4:24:03 PM
oren:

this makes the h1 be at the top and the form be at the bottom

5/14/2022, 2:07:33 PM
oren:

formatting be like

5/14/2022, 2:07:45 PM
micahlt:

Ah, I didn't know that there are two child elements. Your best solution then would be these styles in the parent:

display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
5/14/2022, 4:25:52 PM
oren:

that works, thanks

5/14/2022, 6:28:46 PM
doubledenial:

That’s my go-to solution for total centering

5/14/2022, 11:56:50 PM
micahlt:

It's definitely the cleanest way if you don't want to take elements out of the layout flow :)

5/15/2022, 2:55:50 AM
riverbox:

i always do

margin: auto;
5/13/2022, 8:19:52 PM
lily:

howtocenterincss.com

5/13/2022, 8:18:45 PM