AD JB Toolbox 01 EN

HTML Tutorial / Images: img, Alt Text, and Dimensions

To add an image to an HTML document, put its file URL in the img element's src attribute and describe its relevant content in alt. Set width and height to the image's intrinsic dimensions so the browser can reserve space before the file loads.

Insert an image with img

The img element embeds an image and has no closing tag. Use the lake-and-tree example image to try the following code.

<img src="html-image-sample-lake-tree-1600-v1.webp"
     alt="A tree on a small island in a lake, with mountains beyond"
     width="1600" height="900">
  1. Download the example image and save it beside the HTML file you are editing, such as index.html.
  2. Insert the code above inside the HTML file's body and save the file.
  3. Open or refresh the HTML file in a browser. You should see the lake photo shown below.

A tree on a small island in a lake, with mountains beyond

src identifies the file relative to the HTML file's location. The code above looks for the image in the same folder. If you saved it in an images subfolder, change the attribute to src="images/html-image-sample-lake-tree-1600-v1.webp". The filename and extension must also match the actual file.

Clicking the image here opens the original file. Wrapping an image in a link is a separate feature from inserting it with img.

What should alt say?

alt is alternative text. It gives readers who cannot see the image, or whose browser cannot load it, the information the image contributes in context. Describe that role briefly instead of writing “image” or a list of search keywords.

<img src="html-image-sample-lake-tree-1600-v1.webp"
     alt="A tree on a small island in a lake, with mountains beyond"
     width="1600" height="900">

If an image is purely decorative and repeats nearby information, an empty alt="" may be appropriate. The empty value identifies it as nonessential; omitting the alt attribute does not convey that intent. If the image conveys essential information, explain it in the alternative text or adjacent prose. The W3C WAI alt-text decision tree helps with ambiguous cases.

What do width and height do?

The example file is 1600×900 pixels. Its width="1600" and height="900" values help the browser calculate the aspect ratio in advance. These attributes do not reduce the image file's byte size or pixel count.

If the image is wider than the content area, CSS can limit its displayed width while preserving the aspect ratio.

img {
  max-width: 100%;
  height: auto;
}

To send a smaller file, you need a separately resized image. The responsive-images tutorial explains how srcset and sizes let the browser choose among image files.

If the image does not appear

  • Check the exact filename and extension in src.
  • Check the image's folder relative to the HTML file.
  • Open the image URL directly in a browser to see whether the file is available.
  • Do not assume smaller displayed dimensions mean a smaller downloaded file.

The MDN img element reference describes the available attributes and their behavior.

More in This Category
HTML Tutorial / When to Use div and span

HTML Tutorial / When to Use div and span

Understand the structural difference between div and span, style only the content you need, and choose semantic elements when they better describe the content.

HTML Tutorial / Table Basics: table, tr, th, and td

HTML Tutorial / Table Basics: table, tr, th, and td

Build a basic HTML data table with rows and header or data cells, then organize its headings and body without using tables for page layout.

HTML Tutorial / Basic Structure of an HTML Document

HTML Tutorial / Basic Structure of an HTML Document

Learn the roles of doctype, html, head, and body, then save your first HTML file and open it in a browser. Includes a complete example, a diagram, and fixes for common filename, refresh, and encoding problems.

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

Choose the right HTML list for ordered items, unordered items, and name–description pairs. Try a complete example, custom numbering, and correctly nested lists.

HTML Tutorial / Explain Forms with fieldset, legend, and autocomplete

HTML Tutorial / Explain Forms with fieldset, legend, and autocomplete

Group related controls with fieldset and legend, and use suitable autocomplete tokens to describe the purpose of input fields.

HTML Tutorial / Group Images and Captions with figure and figcaption

HTML Tutorial / Group Images and Captions with figure and figcaption

Use figure and figcaption to keep an image and its visible explanation together, and distinguish captions from image alt text.

HTML Tutorial / Form Structure and GET vs. POST

HTML Tutorial / Form Structure and GET vs. POST

Understand how form action, method, and control names determine where form values go, and when GET or POST is appropriate.

HTML Tutorial / Images: img, Alt Text, and Dimensions

HTML Tutorial / Images: img, Alt Text, and Dimensions

Add an image with HTML img, write useful alternative text, and set width and height correctly. A downloadable example helps you test file paths and sizing.

HTML Tutorial / Connect Labels and Inputs Correctly

HTML Tutorial / Connect Labels and Inputs Correctly

Connect labels to inputs with for and id, distinguish id from submitted name, and learn why placeholder text cannot replace a label.

HTML Tutorial / Links: Paths, Page Fragments, and Email

HTML Tutorial / Links: Paths, Page Fragments, and Email

Create HTML links with absolute or relative URLs, jump to a document fragment, and add email, new-tab, or download links. Learn what to verify before publishing.