TExcelTAD
Home › HTML › HTML Images

HTML Images

Images make pages more engaging and often explain things faster than words. In HTML, an image is added with the <img> element.

The img element

<img> is an empty element: it has no closing tag and no content. Everything it needs is in its attributes.

html
<img src="images/cat.jpg" alt="A ginger cat asleep on a windowsill" width="400" height="300">
Attribute Required? Purpose
src Yes The path or URL of the image file
alt Yes Text alternative, read by screen readers and shown if the image fails
width / height Recommended Intrinsic size in pixels; lets the browser reserve space before loading
loading Optional lazy delays loading off-screen images
title Optional Tooltip text

Image sources that work offline

Usually src points to a file such as photo.jpg. You can also embed an image directly with a data URI. Here is a small SVG drawing embedded straight into src:

html · try it yourself
<img
  src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Crect width='120' height='120' rx='16' fill='%23e34c26'/%3E%3Ctext x='60' y='75' font-size='40' text-anchor='middle' fill='white' font-family='sans-serif'%3EHTML%3C/text%3E%3C/svg%3E"
  alt="Orange square with the word HTML"
  width="120" height="120">
<p>This image is stored inside the <code>src</code> attribute itself.</p>

Note: Data URIs are great for tiny icons and for these offline playgrounds. For photos, separate files are better because the browser can cache them.

Alt text matters

The alt attribute describes the image for people who cannot see it. It is also shown when the image fails to load. Compare a working and a broken image:

html · try it yourself
<p>Broken image with alt text:</p>
<img src="missing-file.png" alt="Diagram of the water cycle" width="220" height="60">
<p>Broken image without alt text:</p>
<img src="missing-file.png" width="220" height="60">

Guidelines for good alt text:

  • Describe what matters about the image in context, briefly.
  • Don't start with "image of" — screen readers already announce it as an image.
  • For purely decorative images, use an empty value: alt="". The screen reader will skip it.

Sizing images

You can size an image with the width/height attributes or with CSS. CSS wins if both are present, and it lets you build flexible layouts. A very common rule makes images shrink to fit their container:

html · try it yourself
<style>
  .frame { width: 260px; border: 2px dashed #999; padding: 6px; }
  .frame img { max-width: 100%; height: auto; display: block; }
</style>
<div class="frame">
  <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='200'%3E%3Crect width='600' height='200' fill='%2387ceeb'/%3E%3Ccircle cx='500' cy='60' r='35' fill='%23ffd700'/%3E%3Crect y='150' width='600' height='50' fill='%233c9a3c'/%3E%3C/svg%3E"
       alt="A simple landscape with blue sky, yellow sun and green grass" width="600" height="200">
</div>
<p>The SVG is 600px wide, but <code>max-width: 100%</code> keeps it inside the 260px frame.</p>

Floating images beside text

The CSS float property lets text wrap around an image:

html · try it yourself
<style>
  .avatar { float: left; margin: 0 12px 6px 0; border-radius: 50%; }
  p { font-family: sans-serif; line-height: 1.5; }
</style>
<svg class="avatar" width="70" height="70" role="img" aria-label="Smiling face">
  <circle cx="35" cy="35" r="35" fill="#ffcc4d"/>
  <circle cx="24" cy="28" r="5" fill="#333"/><circle cx="46" cy="28" r="5" fill="#333"/>
  <path d="M20 44 Q35 58 50 44" stroke="#333" stroke-width="4" fill="none"/>
</svg>
<p>This avatar floats to the left, so this paragraph flows around its right side.
Floats were once the main tool for page layout; today we mostly use Flexbox and Grid,
but floats are still perfect for wrapping text around a picture.</p>

Image formats

Format Good for Transparency Animation
JPEG (.jpg) Photos No No
PNG (.png) Screenshots, graphics with sharp edges Yes No
GIF (.gif) Simple animations Yes (1-bit) Yes
SVG (.svg) Logos, icons, diagrams (vector, scales perfectly) Yes Yes
WebP / AVIF Modern, smaller photos and graphics Yes Yes

Related elements

  • <picture> with several <source> elements lets the browser choose a format or size.
  • <figure> and <figcaption> attach a visible caption to an image.
  • <map> and <area> create image maps with clickable regions.
html
<figure>
  <img src="chart.png" alt="Sales rose 20% from 2023 to 2024">
  <figcaption>Figure 1: Yearly sales</figcaption>
</figure>

Tip: Always include width and height attributes. The browser can then reserve the right space before the image loads, which stops the page from jumping around.

Test yourself

Which attribute provides a text alternative for an image?

  1. title
  2. alt
  3. desc
  4. label

What is the right alt value for a purely decorative image?

  1. alt="image"
  2. Leave the attribute out
  3. alt="" (empty)
  4. alt="decorative"

Why does <img> have no closing tag?

  1. It is deprecated
  2. It is an empty (void) element
  3. Closing tags are optional for all elements
  4. It must be inside a <figure>