Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML concept: responsive

Responsive HTML gives the browser image candidates and conditions so selection can fit viewport and display density.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

picture/source conditions + srcset candidates + sizes → selected image source

picture orders source elements before a fallback img; the browser evaluates their media and type in order.

srcset uses 1x and 2x density descriptors or w widths; width candidates require a matching sizes description.

width and height reserve an aspect ratio, while accurate alt conveys meaning independently of the selected file.

The example matches media, awaits decode(), and distinguishes the 4x2 source from the 1x1 fallback while reserving 40x20.

Examples

<!doctype html>
<meta charset="utf-8">
<picture>
  <source id="candidate" media="(min-width: 1px)" srcset="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0IiBoZWlnaHQ9IjIiPjwvc3ZnPg==">
  <img id="image" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxIiBoZWlnaHQ9IjEiPjwvc3ZnPg==" width="40" height="20" alt="Demo">
</picture>
<output id="result"></output>
<script>
const image = document.querySelector("#image");
const source = document.querySelector("#candidate");
image.decode().then(() => {
  result.textContent = `media=${matchMedia(source.media).matches}; natural=${image.naturalWidth}x${image.naturalHeight}; reserved=${image.width}x${image.height}`;
});
</script>

Result

media=true; natural=4x2; reserved=40x20

The result verifies selection in this viewport; it does not compare network cost, other DPR values, or resizing.

Sources