In the following snippet, I'm specifying the width in pixel of the image using the img.icon
selector. However, as I down-resize the browser the image also shrinks. If I remove the commented line, then the size of the image doesn't change as I resize the browser. Why?
I don't understand how max-width: 100%
affects it, also because the selector img
is less specific than img.icon
. What's happening?
I'd like an answer explaining how max-width
and width
are interacting, because I think I've misunderstood the meaning of one or both of them.
As regards the ruleset img { max-width: 100%; }
it's suggested by Keith J. Grant in his book:
TIP As part of a fluid layout, you should always ensure images don’t overflow their container’s width. Do yourself a favor and always add this rule to your stylesheet to prevent that from happening:
img { max-width: 100%; }
.
Therefore I would like to keep it.
img { max-width: 100%; } /* because of this, the size of the image is not fixed. Why? */
img.icon {
width: 500px;
}
<img class="icon" src="https://image.shutterstock.com/image-vector/example-red-square-grunge-stamp-260nw-327662909.jpg" alt="Home"/>