skip to content

Media and Embedding

Getting images, video, audio, and third-party frames into a page with the right attributes: responsive sources, loading and priority hints, captions, and iframe sandboxing. Interviewers ask because these attributes are where correctness, accessibility, and page weight all meet.

part ofHTMLoverview, primer and where to startread it →
on this pageshow

explore

questions

29

In HTML, what should an img element's alt attribute say, and when is alt="" (an empty value) the correct choice?

level: juniorimportance: must knowfreq 85%

answer

  1. substitution, not description
  2. purpose in context decides it
  3. decorative gets a deliberate value
  4. empty string is not the same as absent
  5. screen readers read the file name otherwise

basics

~20 s

Alt text states the image's purpose in its context, not its appearance — what a reader would lose if the image never rendered. Purely decorative images take alt="" so assistive technology skips them instead of announcing a file name.

solid answer

~50 s

Alt text is a replacement for the image, not a description of it: write what a reader would lose if the image failed to load. That makes it context-dependent — the same product photo might be `alt="Blue running shoe, side view"` in a catalogue and `alt=""` next to a paragraph that already names it. Leave out "image of" or "photo of", since a screen reader already announces that it is an image. When the image carries no information — a divider, a decorative flourish, an icon beside text that already repeats the same word — use `alt=""`, which maps the image to a presentational role so it is skipped. That empty value is a deliberate statement, and it is not the same as leaving `alt` off entirely: with no attribute the image has no author-supplied text at all, and screen readers commonly fall back to reading the file name or URL.

go deeper

for a junior

Be ready to say plainly that alt replaces the image rather than describing it, and to write alt="" for a purely decorative image. Knowing that an empty alt differs from no alt at all already puts you ahead.

for a middle

Explain the mechanics: an empty alt maps the image to a presentational role so it never reaches the accessibility tree, while a missing attribute leaves an unnamed image that screen readers repair by reading the file name. Show that the right text changes with surrounding content.

for a senior

Demonstrate judgment on real pages — spotting redundant alt beside a caption, functional images inside links, and content images buried in CSS backgrounds where no alternative can attach. Interviewers want to hear you reason about a specific page, not recite a rule.

for a principal

Own the fact that alt quality is an authoring-process problem, not a markup problem. Be ready to discuss where alt text comes from in a CMS, who is accountable for it, why automated checkers can only detect absence and never correctness, and what you enforce in review versus in tooling.

## What `alt` actually is The `alt` attribute on `<img>` holds a **text alternative**: the words that take the image's place for anyone who does not perceive the image. That is screen reader users, but also anyone whose image request failed, anyone who blocked images, and any tool that consumes the markup as text. The model that keeps you out of trouble is substitution: remove the image, put the alt text where it was, and read the page. If nothing was lost, the alt text is right. This is why alt text is *not* a description of what the picture looks like. A photograph of a smiling person holding a card is described one way in a photo archive and another way on a bank's "activate your card" page. The pixels are identical; the purpose is not. ## Context decides the text There is no correct alt text for an image in the abstract — only for an image *in a place*. Three common cases: - The image carries information not present in nearby text. Write that information: `alt="Sales fell from 40% to 12% between January and June"`. - The image duplicates adjacent text. It is redundant for a non-visual reader, so `alt=""` is usually right — otherwise the same sentence is announced twice. - The image is functional (inside a link or a button). The alternative describes the destination or the action, not the artwork. ```html <!-- informative --> <img src="chart.png" alt="Revenue doubled between Q1 and Q4 2025"> <!-- redundant with the visible label next to it --> <a href="/cart"><img src="cart.svg" alt=""> Cart</a> <!-- functional: no visible text, so the alt is the link text --> <a href="/"><img src="logo.svg" alt="Acme home"></a> ``` ## Decorative images and the empty alt An image is decorative when removing it costs a non-visual reader nothing: rules, spacers, background flourishes, mood photography chosen purely for texture, an icon that sits beside text saying the same thing. Give those `alt=""`. HTML maps an `<img>` with an empty `alt` to a presentational role, so the element does not surface in the accessibility tree at all and a screen reader moves straight past it. Write it as a genuinely empty string — `alt=""`, not `alt=" "` with a space, and not `alt="spacer"`. A single space is content in some implementations, and a filler word is noise announced on every page. ## Empty alt is not a missing alt This distinction is the reason interviewers ask the question at all. - `alt=""` says: *the author considered this image and decided it needs no alternative*. It is skipped. - No `alt` attribute says: *nothing*. The image is still exposed as an image with no author-supplied text, and screen readers typically repair the gap by announcing the `src` file name — so users hear `hero_final_v3_2x.png`, which is worse than silence and worse than a real description. The HTML specification requires `alt` on `img` outside a few narrow cases, so a validator or a linter flags the missing attribute. An empty `alt` passes because it is an explicit, meaningful value. ## Writing alt that reads well Keep it short — roughly a sentence for a typical informative image. It is announced linearly with no way to skim, so front-load the point. Skip "image of", "picture of", "graphic showing": the role is already announced. End the sentence's punctuation naturally so it does not run into the next line of content. Never use alt as an SEO keyword field: search engines and screen readers read the same attribute, and keyword stuffing degrades the experience for the users the attribute exists for. An image of text — a wordmark, a quote rendered as a graphic, a screenshot of an error message — needs alt that reproduces the text, since that text is the content. ## Common defects The recurring ones are: alt copied from the file name; alt describing colour and composition on a page where only the fact matters; the same alt string reused across every image in a gallery; a decorative image left with the attribute off; and content images pushed into CSS backgrounds, where no alternative can be attached at all — content images belong in `<img>` precisely so they can carry one.

  • Someone argues that a missing alt attribute is fine because the image is decorative anyway. What do you tell them?
    That it produces the opposite result. A decorative image with no `alt` is still exposed as an image with no name, so a screen reader typically announces the file name — `bg_texture_02.png` on every page. `alt=""` is what removes it from the accessibility tree. It also happens to be the difference between passing and failing HTML validation.
  • How long should alt text be, and what do you do when an image genuinely needs a paragraph?
    Aim for around a sentence — it is announced linearly with no skimming. When the image really needs more, the alt gives a short identification and the long description lives in real page content next to it: a caption, a visible "described below" section, or a text equivalent such as a data table. Do not reach for `longdesc`; it is obsolete.
  • Does alt text help SEO, and should that change what you write?
    Search engines do read `alt`, which is exactly why it should stay honest. It is the same string a screen reader announces, so keyword stuffing buys little ranking value and directly degrades the experience for the users the attribute exists for. Accurate, purpose-driven alt text is both the accessible answer and the one crawlers reward.

saying these in an interview costs you the question

  • Says alt should describe how the image looks
  • Thinks omitting alt is the same as alt=""
  • Starts every alt with 'image of' or 'photo of'
  • Treats alt as a place for SEO keywords
  • Claims one correct alt exists regardless of context

context

open as a page

Why should an HTML <img> carry width and height attributes even when CSS controls its rendered size, and what does the browser do with those two numbers?

level: juniorimportance: must knowfreq 62%

basics

~20 s

The width and height attributes give the browser the image's aspect ratio before the file arrives, so it can reserve a correctly proportioned box during the first layout instead of collapsing the space and shoving content down when the image lands.

open as a page

In an HTML <picture> element, which child actually renders the image, and where do attributes like alt, width, height and class belong?

level: juniorimportance: must knowfreq 58%

basics

~20 s

The img renders; picture and source only offer candidates to it. So alt, width, height, class and loading all belong on the img, which must be the last child. A picture with no img displays nothing at all.

open as a page

On an HTML <img>, what do the srcset and sizes attributes do, and what is the difference between a w descriptor and an x descriptor inside srcset?

level: juniorimportance: must knowfreq 72%

basics

~20 s

srcset lists interchangeable files for one image: a w descriptor states a file's intrinsic pixel width, an x descriptor targets a device pixel ratio. sizes declares how wide the image will render, which the browser needs to choose among w candidates.

open as a page

What does adding the sandbox attribute to an HTML <iframe> do, and how do tokens such as allow-scripts and allow-forms change that?

level: middleimportance: must knowfreq 70%

basics

~20 s

The sandbox attribute revokes an iframe's capabilities by default: scripts, form submission, popups, top-level navigation, downloads and modal dialogs are blocked, and the frame runs in a unique opaque origin. Each allow-* token grants one capability back.

open as a page

What does the loading="lazy" attribute on an HTML <img> do, and why is putting it on the large image at the top of the page a mistake?

level: middleimportance: must knowfreq 70%

basics

~20 s

loading="lazy" defers an image's download until layout shows it is near the viewport. On the hero image that delays the page's most visible paint, because the request cannot start until the browser has laid the page out.

open as a page

When should you reach for HTML's <picture> element instead of putting srcset on a plain <img>?

level: middleimportance: must knowfreq 72%

basics

~20 s

Use picture when the image itself must change: a different crop per breakpoint, or an alternative file format the browser may not decode. Use srcset on a plain img when the same image only needs different pixel sizes and the browser should choose.

open as a page

Why does an <img> with a w-descriptor srcset need a sizes attribute at all, rather than the browser simply measuring how wide the element ends up being?

level: middleimportance: must knowfreq 62%

basics

~10 s

sizes declares the image's rendered width in CSS pixels. The browser needs it because it picks a candidate while parsing the markup, before stylesheets and layout have run; with sizes missing it assumes 100vw.

open as a page

An HTML `<video autoplay>` silently refuses to start in Chrome and Safari. Explain the browser autoplay policy and which attributes make autoplay actually work.

level: middleimportance: must knowfreq 72%

basics

~20 s

Browsers block autoplay of audible media until the user has interacted with the page. Adding the muted attribute lets a video autoplay, and playsinline keeps playback inline on iPhone. Playing with sound requires a real user gesture such as a click.

open as a page

Why do the <iframe> sandbox tokens allow-scripts and allow-same-origin, used together on content served from your own origin, amount to no sandbox at all?

level: seniorimportance: must knowfreq 52%

basics

~20 s

Together those tokens make the framed document same-origin with the embedder and let it run script, so its code can reach the parent document, delete the sandbox attribute from its own iframe element, and reload itself unsandboxed.

open as a page

In HTML, what does the poster attribute on `<video>` do, and what do `preload="none"`, `preload="metadata"` and `preload="auto"` tell the browser?

level: juniorimportance: should knowfreq 52%

basics

~20 s

poster names an image the browser shows in the video's frame until playback produces a picture. preload is a hint about how much of the media file to fetch before play: none fetches nothing, metadata fetches just duration and dimensions, auto invites the browser to buffer the whole thing.

open as a page

When should an image be wrapped in a figure element with a figcaption, and how should the alt text relate to the caption?

level: middleimportance: should knowfreq 50%

basics

~20 s

Use figure with figcaption when content is referenced as a self-contained unit that carries a visible caption. The caption is visible to everyone; alt serves only readers who cannot see the image, so the two must not repeat each other word for word.

open as a page

An icon is rendered as inline SVG markup rather than as an img element. What markup makes a screen reader announce it as an image with a name, and what do you do when the same icon is decorative?

level: middleimportance: should knowfreq 40%

basics

~20 s

An inline svg has no alt attribute. Give a meaningful one role="img" plus aria-label (or an svg title element as the first child) so it is announced as a named image; give a decorative one aria-hidden="true" so it is skipped entirely.

open as a page

Why is the title attribute on an img element not a substitute for its alt attribute?

level: middleimportance: should knowfreq 45%

basics

~20 s

The title attribute renders only as a hover tooltip, so keyboard and touch users never see it, and screen reader exposure of title is inconsistent and often switched off. Alt is the defined text alternative for an image; title is optional advisory information.

open as a page

On an HTML <iframe>, what does the allow attribute control, and how is it different from the sandbox attribute?

level: middleimportance: should knowfreq 38%

basics

~20 s

The allow attribute delegates powerful features such as camera, microphone, geolocation, fullscreen and payment to an embedded frame, which otherwise gets none of them cross-origin. sandbox is a separate attribute that strips generic document capabilities like scripting, forms and navigation.

open as a page

What does the decoding attribute on an HTML <img> control, and how does decoding="async" differ from loading="lazy"?

level: middleimportance: should knowfreq 32%

basics

~20 s

decoding hints at how the browser turns downloaded image bytes into displayable pixels: async allows that work to happen off the critical path, sync asks for it before the image is presented, and auto leaves the choice to the browser. It never affects when the file is fetched.

open as a page

What does the type attribute on a <source> inside an HTML <picture> do, and why does the order of the source elements matter when offering AVIF, WebP and JPEG?

level: middleimportance: should knowfreq 56%

basics

~20 s

type declares a candidate's MIME type, so a browser that cannot decode that format skips the source without downloading it. Selection stops at the first supported source, so sources must be listed best-format first, with the universally supported file on the trailing img.

open as a page

Given <img srcset="p-400.jpg 400w, p-800.jpg 800w, p-1600.jpg 1600w" sizes="400px">, how does a browser combine sizes with the device pixel ratio to choose a candidate, and can you rely on it always picking the same one?

level: middleimportance: should knowfreq 48%

basics

~20 s

The browser resolves sizes to CSS pixels and multiplies by the screen's device pixel ratio to get the image pixels it needs — 400px at 2x asks for about 800 — then takes a candidate that meets it. Selection is a hint, so the exact file is not guaranteed.

open as a page

An HTML `<video>` has three `<source>` children in different formats. Explain how the browser picks one, what the `type` attribute contributes, and when the content between the `<video>` tags is displayed.

level: middleimportance: should knowfreq 46%

basics

~20 s

The browser walks the source children in document order and plays the first one it believes it can decode — first playable wins, not best. The type attribute lets it skip unsupported candidates without fetching them. Content between the video tags is fallback only for browsers with no video support at all.

open as a page

How do you add captions to an HTML `<video>` using the `<track>` element, and what distinguishes `kind="captions"` from `kind="subtitles"`?

level: middleimportance: should knowfreq 44%

basics

~20 s

Add a void track child to the video pointing at a WebVTT file, with kind, src, srclang, label and optionally default. Captions convey non-speech audio too and assume the viewer cannot hear; subtitles are a transcription or translation for someone who can hear the audio.

open as a page

How does the text alternative differ between an image that is the entire content of a link, an image of text such as a wordmark, and a complex data chart?

level: seniorimportance: should knowfreq 48%

basics

~20 s

A functional image's alternative describes the destination or action, not the picture. An image of text reproduces that text verbatim. A complex chart gets a short alt identifying it plus a full text equivalent in real page content, because no attribute holds a paragraph well.

open as a page

After adding sandbox to a third-party <iframe>, its form stops submitting, its "open in new tab" links do nothing, and its export button downloads nothing. Which sandbox tokens restore each behaviour, and what happens to a popup the frame does open?

level: seniorimportance: should knowfreq 44%

basics

~10 s

Add allow-forms for submission, allow-popups for new-tab links and window.open, and allow-downloads for the export. A popup opened from a sandboxed frame inherits the frame's sandbox flags unless you also grant allow-popups-to-escape-sandbox.

open as a page

In HTML, how do fetchpriority="high" on an <img> and a <link rel="preload" as="image"> in the head differ in what they ask the browser to do for a page's main hero image?

level: seniorimportance: should knowfreq 40%

basics

~20 s

fetchpriority="high" raises the urgency of a fetch the browser was going to make anyway; <link rel="preload" as="image"> makes the browser start a fetch it would not otherwise have discovered yet. One reorders, the other discovers earlier.

open as a page

A page uses an HTML <picture> with several art-directed <source> elements, and the narrow mobile crop keeps appearing on wide desktop screens. Given how the browser selects a source, what are the likely causes?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Almost always source order or overlapping media conditions: the walk stops at the first matching source, so a broad condition listed above a narrow one wins everywhere. Other causes are a source with no matching media at all, srcset misspelled as src, and markup order not matching the intended priority.

open as a page

A product page ships images as <img srcset="…400w … 2000w" sizes="100vw">, but CSS renders each one inside a 480px-wide column on desktop. What is going wrong, how would you confirm which candidate the browser actually chose, and how do you fix sizes so it does not drift again?

level: seniorimportance: should knowfreq 44%

basics

~20 s

The sizes value promises a full-viewport image, so the browser requests a candidate sized for the whole screen instead of the 480px column — often four times the needed bytes. Confirm via the element's currentSrc and the network panel, then rewrite sizes to match the real column width.

open as a page

A team drops the `controls` attribute from `<video>` and builds their own control bar. What does the native control bar provide that the custom one now has to reproduce?

level: seniorimportance: should knowfreq 38%

basics

~20 s

The native bar is a fully keyboard-operable, screen-reader-labelled, localised player with play, seek, volume, fullscreen, picture-in-picture, a captions menu and OS media-key integration. Removing it hands all of that to the team, and most custom bars rebuild only the visible parts.

open as a page

What does the srcdoc attribute on an HTML <iframe> do, and how does it interact with the src attribute?

level: juniorimportance: nice to knowfreq 24%

basics

~20 s

srcdoc holds the framed page's HTML inline in the attribute instead of fetching it from a URL. When srcdoc is present it wins over src, which is left as a fallback, and the resulting document inherits the embedding page's origin unless you also sandbox it.

open as a page

Art direction with HTML's <picture> means shipping a separate crop per breakpoint. Across a large site, how do you decide which images earn that, and how do you keep the markup from becoming unmaintainable?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Reserve art direction for a small set of images where a single crop genuinely fails — heroes with an off-centre subject, images carrying text. Everything else gets one well-composed file with srcset. Generate the markup from a component so crops, formats and breakpoints come from one source.

open as a page

Across a large site, sizes values on <img> elements drift out of sync with the CSS that actually lays those images out. How would you keep responsive-image markup honest at that scale, and when would you decide an image should not carry a srcset at all?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Treat sizes as duplicated layout knowledge: generate it from the same breakpoint and container tokens the CSS uses, own it per layout slot in a component rather than per image, and check it automatically. Skip srcset where the rendered box is fixed or the image is tiny.

open as a page