<img>

An image embedded in the page.

Display
inline (replaced element)
Closing tag
None, void element
Permitted content
None, empty element
On this page

Example

#
HTML
<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"
>
Preview

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

#
Syntax
<img src="image-address" alt="image description" width="…" height="…">

Attributes

#
  • src

    URL

    The address of the image file. It is the only attribute without which the image won’t be displayed at all.
  • alt

    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.
  • width / height

    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.
  • loading

    eager | lazy

    lazy 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.
  • srcset

    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 example image-2x.png 2x for high pixel density screens.
  • sizes

    list of conditions and widths

    Works together with a width-based srcset (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. async lets 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 setting high on 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

URL

The address of the image file. It is the only attribute without which the image won’t be displayed at all.

HTML
<img src="/collections/covers/vibe-coding.webp" alt="A programmer wearing headphones in front of a monitor" width="240" height="160">
Preview

alt

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.

HTML
<img src="data:," alt="Chart of student growth in 2026" width="240" height="120">
Preview

width / height

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.

HTML
<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">
Preview

loading

eager | lazy

lazy 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.

HTML
<img src="/collections/covers/darmowa-nauka-programowania.webp" alt="A library at night" width="240" height="160" loading="lazy">
Preview

srcset

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 example image-2x.png 2x for high pixel density screens.

HTML
<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"
>
Preview

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 alt text 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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.