HTML Images

Beginner
14 min read
Updated: Nov 2026
6 examples
img / figure / picture

Definition and Usage

HTML images embed pictures into a webpage with the <img> element. The web started as text; images make pages informative and visual. <img> is a void (self-closing) element and a replaced element—its content comes from an external file, not from child HTML text.

How is this page different from the <img> tag reference?

This tutorial teaches how to use images in real HTML pages: choosing src paths, writing alt, reserving space with width/height, captions with <figure>, formats, responsive <picture>, and when CSS backgrounds are better. For attribute lists and technical rules, open the <img> tag reference.

What do you need for a useful image?

At minimum: src (where the file is) and alt (what the image means when it cannot be seen). Add width and height so the layout does not jump when the file arrives.

When should an image be in HTML vs CSS?

Put content images in HTML (<img>) so they have alt text and print with the article. Put decorative surfaces in CSS (background-image) when they are not part of the meaning.

Descriptive filenames help.

Prefer mountain-sunrise.jpg over img835.png—humans and search engines both benefit.

Do not hotlink.

Do not point src at someone else’s image host without permission. Host files you have the right to use, or use a CDN you control.

Relative URLs are easier to maintain.

Keep images on your site (or CDN) and use paths like images/photo.jpg instead of hard-coding another domain for every asset.

HTML Syntax for Images

The basic pattern every content image needs:

img.html
<img src=" url" alt="description">
HTML
<!-- Same folder as the HTML file -->
<img src="dinosaur.jpg" alt="Dinosaur skeleton">

<!-- Inside an images/ subfolder -->
<img src="images/dinosaur.jpg" alt="Dinosaur skeleton">

<!-- Absolute URL (host yourself; avoid hotlinking) -->
<img src="https://www.example.com/images/dinosaur.jpg" alt="Dinosaur skeleton">

Optional but recommended: width and height in pixels (integers, no units) to reserve layout space.

Core HTML Concepts

Ideas that make image markup work well on real pages:

ConceptMeaning in HTML
<img>Void element that embeds an image file
srcURL of the image resource (relative or absolute)
altText alternative when the image cannot be seen
width / heightIntrinsic size hints that reduce layout shift
Replaced elementContent/size defined by an external resource
<figure> / <figcaption>Group an image with a visible caption
<picture> / <source>Art direction or format choice with img fallback
loading="lazy"Defer download until near the viewport
CSS backgroundDecorative imagery that is not page content

Choose an image format

FormatBest for
JPEG / JPGPhotos; smaller files, lossy compression
PNGTransparency, sharp graphics, screenshots
SVGLogos and icons that must scale cleanly
WebP / AVIFModern formats with smaller sizes; offer fallbacks when needed
GIFSimple animation; prefer video or CSS for most motion

What to put in alt

Image roleAlt guidance
Content / informativeDescribe the information the image adds
Already described in nearby textOften alt="" to avoid duplication
Decorativealt="" (or use CSS background instead)
Inside a linkAlt (or visible text) must name the link destination
Text baked into a graphicAvoid if possible; otherwise put that text in alt

How to Add Images to an HTML Page

A practical order from first photo to responsive setups.

  1. Host the file (or use a CDN you control) and pick a descriptive filename.
  2. Add <img src="…" alt="…"> where the image belongs in the content flow.
  3. Set width and height to the intrinsic pixel size (scale with CSS if needed).
  4. Wrap with <figure> + <figcaption> when a visible caption helps.
  5. Use loading="lazy" for below-the-fold images; keep hero/LCP images eager.
  6. Add <picture> when you need different crops or formats per screen.
GoalHTML pattern
Basic content image<img src="photo.jpg" alt="…">
Reserve layout spacewidth="400" height="300" on img
Visible caption<figure>…<figcaption>…</figcaption></figure>
Defer offscreen loadloading="lazy"
Art direction / formats<picture><source>…<img></picture>

Live Preview

Switch how an image is marked up and see the role of alt, size, caption, and lazy loading.

Preview updates the sample markup note and a simple rendered result.

Sample result
A scenic mountain landscape at sunrise
<img src="photo.jpg" alt="A scenic mountain landscape at sunrise">

HTML Examples

Six patterns: basic img, dimensions, figure caption, lazy loading, responsive picture, and a full page.

Example 1 — Basic Content Image

Embed a photo with src and meaningful alt:

HTML
<img src="photo.jpg" alt="A scenic mountain landscape at sunrise">
Try It Yourself

How It Works

  1. src tells the browser which file to fetch.
  2. alt describes the image if it fails or cannot be seen.
  3. Void element — no closing </img> tag.
  4. Test alt by temporarily misspelling the filename and checking what appears.

Example 2 — Width, Height, and Title

Reserve space before the file loads to avoid layout jump:

HTML
<img src="photo.jpg" alt="Green landscape photo"
  width="300" height="195" title="Hover tooltip text">
Try It Yourself

How It Works

  1. HTML and the image load as separate requests; size hints keep text from jumping.
  2. Integers — width/height are pixels without units.
  3. CSS can still scale the image; attributes define the aspect-ratio box.
  4. title is optional hover text—not a substitute for alt.

