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
Luke Glazebrook
13,564 PointsSome problems with CSS alignment.
I am working on a personal project and for some reason cannot figure out why the images won't line up with the centre of the page. They are all slightly too far to the left.
I have been trying to figure this out for a little bit but for some reason I just cannot seem to crack it. I may be missing something really simple but I just can't see it.
Thank you ever so much to anyone who helps!
Gallery.html
<div id="galleryArea">
<ul id="gallery">
<a href="#"><li>
<img src="img/img.png" />
<p>Desc...</p>
</li></a>
<a href="#"><li>
<img src="img/img.png" />
<p>Desc...</p>
</li></a>
<a href="#" class="clearFloat"><li>
<img src="img/img.png" />
<p>Desc...</p>
</li></a>
<a href="#"><li>
<img src="img/img.png" />
<p>Desc...</p>
</li></a>
<a href="#"><li>
<img src="img/img.png" />
<p>Desc...</p>
</li></a>
<a href="#"><li>
<img src="img/img.png" />
<p>Desc...</p>
</li></a>
</ul>
</div>
Main.css
#galleryArea {
display: inline-block;
width: 90%;
margin: 0 auto;
}
#gallery {
list-style:none;
}
#gallery img {
width: 100%;
height: auto;
max-width: 250px;
max-height: 250px;
}
#gallery li {
float: left;
width: 25%;
margin: 0 1.6%;
text-align: center;
}
#gallery a {
color: black;
}
3 Answers
Ryan Duchene
Courses Plus Student 46,022 PointsThe short, quick answer is that the margin you've given your li elements is causing them to be wider than what you think they are. They are, in fact, 25% wide, but the margin adds 3.2% width to that (1.6% on each side), so the fourth item ends up flowing down to the second row, causing them to appear partially aligned to the left, but not completely.
To fix this, swap your margin property out for padding, and add this snippet to the top of your stylesheet:
*,
* > *:before,
* > *:after {
box-sizing: border-box;
}
This snippet changes the CSS box model for everything on the page so that the width property includes not only the explicit width you define, but also the width of any padding and border you give the element. Thus, the browser will know to include your padding in your width when it calculates the true width of your gallery item. If you're confused about any of this, CSS tricks has a page dedicated to explaining it.
(In case you're wondering, using the * selector is usually not recommended, but in this case, it's fine.)
You have a couple of additional problems here. First and foremost, the ul and ol elements cannot contain any elements other than li, and li elements may only be placed inside ul and ol elements (and the menu element, to be precise, but that's for another day). You can solve this by placing your a tags inside your li tags instead of outside. You may also have to adjust your styles accordingly.
Second, a elements display as inline elements by default. This means that they will stick to the left no matter what, and that you won't be able to give them top or bottom margin. Try setting their display to block. This may not fix the problem you're having, but it will probably help with other issues down the road.
Third, the float: left in your #gallery is ringing the clearfix alarm in my head. Since your #gallery contains only floated child elements, it will collapse to a height of 0. The same thing will happen to your #galleryArea. If this is your first time hearing about it, it's a legendary bug with floats that's been around for a decade or so. There are a lot of ways to fix this one: just Google clearfix and you should come up with a lot. This one seems pretty legit to me:
.clearfix:after {
content: "";
display: table;
clear: both;
}
To use it, just give your #gallery and #galleryArea elements the clearfix class.
Finally, your #gallery-area should really be display: block. Otherwise, the margin: 0 auto trick won't work, as inline-block elements do not support the auto value for the margin property.
In case you're confused by any of that, here's a CodePen to help you out. Hope that helps!
John Norris
22,455 PointsTry replacing
#galleryArea {
display: inline-block;
width: 90%;
margin: 0 auto;
}
With
#galleryArea {
display: block;
width: 90%;
margin-left: auto;
}
As seen here...
#galleryArea {
display: block;
width: 90%;
margin-left: auto;
}
#gallery {
list-style:none;
}
#gallery img {
width: 100%;
height: auto;
max-width: 250px;
max-height: 250px;
}
#gallery li {
float: left;
width: 25%;
margin: 0 1.6%;
text-align: center;
}
#gallery a {
color: black;
}
ul{
margin-left:auto;
margin-right:auto;
}
See it in action here: http://jsfiddle.net/eaf64k4a/
Luke Glazebrook
13,564 PointsThanks so much I really appreciate you taking your time to help me out!
Hope you're having a great day.
Aaron Gill-Braun
180 Pointsi think you have to set the width to 80% because it can be centered with 10% space on each size?
Luke Glazebrook
13,564 PointsIt's 90% so it can have 5% on each side.
Luke Glazebrook
13,564 PointsLuke Glazebrook
13,564 PointsThis answer is so awesome, thanks for all the help!
I have only just got back into HTML/CSS after not having done it for a long time so all of this stuff is slowly coming back to me.
I will take a look at the CodePen and try some of the fixes you suggested and let you know how it goes.
Thanks!
Ryan Duchene
Courses Plus Student 46,022 PointsRyan Duchene
Courses Plus Student 46,022 PointsNo problem! I'm actually just coming back from a hiatus too. Glad I could help!
Luke Glazebrook
13,564 PointsLuke Glazebrook
13,564 PointsWell I wish you the very best of luck dude! Once again, thanks a bunch for the help.