How to vertically align any HTML element to the middle of its container? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to vertically align any HTML element to the middle of its container?

It is easy to horizontally align any HTML element using the CSS property "align", but how to do the same vertically using CSS?

24th Mar 2017, 11:10 PM
Ibrahim Abdul-Hakim
Ibrahim Abdul-Hakim - avatar
3 Answers
+ 3
assuming you have created the container and the element is in it, having top:50% and right:50% should suffice. You can also play around with the margins for more accuracy. Try building a container and set a background color for it and use different attributes for practice. It helps a lot
25th Mar 2017, 12:09 AM
Fotis Aronis
Fotis Aronis - avatar
+ 3
<div> <center><h1>Hello Salam </h1></center> </div>
27th Mar 2017, 5:13 PM
Amin Al olofi
Amin Al olofi - avatar
+ 2
This technique will center your content vertically. HTML: <div> <h1>Some Text</h1> </div> CSS: div { position: relative; width: 500px; height: 500px; } h1 { position: absolute; top: 50% transform: translateY(-50%); }
1st Apr 2017, 5:12 PM
Naeem Rind
Naeem Rind - avatar