Regarding Responsive Web Design Principles: Make an Image Responsive
The explanation and solution to this challenge seems wrong, but this isn’t the only time I’ve heard the max-width: 100%
thing in relation to responsive images. I must have a misunderstanding about the topic. Can someone help me understand this better?
As I understand the goals behind Responsive Web Design we wan’t our images and content to respond to the size of the viewport. This usually refers to the size of either the browser window or the screen of a mobile device.
The challenge:
Add style rules for the
img
tag to make it responsive to the size of its container. It should display as a block-level element, it should fit the full width of its container without stretching, and it should keep its original aspect ratio.
The code of an accepted answer:
img {
max-width: 100%;
display: block;
height: auto;
}
Why I don’t think this is responsive:
- Using
max-width: 100%
, the image only responds to the viewport size if it’s really small (250px in this case… narrower than the first iPhone). - Using
display: block
is pointless. An inline image can be responsive as well as a block-level element. - Using
height: auto
is the default. It has no effect.
A real responsive image should change size based on the size of the viewport, shouldn’t it? All that we should need to make an image responsive is a width
value that’s based on the width of the viewport using either a percentage or a vw unit. Then max width
and min-width
could be used to prevent the image from getting too big or too small as it seems the properties are intended to do.
I created a demo of what I think is a correct example of a responsive image at CodePen.