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
Ryan Field
Courses Plus Student 21,242 PointsQuestion 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
David Tonge
Courses Plus Student 45,640 PointsThe divs aren't the problem. It's actually the text inside the div that has default styles applied.
Ryan Field
Courses Plus Student 21,242 PointsRyan Field
Courses Plus Student 21,242 PointsOh, 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! :)
David Tonge
Courses Plus Student 45,640 PointsDavid Tonge
Courses Plus Student 45,640 Pointsadd this to your code.
vertical-align: middle;http://stackoverflow.com/questions/5932201/how-to-vertical-align-an-inline-block-in-a-line-of-text
Ryan Field
Courses Plus Student 21,242 PointsRyan Field
Courses Plus Student 21,242 PointsI've noticed that
vertical-alignwill fix the issue, but I was wondering why it happened in the first place, and your answer makes sense!David Tonge
Courses Plus Student 45,640 PointsDavid Tonge
Courses Plus Student 45,640 PointsNo prob, man. Happy coding.