The <img> tag
The <img> tag embeds an image in the document. It's a void element — it has no closing tag.
Syntax
<img src="photo.jpg" alt="Image description"> Attributes
| src | The image URL. Required. |
| alt | A text description for screen readers and for when the image fails to load. Required for accessibility. |
| width / height | Pixel dimensions. Setting both lets the browser reserve space and avoid layout shift (CLS). |
| loading | "lazy" defers loading until the image approaches the viewport. |
| srcset / sizes | A list of images at different densities/sizes for responsive loading. |
Example
<img src="hero.jpg" alt="Team working in the office" width="800" height="450" loading="lazy">
Notes
- alt="" (empty) is correct for purely decorative images that carry no information.
- Without width/height, the browser can't reserve space before the image loads, which causes content to shift.
Accessibility
- Don't just repeat the word "image" in alt text — describe what's actually shown.
- For complex images (charts, diagrams), alt should convey the data's meaning, not just its appearance.
Related tools
The src attribute
The src attribute of img, script, video, audio, iframe: the URL of a resource to load.
The alt attribute
The alt attribute of img: a required text description for accessibility and fallback.
Responsive image
How to give the browser several image variants for different screen sizes.
Lazy-loaded image
The loading="lazy" attribute for deferring images that are off-screen.
HTML Formatter & Minifier
Format and minify HTML code online. Fully processed in your browser, nothing sent to a server.