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
Stephen Blank
16,286 PointsVertical alignment using image helper span
I'm trying to get my list items to align vertically to the middle of a container, but I can't seem to get it to work right. I set up a jsfiddle to show the problem. What is causing the image to show up outside of the container rather than be vertically aligned within it?
Here is where I found the idea for the img-helper span: http://jsfiddle.net/kizu/4RPFa/4570/
3 Answers
Becky Castle
15,294 PointsHi Steve,
Haha, I have to admit that finding the answer to your second question wasn't as easy as I thought it was going to be. After fiddling awhile I learned that the vertical-align is actually meant for inline elements. (But even when I changed our elements to " display: inline/ display:inline-block " I still didn't get the response I was looking for.) However, thanks to some helpful advice at http://phrogz.net/css/vertical-align/index.html, I was able to find the solution -- which is quite different than I had originally posted.
I believe this css styling should get you the desired result:
ul.navlist {
height:200px;
width:100%;
background:lightblue;
position:relative;
margin: 210px auto 0 auto;
list-style-type:none;
}
.navlist img {
height:50%;
width:auto;
position:absolute;
top:25%;
}
Eric Ridpath
1,519 PointsIf you add text-align: center; to the .navlist class and change float: left; to float: none; on the .navitem class that should do the trick.
Becky Castle
15,294 PointsHi Steve,
It looks like this code got a little more complicated than it needed to be.
For me, I like to keep things simple and I try to avoid extra "helper" html markup if possible. To accomplish that in this instance, I'd focus on the fact that I have a < ul > tag, and nested inside of that I have an < li > tag, and then finally nested inside of that I have an < img >, which would look like this:
<ul class="navlist">
<li class="navitem">
<a href="#">
<img class="navimage" src="http://lorempixel.com/235/180/nature/1"/>
</a>
</li>
</ul>
Once you can picture what items are nested inside of what, the css styling becomes much more clear. Remember that your < ul > is the container for EACH of your (potential) < li >, and your < li > (the only one in this case) is the container for the < img >. And in order to style the img, you need to mention it specifically, like this: .navitem img {}.
So, to get the image to fit inside of the < li > and < ul > and align it vertically, your markup should look something like this:
.navlist {
height: 150px;
margin: 210px auto 0 auto;
background-color: lightblue;
padding: auto;
list-style-type:none;
}
li.navitem {
vertical-align:middle;
height:100%;
}
.navitem img {
height: 100%;
width: auto;
}
Stephen Blank
16,286 PointsThanks for your detailed response, Becky!
What would you do to vertically center the image, if the image were 50% of the height of the container?
I made a jsfiddle where the image is 50% height, but I don't know how to center it vertically without a helper of some sort: