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 align objects / paragraphs and nexted divs inside their containing div

I have div's set up to create background colors for sections of my website and then have a container div within each section div.

My question is how do i vertically align text (paragraphs) / objects (images) and nested div's (buttons) within the containing div?

<div class="section-one">
<div class="container">
<p>CONTENT I WANT VERTICALLY ALIGNED</p>
</div>
</div>

<div class="section-one">
<div class="container">
<div class="button">  - CONTENT I WANT VERTICALLY ALIGNED
</div>
</div>
</div>

<div class="section-one">
<div class="container">
<img src="image.jpg">   - CONTENT I WANT VERTICALLY ALIGNED
</div>
</div>

Do you mean you want each element inside the container to be vertical and 100% width of the container?

If this is the case you could do

    .button, img { display: block; } /* Just a quick way to do it. But not very specific with the img */ 

Hoope this helps

1 Answer

Do you mean something like this?

Imgur

If so here you go:

.container {
  display: table;
  text-align: center;
}

.copy {
  display: table-cell;
  vertical-align: middle;
}

Here's a working demo on codepen to play with