Make images resize automatically to fit horizontal space

I use the following HTML code to make images adjust to the width of their container automatically. In the sample below, the original image is 1024 by 683 pixels in size. But it fits within a much smaller container and adjusts automatically when the page is resized.

<img src="example.jpg" style="width: 100%; height: auto" width="x" height="y" alt="text">

Manila Bay sunset
Manila Bay sunset
width: 100%
This is what makes the image shrink or expand to occupy the entire width of its container
height: auto
If not included the height will remain constant as the width changes resulting in weird-looking images
height, width, alt
optional but should be included as much as possible

To target all images automatically rather than individually coding each image you can add the following line to your stylesheet instead.

img {width: 100%; height: auto}

0 comment... add one now