While text forms the structural backbone of the web, images provide the critical visual context that engages users. However, images are inherently massive assets. We must master technical integration to ensure strict accessibility, eradicate layout shifts, and implement responsive art direction.
1The Mechanics & Layout Shifts
The <img> tag is a 'Void Element', meaning it is self-closing and wraps no internal content. Its core engine relies on the src attribute to declare the precise URL of the asset.
However, a catastrophic performance error is failing to declare the image's dimensions. When an image downloads, it violently pushes text down the screen upon rendering, causing a Cumulative Layout Shift (CLS). Professional developers actively prevent this by providing precise width and height attributes directly on the tag. This allows the browser to instantly reserve the exact layout canvas area before the image file even begins downloading over the network.
2Semantic Accessibility
Web accessibility is a strict technical requirement. Every single <img> tag must include an alt (Alternative Text) attribute. Screen readers natively parse this description and announce it to visually impaired users, providing them with the visual context they cannot see.
If an image is purely decorative (like a background swirl or a generic dividing line) and conveys absolutely no meaningful information, you must set the attribute to an empty string (alt=""). This explicitly commands the screen reader to silently ignore the node entirely, preventing it from reading out useless filenames like 'divider-line-blue-final.png'.
3Responsive Art Direction
Serving a massive 4K desktop image to a mobile phone destroys bandwidth and often ruins the visual framing. We solve this via 'Art Direction' using the <picture> wrapper.
The <picture> tag houses multiple <source> elements. By attaching a CSS query directly to the media attribute, you command the browser's engine to dynamically swap out the image file based on the user's screen size. You can also use <source type="image/webp"> to serve highly compressed, next-generation formats to modern browsers, while seamlessly failing back to a standard .jpg for older systems.
4Step-by-Step Breakdown
Introduction to HTML Media. While text forms the structural backbone of the web, images provide the critical visual context that engages users. Today, we are mastering the technical integration of media. We will explore how to embed images, ensure strict accessibility, prevent catastrophic layout shifts, and utilize advanced elements for responsive art direction.
The Void Element: img. The <img> tag is classified as a 'Void Element', meaning it is self-closing and never wraps internal content. The single most critical attribute is src (source), which strictly defines the precise URL path to your image file.
Source Definition. Which mandatory attribute must be defined on every single <img> tag to tell the browser engine exactly where to download the graphical asset from?
- →href
- →src
- →link
- →file
Mandatory Accessibility: alt Text. Web accessibility is absolutely non-negotiable. Every <img> tag must include an alt (Alternative Text) attribute. Screen readers read this description to visually impaired users, and it renders visibly if the image fundamentally fails to download.
Decorative Images. If an image is purely decorative (like a background swirl) and conveys absolutely no meaningful information, what is the technically correct way to handle its alt attribute for screen readers?
- →Omit it entirely
- →Empty string (alt="")
- →Text "none"
Preventing Cumulative Layout Shift. Cumulative Layout Shift (CLS) occurs when a downloading image brutally pushes text down the screen upon rendering. Professional developers actively prevent this by providing precise width and height attributes directly on the tag, allowing the browser to safely reserve the exact canvas area instantly.
Space Reservation. Which two attributes must be explicitly defined on an image element to reserve its layout space natively, perfectly eliminating Cumulative Layout Shift (CLS) before CSS styling applies?
- →x and y
- →size
- →width and height
- →columns and rows
Responsive Art Direction with Picture. Sometimes, scaling down a desktop image makes the subject too small. We utilize 'Art Direction' via the <picture> element. It wraps an <img> tag and utilizes multiple <source> tags. By writing CSS media queries inside the media attribute, the browser dynamically swaps the file.
Art Direction Query. Inside a <picture> element, which specific attribute on a nested <source> tag allows you to write a CSS query that tells the browser exactly when to load that specific asset variant?
- →query
- →media
- →screen
- →condition
Modern Formats: WebP and AVIF. Modern engineering offers formats like WebP and AVIF. They achieve vastly superior compression algorithms, providing smaller file sizes without noticeable visual degradation. Using <picture>, you serve these next-generation formats while automatically maintaining a .jpg fallback.
Introduction to Scalable Vectors (SVG). While JPGs are raster grids of fixed pixels, SVGs (Scalable Vector Graphics) are XML math. Because they are driven by mathematical formulas, SVGs scale infinitely without pixelation. Crucially, as raw code, you can inject them directly into HTML and modify colors dynamically via CSS.
Infinite Scalability. Which image format is structurally based on pure XML mathematical code rather than a grid of fixed pixels, allowing it to scale infinitely without ever losing crisp visual quality?
- →JPEG
- →PNG
- →SVG
- →GIF
Media Integration Mastered. Outstanding work! You mastered the architectural standards for integrating media. You understand strict accessibility with alt text, layout preservation with dimensions, art direction with <picture>, and next-gen formats like SVG. Next, we build Forms.
Prevent Layout Shift With Image Dimensions. Setting width and height lets the browser reserve space before the image loads.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1`alt` Text Should Describe Purpose, Not Just Appearance
For a photo of a graph, describing the visual ("a bar chart") is less useful than describing the takeaway ("bar chart showing Q3 revenue up 40% year over year") — write alt text for what a sighted user would actually learn from looking at it.
2Purely Decorative Images Need an Empty `alt=""`, Not a Missing Attribute
A missing `alt` attribute causes some screen readers to announce the full image filename or URL, which is worse than nothing. An explicit empty `alt=""` correctly tells assistive technology to skip the image entirely.
<img src="decorative-swirl.png" alt="">SEO Implications
- 1
`alt` Text Is a Direct Image Search Ranking Signal
Google Images relies heavily on `alt` text (alongside filename and surrounding context) to understand and rank images — missing or keyword-stuffed alt text either loses image search traffic entirely or risks being flagged as manipulative.
- 2
Unsized Images Are a Leading Cause of Poor Cumulative Layout Shift
An `<img>` without explicit `width`/`height` attributes (or CSS aspect-ratio) reserves no space before it loads, causing surrounding content to jump once the image arrives — one of the most common real-world causes of a poor CLS score.
Best Practices
Always Set Explicit `width` and `height` Attributes
Even when the image is styled responsively with CSS, setting the intrinsic `width`/`height` attributes lets the browser reserve the correct aspect-ratio space immediately, preventing layout shift before the CSS or image data even arrives.
Use `loading="lazy"` for Below-the-Fold Images Only
Lazy-loading the hero image or anything visible on initial load actually delays it and can hurt Largest Contentful Paint — reserve `loading="lazy"` for images further down the page that aren't immediately visible.
Frequent Bugs
The page visibly jumps as images finish loading, especially on a slow connection.
The `<img>` tags are missing explicit `width`/`height` attributes, so the browser can't reserve their aspect-ratio space in advance. Add the intrinsic dimensions (even if CSS later overrides the display size) to eliminate the layout shift.
A screen reader announces a long, meaningless filename like 'IMG_4821.jpg' out loud.
The `alt` attribute is missing entirely. Even a purely decorative image needs an explicit empty `alt=""` — an absent attribute causes some assistive tech to fall back to announcing the raw filename instead of silently skipping it.
Real-World Examples
Layout-Stable, Accessible Product Image
An e-commerce product image ships with meaningful alt text, explicit dimensions to prevent layout shift, and lazy loading since it sits below the page's hero section.
<img src="chair.jpg" alt="Mid-century walnut dining chair, side profile" width="400" height="400" loading="lazy">