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

Vertical align

I want to know how to professionally vertical align elements here the code and my question :

    <header>
        <div id="header-content">
            <h1>Dada</h1>
            <p>Art movement</p>
        </div>
    </header>

i centered the text vertically like that :

     padding-top:20%;

it worked but it`s kinda messy i feel like its not the proper way to vertically align elements, specially that when i tried to use 50% it wasnt 50 at all, i also dont know why.

you want to vertically align the whole div ?

6 Answers

In that case this might be able to help you.

i want to center elements inside the div, i want the h1 and h2 to be centered vertically and horizontally, horinzontal align is not a problem since i can use text-align:center but what about the vertical align ?

is my question that hard ?

Both the h1 and p elements are centered vertically, as the div element is only as tall as the content within it demands it to be. Look at this codepen to see what I mean.

yea but if the header height is more then the content itself the content will stay the same, it will not be centered vertically

thank you mike i used the following :

  display:flex;
  justify-content: center;
  align-items: center;