liveBook · Manning

Img element

As explained in Web Design Playground, Second Edition

The HTML img element embeds an image resource in a web document.

The img element embeds an image file in a web page. The browser retrieves the separate file from the location given by src, then displays it where the element appears. Think of the tag as a tiny sign saying, “Image goes here—please fetch it.” section 6.1

Build the element from three useful attributes:

  • src identifies the image file using a URL, filename, or path such as /images/logo.png. section 6.1
  • alt supplies replacement text for missing images and helps screen readers and Braille apps describe meaningful images. Use an empty value for purely decorative images. section 6.1
  • title optionally supplies tooltip text. section 6.1

A logo that links home

Use an image as a link by wrapping the img element in an a element: <a href="https://atomicwebdesign.io/"><img src="/images/atomic-logo.png" alt="Atomic Web Design logo" title="Logo for Atomic Web Design"></a>. The image becomes clickable, while its alternative text still identifies it. section 6.5

An img element is inline, so it can flow inside a paragraph or other block. Use CSS vertical-align to place it at the baseline, bottom, middle, top, or a precise pixel offset relative to nearby text. section 6.7

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

Make an image fluid with max-width: 100% and height: auto. It shrinks with its parent container without exceeding its original width or distorting its aspect ratio. section 14.7

How Web Design Playground, Second Edition covers it

How deep it goes
Explains syntax, file paths, accessibility, linking, alignment, and responsive sizing, but stops short of advanced image delivery.
Examples
Uses a company logo, a clickable logo, trail images aligned with labels, and an image scaling inside a responsive aside.
What it assumes
Builds from HTML tags and attributes while teaching image locations, alternative text, and basic CSS image behavior.
What you can do after
Build it from scratch by embedding, describing, linking, aligning, and responsively sizing images with HTML and CSS.
How it connects
Connects the img element with HTML attributes, the a element, the figure element, figcaption, CSS, and responsive web design.

Also explained in

Compare how 3 books explain img element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.