The img Element: Putting Pictures on a Page
src, alt, width and height. Four attributes, and three of them are doing more work than most people realise.
-
HTML Basics
- What is HTML: The Structure Layer of Every Web Page
- HTML Document Structure: DOCTYPE, html, head and body
- Elements, Tags and Attributes: The Vocabulary of HTML
- HTML Comments: Notes That Ship With Your Code
- Block Level and Inline Elements
- Writing and Running Your First HTML Page
- How a Browser Turns Markup Into a Page
- Text and Formatting
- Links and Navigation
- Images and Media
- Lists
- Tables
-
Forms
- Form Structure: form, action and method
- Input Types: Text, Email, Number, Date and the Rest
- Labels: The Most Important Element in a Form
- Checkboxes, Radio Buttons and Grouping
- select, option, optgroup and datalist
- textarea, File Uploads and Hidden Fields
- Buttons: submit, reset and button
- Built In Form Validation
- GET or POST: What Happens When a Form Is Submitted
- Semantic HTML
- HTML5 Features
- Head and Metadata
- HTML with CSS
- HTML with JavaScript
- Accessibility
-
HTML SEO
- How Google Works: Crawling, Indexing and Ranking
- SEO Friendly HTML Structure
- Titles and Descriptions That Earn Clicks
- Headings and Content Structure for Search
- Internal Linking and Anchor Text
- robots.txt and XML Sitemaps
- Canonical URLs and Duplicate Content
- Structured Data and JSON-LD
- Image SEO
- Core Web Vitals and Mobile Friendliness
- DevTools and Debugging
- Editor Productivity
- HTML Best Practices
- HTML Projects
- Advanced Projects
- Practice and Exams
Concept
The img element places an image in the flow of a document. It is a void element - it has no content and no end tag - and it is replaced, meaning the browser swaps it for an external file rather than rendering anything of its own.
Syntax
<img src="path/to/file.jpg" alt="Description of the image" width="800" height="600">| Attribute | Required | Purpose |
|---|---|---|
src | Yes | Where the file is |
alt | Yes | A text alternative for readers who cannot see it |
width / height | Strongly advised | The intrinsic pixel size, so the browser can reserve space |
loading | No | lazy defers images below the fold |
decoding | No | async lets decoding happen off the main thread |
srcset / sizes | No | Offer several files and let the browser pick |
Why width and height are not optional in practice
When the browser lays out the page, an image whose size it does not know occupies no space. Everything below moves up to fill the gap. The file then arrives, the browser discovers it is nine hundred pixels tall, and the entire page below jumps down.
The reader loses their place, or taps a button that has just moved. Google measures this directly as Cumulative Layout Shift, one of the Core Web Vitals, and it affects search ranking.
<!-- causes a jump when it loads -->
<img src="/images/hall.jpg" alt="An empty lecture hall">
<!-- space is reserved on the first layout pass -->
<img src="/images/hall.jpg" alt="An empty lecture hall" width="1600" height="900">The numbers are the intrinsic size of the file, not the size you want on screen. Modern browsers use them purely to compute the aspect ratio, so CSS is still free to resize the image:
img {
max-width: 100%;
height: auto;
}That pair keeps the image inside its container and preserves the ratio, and it is the single most useful rule in a responsive stylesheet.
Choosing a format
| Format | Best for | Notes |
|---|---|---|
| JPEG | Photographs | Lossy, no transparency, universally supported |
| PNG | Screenshots, flat graphics, transparency | Lossless, large files for photographs |
| SVG | Logos, icons, diagrams | Vector, scales perfectly, tiny, text inside is selectable |
| WebP | General replacement for JPEG and PNG | Roughly a quarter smaller, supported everywhere current |
| AVIF | Photographs where size matters most | Smallest of all, slower to encode |
| GIF | Nothing, in new work | Superseded by video or WebP for animation |
Prefer SVG for anything drawn rather than photographed. One file covers every screen density and it usually weighs less than a single PNG.
Example
<article>
<h2>The old printing press</h2>
<img src="/images/press.jpg"
alt="A hand operated printing press with type still set in the bed"
width="1200" height="800"
loading="lazy"
decoding="async">
<p>The press was in daily use until 1974.</p>
<!-- decorative: hidden from screen readers -->
<img src="/images/divider.svg" alt="" width="600" height="12">
</article>Explanation
The photograph carries a description, its intrinsic size, and instructions to load lazily and decode off the main thread. The divider is pure decoration, so its alt is deliberately empty - which is not the same as omitting it. An empty alt tells assistive technology to skip the image entirely; a missing alt makes it read the file name out loud.
When the image fails to load
Files go missing, connections drop, and some readers turn images off to save data. When the image cannot be shown, the browser displays the alt text in its place. This is why alt is described as a text alternative rather than a tooltip: it is what the page falls back to.
Important rules
altis required on everyimg. Decorative images getalt="".imgis inline by default, which means it sits on a text baseline and leaves a few pixels of gap underneath.display: blockorvertical-align: middleremoves it.- The
titleattribute is not an alternative toalt. It is unreliable on touch and keyboard. - Hotlinking an image from another site is a bandwidth theft and can break without warning. Host your own copies.
- An image with a
srcthat fails still occupies its reserved box if width and height were given.
Common mistakes
- Omitting
widthandheightand shipping a page that visibly jumps as it loads. - Uploading a four thousand pixel wide photograph straight from a camera and scaling it down in CSS. The reader still downloads all of it.
- Using a PNG for a photograph, producing a file five times larger than the JPEG equivalent.
- Leaving out
altentirely rather than writingalt=""for decoration. - Putting text inside a raster image. It cannot be selected, searched, translated or read aloud, and it blurs when scaled.
- Setting only
widthin CSS withoutheight: auto, which squashes the image.
Best practices
- Export images at roughly the size they will be displayed, at most twice that for high density screens.
- Always state
widthandheight. - Use SVG for logos, icons and diagrams.
- Add
loading="lazy"to everything below the fold, and never to the main image at the top. - Compress before uploading. A photograph at eighty percent JPEG quality is usually indistinguishable and half the size.
- Give files descriptive names.
brass-lamp-workshop.jpghelps image search;IMG_20260218.jpgdoes not.
Practice
- Load a page with three large images and no dimensions on a throttled connection. Record the layout shift in DevTools, add the attributes, and measure again.
- Take one photograph and export it as JPEG, PNG and WebP at the same visual quality. Compare file sizes.
- Place a logo as a PNG and as an SVG, then zoom the browser to three hundred percent and compare.
- Explain the difference between a missing
altandalt=""to someone who has never used a screen reader.