Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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,650 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 :)