The reason for this is that the width
and height
attributes define the inherent width and height of the image.
By default, the CSS properties width
and height
have an initial value of auto
. As images are considered replaced elements, they will use their intrinsic sizes (§10.3.2 and §10.6.2).
However, you can override this behavior by specifying a different size in your style declaration, which will then be applied to the image.
If you want to prevent overriding, you can add inline styles with the important flag:
<img src="img_forest.jpg" alt="Forest" width="600" height="400"
style="height: auto !important; width="auto"!important" />
img {
height: 100px;
width: 500px;
}
<img src="/favicon.ico" alt="Forest" width="16" height="16"
style="height: auto !important; width: auto !important" />