HTML Images: Alt Text, Dimensions and Responsive Sources
Learn how to add HTML images with useful alt text, width and height, responsive srcset and picture sources.
Add an image
The <img> element embeds an image and is a void element, so it has no closing tag. Give informative images useful alternative text. Use an empty alt value when an image is decorative and adds no information.
<img src="garden.jpg" width="1200" height="800"
alt="Red flowers growing beside a garden path">
Width and height help the browser reserve the image's aspect ratio while the file loads. Use CSS to let images fit their container.
Offer responsive image files
Use srcset and sizes when you have several resolutions of the same image. The browser can choose a suitable file for the rendered width and screen density. Use <picture> when you need art direction or format-specific sources, with an <img> fallback.
<img
src="garden-800.jpg"
srcset="garden-400.jpg 400w, garden-800.jpg 800w, garden-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, 800px"
width="1200" height="800"
alt="Red flowers beside a garden path">
The sizes value should describe the image's expected rendered width at each breakpoint. Keep the width and height in the same aspect ratio as the source images so the browser can reserve the right amount of space.
<picture>
<source media="(max-width: 600px)" srcset="garden-portrait.jpg">
<img src="garden-wide.jpg" width="1200" height="800"
alt="Red flowers growing beside a garden path">
</picture>
Use CSS backgrounds for decoration
A CSS background-image is useful for decorative imagery. If an image communicates information, use an HTML image with appropriate alternative text instead.
Write useful alternative text
Describe the information an image adds in its context. For example, if a chart shows that monthly visits increased, alt text can state that takeaway rather than only saying “chart.” Avoid starting with “image of”; screen readers already announce the element as an image. For a purely decorative image, use alt="" so it can be skipped.
Load images at the right time
Images below the initial viewport can use loading="lazy" so the browser waits until they are near the visible area. Do not lazy-load the main image that appears immediately when the page opens; let it load normally. Width and height are especially useful with lazy loading because they reserve space before the file arrives.
<img src="gallery-detail.jpg" width="1200" height="800"
loading="lazy" alt="Close-up of red flowers in the garden">
Examples
- View the image element example (opens in a new tab)
- View the background image example (opens in a new tab)
- View the background sizing example (opens in a new tab)
- View the picture element example (opens in a new tab)
For responsive source selection, alt text and loading options, see MDN's HTML image element reference.