Images

HTML Images Syntax

The <img> element embeds an image. It is empty (no end tag) and needs two attributes: src (path to the image) and alt (alternate text).

<img src="url" alt="alternatetext">

Image Size

Example

Always set width and height: the browser reserves the space before the image loads, so the page does not jump:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<img src="https://picsum.photos/id/1025/300/200" alt="A dog wrapped in a blanket" width="300" height="200">

</body>
</html>

Result

The alt Attribute

Example

If the image cannot be loaded, the alt text is shown instead (and screen readers read it aloud):

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<img src="missing-photo.jpg" alt="Team photo at the 2024 annual meetup" width="300" height="80">

</body>
</html>

Result

Note: Describe what the image shows or does. For purely decorative images, use an empty alt="" so screen readers skip them.

Images as Links and Floating

Example

Wrap an image in <a> to make it clickable; float it with CSS to wrap text around it:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<a href="https://picsum.photos/">
  <img src="https://picsum.photos/id/1062/120/90" alt="Dog in a jacket" width="120" height="90" style="float:right; margin-left:10px;">
</a>
<p>The image floats to the right of this text. Text wraps around floated images, which is useful for articles with pictures. Click the image to follow the link.</p>

</body>
</html>

Result

Common Image Formats

FormatBest for
JPEG (.jpg)photographs
PNG (.png)graphics with sharp edges or transparency
SVG (.svg)logos and icons (vector: sharp at any size)
WebP / AVIFmodern formats: smaller files than JPEG/PNG
GIF (.gif)simple animations