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

Logan Murphy
Logan Murphy
1,685 Points

How to put the different size of the images in a container in CSS.

HI there Q1: I try couple hours to fix my issue here, I try to make 4 different sizes of the image make them evenly inside div container without smash the picture size. I try flexbox, flex-grow, flex-basis.

Q2: How I style image alt text and let them show up on the page? Please help, thank you!

2 Answers

Alonso Quesada
Alonso Quesada
14,782 Points

1) Place your image inside <figure> tags, and then use flexbox in the parent element to make them position in the same row, you can use overflow:hidden in the <figure> tag to avoid weird stretching of the images : The html should look like this

<div class="parent">
<figure>
<img src="..." alt="...">
<figcaption>The img description goes here</figcaption>
</figure>
<figure>
<img src="..." alt="...">
<figcaption>The img description goes here</figcaption>
</figure>
<figure>
<img src="..." alt="...">
<figcaption>The img description goes here</figcaption>
</figure>
<figure>
<img src="..." alt="...">
<figcaption>The img description goes here</figcaption>
</figure>
</div>

And your css should look like this

.parent {
  height:300px;
  display:flex;
  width:100%;
}
figure {
  height:100%;
  overflow:hidden;
  margin: 5px;
}
img {
  height:200px;
  width:200px;
}
figcaption {
  font-family:verdana;
  color:#ff0099;
}

Here is a fiddle of with four different sized images: https://jsfiddle.net/tgtgmdwL/

2) Alt attribute is more for accessibility purposes, you can't style it and show it (well, you can, but it is not recommended), use <figcaption> tags as the example above.

Logan Murphy
Logan Murphy
1,685 Points

Thank you so much! Love your detail explanation (Sorry about the late reply.)

Bruno Navarrete
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 Points

If you want to keep the natural ratio of your images, you can fix their height and leave their width:auto. If you want them to fill your containing elements, you can use background-size:cover and background-position:center. Both examples right here

Logan Murphy
Logan Murphy
1,685 Points

Thanks, Bruno so much! help me out a lot! (Sorry about the late reply.)