Archived topic
How to vertically center a single div
5 replies · Started by Roberto Enrique on June 21, 2016
I'm on this situation:
https://i.imgur.com/JeZINcC.png
i need to center the "left" div-50 relatively to the parent grid container.
https://i.imgur.com/enCeWjU.png
I have been trying a lot of js tricks but none seems to work.
Do you have a "best-practice" to accomplish this or should I desist?
Thanks in advance
You might want to look into Flexbox - it's a pretty badass new method in HTML5/CSS3:
https://www.kirupa.com/html5/centering_vertically_horizontally.htm
https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/
https://davidwalsh.name/css-vertical-center-flexbox
Let me know if you need more info :)
Thanks!
It was so easy
I added a class to the grid-container
<div class="grid-container flex-vertical"></div>
and then the css:
.flex-vertical{
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
}
And it works beautifully
Flexbox is awesome. If you need support for old versions of IE, take a look at flexibility: https://github.com/10up/flexibility
Hey Tom, how does the "Vertically Align Content" feature work in the Page Header > Content options? When you switch it on, it seems to center any text vertically.
Could I somehow use this elsewhere by calling the class that is powering this option?
It's using flexbox.
This might help: https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/