- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <img> Tag
HTML Tags
HTML <img> Tag
An image without width and height makes the page jump when it loads. An image without alt text is invisible to anyone using a screen reader. Most problems with the HTML img tag are one of those two.
What the img tag does
The <img> tag puts an image on the page. It is a void element: there is no closing tag and nothing goes inside it. Two attributes do almost all the work: src says where the file is, and alt says what the image shows.
Syntax
html
<img src="image.jpg" alt="Description of image">Attributes
| Attribute | What it does | Example |
|---|---|---|
| src | Path or URL of the image file. | src="photo.png" |
| alt | Text that replaces the image when it cannot be seen. Use an empty alt="" for purely decorative images. | alt="Profile picture" |
| width | Intrinsic width in pixels, written without units. Lets the browser reserve space before the file arrives. | width="300" |
| height | Intrinsic height in pixels, written without units. | height="200" |
| loading | lazy waits until the image is near the screen; eager (the default) loads it straight away. | loading="lazy" |
| srcset | The same image at several sizes, so the browser can download the smallest one that still looks sharp. | srcset="img-400.jpg 400w, img-800.jpg 800w" |
| sizes | Tells the browser how wide the image will be shown, so it can pick the right file from srcset. | sizes="(max-width: 600px) 480px, 800px" |
| title | A tooltip on hover. Not a replacement for alt: touch and keyboard users never see it. | title="HTML5 Logo" |
| crossorigin | Fetches the image with CORS. You need it when JavaScript will read the image's pixels on a canvas. | crossorigin="anonymous" |
Writing alt text that actually helps
- Describe what the image tells the reader, not every detail in it. "Sales doubled between March and June" is more useful than "A bar chart".
- Do not start with "Image of". Screen readers already announce it as an image.
- A decorative image still needs the attribute, just empty:
alt="". When alt is missing completely, some screen readers read the file name aloud instead, something like "IMG underscore 4032 dot jpg". - When the image is inside a link, the alt text should say where the link goes.
Stop the page from jumping
When the browser does not know an image's size, it lays the page out as if the image had no height, then pushes everything down once the file arrives. Readers lose their place, and Google measures that movement as Cumulative Layout Shift. Setting width and height fixes it, even when CSS makes the image responsive:
Reserve space, stay responsive
html
<img src="sunrise.jpg" alt="Sunrise over the hills" width="800" height="500">
<style>
img { max-width: 100%; height: auto; }
</style>The browser uses the two numbers for the aspect ratio, so it reserves the right amount of space at any screen width.
Lazy loading, and when not to use it
loading="lazy" delays images until the reader scrolls near them. That saves data on long pages, so use it on images further down. Do not use it on the main image at the top of the page: it delays the very image the reader sees first, and the page feels slower.
Example
html
<h2>Example: Adding an Image</h2>
<img src="https://picsum.photos/seed/picsum/400/300" alt="Beautiful sunrise over the mountains" width="400" height="250">Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Common mistakes
- No width and height. The page shifts as images load.
- Useless alt text, such as
alt="image"or a file name. It tells a screen reader user nothing. - Lazy loading the hero image. The first thing on screen arrives last.
- A 4000-pixel photo in a 400-pixel slot. Resize the file, or offer smaller versions with srcset.
width="50%". HTML5 only accepts pixel numbers in the width attribute. Set percentages in CSS instead.
HTML <img> Tag exercises
Show an image
Show this image and describe it as A star: https://html5andcss3.org/samples/star.svg
Result
