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
Grim Fandango
5,804 PointsSetting 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 :)
1 Answer
Felix Lübke
5,730 PointsUse my following code!
img {
display: block;
max-width: 100%;
height: auto;
}
David Bath
25,940 PointsExcellent! Though just specifying "max-width: 100%" seems to work just as well.
David Bath
25,940 PointsDavid Bath
25,940 PointsGood 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:
I would like to know if there is a better way also.