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 / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

Learn when to use HTML em, strong, q, blockquote, abbr, code, and kbd. Examples show how each element conveys meaning rather than just changing the appearance of text.

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 / What Is HTML and What Does It Do on a Web Page?

HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML gives web content structure and meaning. Learn how it differs from CSS and JavaScript, see how basic markup appears in a browser, and make your first HTML file.

HTML Tutorial / Headings and Paragraphs

HTML Tutorial / Headings and Paragraphs

Learn when to use h1–h6 headings, p paragraphs, and br line breaks in HTML. Examples show document hierarchy, browser output, and common mistakes.

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 / Responsive Images: srcset, sizes, and picture

HTML Tutorial / Responsive Images: srcset, sizes, and picture

Offer image resolutions with srcset and sizes, and switch crops with picture. Downloadable files let you test the examples in a browser.

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 / Syntax

HTML Tutorial / Syntax

Learn the essential rules of HTML syntax, including elements, tags, attributes, void elements, comments, nesting, and the structure of a complete HTML document.

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.