<img>
An image embedded in the page.
- Display
- inline (replaced element)
- Closing tag
- None, void element
- Permitted content
- None, empty element
Example
#<img
src="/collections/covers/jak-zostac-programista.webp"
alt="A person with a backpack walks along a path toward a city at dusk"
width="320"
height="213"
>Definition and usage
#The <img> element inserts an image. It is an empty element (it has no closing tag), and the src attribute decides what is displayed. Supported formats include JPEG, PNG, GIF, SVG, WebP, and AVIF.
The alt attribute is required. It is the text a screen reader reads instead of the image, and the text that appears when the graphic fails to load. For purely decorative images, leave it empty (alt="") and assistive technologies will skip it.
Always provide width and height. The browser then reserves space for the image before it downloads, and the page content doesn’t jump while loading. You can still change the display size later in CSS.
Syntax
#<img src="image-address" alt="image description" width="…" height="…">Attributes
#URL
The address of the image file. It is the only attribute without which the image won’t be displayed at all.text
Alternative text. Describe what is in the image and why it is there. When the file fails to load, this is the text you will see, as in the example with a broken address.number of pixels
The intrinsic dimensions of the image in pixels, without a unit. They let the browser know the image’s aspect ratio before downloading it, so it doesn’t shift the content.eager | lazy
lazydefers downloading the image until it is close to the visible part of the page. It is a simple way to save bandwidth for images below the first screen. Don’t mark an image that is visible right away aslazy.list of addresses with a width or density
A set of versions of the same image in different sizes. The browser picks the best one for the screen, for exampleimage-2x.png 2xfor high pixel density screens.sizes
list of conditions and widths
Works together with a width-basedsrcset(800w). It tells the browser how wide the image will be on the page depending on the window width, for example(min-width: 768px) 50vw, 100vw.decoding
sync | async | auto
A hint about whether the image can be decoded independently of the rest of the page.asynclets the content appear without waiting for the image.fetchpriority
high | low | auto
Hints at how urgent it is to download the image. It is worth settinghighon the main image at the top of the page so that it appears sooner.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#src
URLThe address of the image file. It is the only attribute without which the image won’t be displayed at all.
<img src="/collections/covers/vibe-coding.webp" alt="A programmer wearing headphones in front of a monitor" width="240" height="160">alt
textAlternative text. Describe what is in the image and why it is there. When the file fails to load, this is the text you will see, as in the example with a broken address.
<img src="data:," alt="Chart of student growth in 2026" width="240" height="120">width / height
number of pixelsThe intrinsic dimensions of the image in pixels, without a unit. They let the browser know the image’s aspect ratio before downloading it, so it doesn’t shift the content.
<img src="/collections/covers/python-czy-javascript.webp" alt="Two desks: data analysis and web design" width="180" height="120">
<img src="/collections/covers/python-czy-javascript.webp" alt="" width="90" height="60">loading
eager | lazylazy defers downloading the image until it is close to the visible part of the page. It is a simple way to save bandwidth for images below the first screen. Don’t mark an image that is visible right away as lazy.
<img src="/collections/covers/darmowa-nauka-programowania.webp" alt="A library at night" width="240" height="160" loading="lazy">srcset
list of addresses with a width or densityA set of versions of the same image in different sizes. The browser picks the best one for the screen, for example image-2x.png 2x for high pixel density screens.
<img
src="/collections/covers/kurs-html-css-od-podstaw.webp"
srcset="/collections/covers/kurs-html-css-od-podstaw.webp 1x, /collections/covers/kurs-html-css-od-podstaw.webp 2x"
alt="A temple wall arranged like a web page layout"
width="240"
height="160"
>Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Write the
alttext as if you were describing the image to someone over the phone. Don’t start with “image of”. - Save photos as WebP or AVIF. They are usually about half the size of JPEG at the same quality.
- Put an image with a caption in a
<figure>together with a<figcaption>.
Related entries
#- <picture>A container for several versions of an image. The browser picks the one that best fits the screen and format.
- <figure>A self-contained illustration with an optional caption: an image, chart, code snippet, or quote.
- <figcaption>The caption of an illustration inside a `<figure>` element.
- <source>An alternative source of an image, audio, or video for `<picture>`, `<audio>`, and `<video>`.
- onloadRuns JavaScript code when a resource has finished loading: the whole page, an image, a frame, a script or a stylesheet.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.