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
Josh McMullin
3,012 PointsFloating individual images for an image gallery.
I'm currently working on building an image gallery and want to layout the photos with CSS in a bit of an unorganized manner. I've got a markup of what I'm looking to do here - http://www.gliffy.com/go/publish/6976777. Is there a best way to go about this?
3 Answers
Jeff Jacobson-Swartfager
15,419 PointsIf you'd like to use float, you can use margins to help you align each photo slightly differently within your page. Just make sure to keep collapsing margins in mind as you do this.
For example, if your html for your images looked like this:
<ul>
<li><img src="photo-1.jpg" alt="" /></li>
<li><img src="photo-2.jpg" alt="" /></li>
<li><img src="photo-3.jpg" alt="" /></li>
</ul>
You might do something like this in your CSS:
ul { list-style-type: none; }
li, img { float: left; }
li img[src="photo-1.jpg"] {
width: 100px;
height: 100px;
margin: 10px 20px 30px;
}
li img[src="photo-2.jpg"] {
width: 100px;
height: 100px;
margin: 0 20px 40px 0;
}
li img[src="photo-3.jpg"] {
clear: left;
width: 200px;
height: 100px;
margin: 10px auto;
}
Josh McMullin
3,012 PointsAnd for my CSS -
gallery {
display: block;
margin: 0;
padding: 0;
list-style-type: none;
}
li, img { float: left; }
li img[src="img/flowers0.jpg"] { width: 100%; height: auto; margin: 10px 20px 30px; }
li img[src="img/flowers1.jpg"] { width: 100%; height: auto; margin: 0 20px 40px 0; }
li img[src="..img/flowers2.jpg"] { clear: left; width: 100%; height: auto; margin: 10px auto;
}
li img[src="img/flowers3.jpg"] { clear: right; width: 100%; height: auto; margin: 40px 10px 1px 5px;
}
Josh McMullin
3,012 PointsI'm having trouble with the img/flowers3.jpg I can't get it to start below img/flowers2.jpg. Any ideas?
Josh McMullin
3,012 PointsThanks for all the feedback everyone. I really appreciate the help.
Josh McMullin
3,012 PointsJosh McMullin
3,012 PointsSo this is what I have for my HTML -
<ul id="gallery"> <li> <img src="img/flowers0.jpg" alt="flower">
</li> <li> <img src="img/flowers1.jpg" alt="flower"> </li> <li> <img src="img/flowers2.jpg" alt="flower"> </li> <li> <img src="img/flowers3.jpg" alt="flower"> </li>
Jeff Jacobson-Swartfager
15,419 PointsJeff Jacobson-Swartfager
15,419 PointsIf you want flowers3 to start on a new line, you'll have to apply the
clear:leftto theli. Unfortunately, since there are no parent selectors in CSS, that will make your html a little dirtier (or, you're going to need to add some javascript).Assuming the absence of javascript, your html will need to look something like this:
And then something like this for your css:
I noticed that you had some
..preceding your URI path for flowers2.jpg which I removed. I also refactored a bit.I haven't actually tested this to make sure it works, but it should get you going along the right path.
If you want to make use of the parent selector in javascript, take a look at this post for plain 'ol javascript or this page for jQuery.