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!

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


Vertical Align within a Div

What is the best way to vertically align elements within a div. For example a <h3> and <p> vertically centered within a <div>.

I have seen multiple ways to do this online, but I am wondering what you all think is the best way. This is just one of those aspects I never really mastered.

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Danielle,

The vertical-align property can only be applied to elements with a display level of inline or inline-block so you can't apply it to a div element which is an block level element, in saying that see the below example I created quickly showing you how you can use CSS tables to achieve vertical alignment.

Preview - Source

Thanks! The example helped :)