Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Vertically center text in a div

I'm trying to vertically center text in a div but cannot seem to figure out how. I used text-align to horizontally center it, but vertical-alignment doesn't seem to work.

Does anyone know the proper way to vertically align text?

Guil Hernandez
Guil Hernandez
Treehouse Teacher

Hey Carlos Favela,

Take a look: http://codepen.io/Guilh/pen/ExuLG

I used display: table and display: table-cell with vertical-align: middle to make it happen.

5 Answers

Use Guil Hernandez's suggestion, it requires the least amount of code and least amount of hackiness.

It's very tough. There isn't really a great way to do with CSS like there is for center align, that I am aware of anyway. Here is a good description of vertical align. http://css-tricks.com/what-is-vertical-align/

I've seen CSS ways to do this but it seemed rather complicated, almost "hacky". I'm not sure what exactly qualifies as a CSS hack

I had the same problem and currently my favorite way to solve this problem has been to use midway.js. It's very clean, simple to use, minimal markup, and seems to be pretty reliable for the 98% or so that do have javascript enabled.

The plugin can be found at http://shipp.co/midway/

Hope that helps.

Line height. Set it to the height of the div and it'll get it somewhere close, then tweak till it's perfect.

Thanks a lot for all your guys help!

I will go Guil's route since I can understand it. :-)