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

Question about divs when displayed inline-block

So, I've run into this before, but perhaps someone could provide an explanation for me.

When I have two divs that have display: inline-block;, and the first has content but the second doesn't, the first div's position goes out of line with the second, as shown in this codepen. Can anyone explain to me why this happens? As soon as content is added to the second div, they realign themselves nicely again.

1 Answer

The divs aren't the problem. It's actually the text inside the div that has default styles applied.

Oh, I see, so the text is trying to be in line with the next div, whose bottom is further down than it, so it pushes the entire div down? I suppose that makes sense. In one of my side projects, I actually had a table in the second div, and it was doing this, so I was even more confused since both had content. Thanks for the answer! :)

I've noticed that vertical-align will fix the issue, but I was wondering why it happened in the first place, and your answer makes sense!

No prob, man. Happy coding.