Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

The media attribute — conditional resource applicability

The media attribute contains a media query list that determines when styles apply, a source is considered, or theme-color metadata is used. Its exact behavior depends on the element: link, style, source, or meta.

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

Syntax

media="<media-query-list>"

media is not a global attribute. It accepts a valid media query list, such as screen, print, or (min-width: 600px). The condition is evaluated against the user's environment; it does not declare the resource's MIME type.

On style and on link referencing an external resource, such as a stylesheet, it conditions the application of styles or the resource, subject to other requirements. Omitting the attribute means all. Internal @media rules still apply. For a link that creates a hyperlink, the attribute is advisory only.

On source, a nonmatching condition causes selection to proceed to the next source. Inside picture, image sources use srcset, and selection responds to environmental changes. Inside audio and video, the resource uses src; media is evaluated only once during a given run of the resource selection algorithm, so resizing the window alone does not switch playback.

On meta, authors may use media only with name="theme-color"; with other names it has no processing effect and must not be added. The media values must be unique among the document's theme-color declarations. The color is a suggestion for browser UI, not a style for page content.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>media demo</title>
  <style>
    p { color: black; }
  </style>
  <style media="screen">
    p { color: blue; }
  </style>
  <style media="print">
    p { color: red; }
  </style>
</head>
<body>
  <p>media demo</p>
</body>
</html>

Result

Screen: blue “media demo” text. Print preview: red “media demo” text.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources