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

Setting responsive images to only expand to 100% of their actual original size rather than viewport?

Hi,

This may be a silly question, but in my responsive design I want images to only ever expand to 100% of the original image size (and so never exceed the resolution of the actual image), but shrink to fit the width of smaller devices.

Is this possible with CSS?

Thanks :)

Good question! You could give the image both width and max-width properties, but I believe it will require you to do the same thing for each image separately. So something like:

img {
  width: 100%;
  max-width: 400px; /* or whatever the true width of the image is */
}

I would like to know if there is a better way also.

1 Answer

Use my following code!

img {
    display: block;
    max-width: 100%;
    height: auto;
}

Excellent! Though just specifying "max-width: 100%" seems to work just as well.