Example 3 — Figure with Caption

Group a content image with a visible caption everyone can read:

HTML
<figure>
  <img src="chart.jpg" alt="Bar chart showing 25% sales growth in Q1">
  <figcaption>Figure 1: Sales growth in Q1.</figcaption>
</figure>
Try It Yourself

How It Works

  1. figure marks a self-contained illustration in the article.
  2. figcaption is visible to all users; alt still describes the graphic for assistive tech.
  3. Do not duplicate the same long sentence in both alt and caption unnecessarily.
  4. Charts need alt that conveys the key takeaway, not just “chart.png”.

Example 4 — Lazy Loading Below the Fold

Defer downloading images that are not needed for the first screen:

HTML
<img src="large-photo.jpg" alt="High-resolution photo"
  loading="lazy" width="500" height="320">
Try It Yourself

How It Works

  1. lazy waits until the image is near the viewport.
  2. Saves bandwidth on long pages with many photos.
  3. Keep heroes eager — do not lazy-load the main above-the-fold image.
  4. Still set width/height so reserved space is correct.

Example 5 — Responsive <picture>

Offer different files by viewport; keep img as the required fallback:

HTML
<picture>
  <source srcset="photo-small.jpg" media="(max-width: 600px)">
  <source srcset="photo-large.jpg" media="(min-width: 601px)">
  <img src="photo-default.jpg" alt="Responsive landscape photo">
</picture>
Try It Yourself

How It Works

  1. source candidates are tried in order against media (or type).
  2. img is mandatory inside picture as the fallback and for alt text.
  3. Art direction — crop differently for phone vs desktop when needed.
  4. Formats — you can also offer WebP with a JPEG img fallback.

Example 6 — Complete Images Page

Combine basic embedding, a captioned figure, and lazy loading on one HTML page:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML Images Example</title>
</head>
<body>
  <h1>HTML Images Example</h1>

  <h2>Basic Image</h2>
  <img src="photo.jpg" alt="A beautiful landscape" width="400" height="260">

  <h2>Figure with Caption</h2>
  <figure>
    <img src="chart.png" alt="Sales chart for Q1">
    <figcaption>Figure 1: Sales growth in Q1.</figcaption>
  </figure>

  <h2>Lazy Loading</h2>
  <img src="large.jpg" alt="High-resolution image" loading="lazy" width="400" height="260">
</body>
</html>
Try It Yourself

How It Works

  1. Structure — headings organize image sections like any other HTML content.
  2. Accessibility — every content image keeps an alt.
  3. Performance — dimensions + lazy loading for offscreen assets.
  4. Semantics — figure when a caption is part of the story.

Edge Cases & Pitfalls

Most image bugs are missing alt, bad paths, or treating decoration as content.

alt

Missing alt or filename as alt

Never omit alt on content images. Do not write alt="photo.jpg"—describe the meaning.

path

Broken src paths

Wrong folders or typos show the alt text (or a broken icon). Check relative paths from the HTML file.

hotlink

Hotlinking other sites

Unethical bandwidth use and fragile URLs. Host your own assets or licensed CDN files.

cls

No width/height

Text jumps when large images finally load. Set intrinsic dimensions (or CSS aspect-ratio).

lazy

Lazy-loading the hero

Above-the-fold / LCP images should load immediately—skip loading="lazy" there.

css

Content stuck in CSS backgrounds

Important photos and charts belong in <img> so they get alt text and remain part of the document.

Browser Support

The img element, figure/figcaption, picture, and loading="lazy" are supported in all modern browsers.

✓ Baseline · Widely available

Images in HTML

Embedding images with img — plus captions and responsive picture — works across current browsers.

100% img / picture support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML images features Universal

Bottom line: Focus on alt text, file size, and correct paths—browser support for basic HTML images is not the bottleneck.

Key Takeaways

  • src + alt are the minimum for useful content images.
  • width/height reduce layout shift while the file downloads.
  • figure adds a visible caption; picture chooses among sources.
  • HTML for content, CSS backgrounds for decoration.

One line: Embed with <img src alt> → size it → caption or respond with figure/picture when needed.

Frequently Asked Questions

Use the img element with src pointing to the image URL (relative or absolute) and alt with a text description. img is a void element—no closing tag.
This tutorial teaches how to use images in real HTML pages: alt decisions, width/height, figure captions, formats, responsive picture, lazy loading, and CSS backgrounds. The img tag reference covers element syntax and attributes in detail.
Alt describes the image when it cannot be seen—screen readers, broken paths, text-only browsers, or images turned off. Write what the image contributes to the page, not the filename.
Use alt="" only for purely decorative images that add no information. Prefer CSS background images for decoration. Never omit the alt attribute entirely on content images.
Use img when the image is content (photos, charts, product shots). Use CSS background-image for decorative surfaces that do not need alt text or to be printable as content.
img is a replaced element: its visual content and intrinsic size come from an external file, not from child HTML text inside the tag.

Need the element reference?

Open the <img> tag page for the full attribute list and edge cases.

img tag reference →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful