What's the best way to center a div both horizontally and vertically using css? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the best way to center a div both horizontally and vertically using css?

I've always found many solutions but the most of them never worked for me or they not totally satisfy my needs :/

14th Aug 2017, 8:55 PM
Mattia Meneghetti
Mattia Meneghetti - avatar
3 Answers
0
horizontal-align and vertical-align do not satisfy you? maybe try with margin="auto" too but for the upper div
14th Aug 2017, 9:53 PM
Paul
0
Well, I have no problem in center an element horizontally, but I always find difficult to center it vertically because 90% of times I use vertical-align it doesn't work. In my last code, for example, I had to modify paddings. That's why I'm asking.
14th Aug 2017, 11:04 PM
Mattia Meneghetti
Mattia Meneghetti - avatar
0
If i remember correctly if you set the line spacing to = the height of the parent div that might work. Apologies if it doesn't
14th Aug 2017, 11:44 PM
James Cooke