HTML media Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The media attribute tells the browser when a resource should apply. Put it on <link rel="stylesheet">, <style>, or <source> (often inside <picture>). The value is a CSS media type or media query list — for example screen, print, or (min-width: 768px).

If the condition matches the current device or context, the stylesheet or source is used; otherwise it is skipped for styling (or the browser moves to the next <source>). Default for link and style is all when omitted.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

Not the same as CSS @media.

media on HTML gates an entire stylesheet or source. @media inside a CSS file switches individual rule blocks. See CSS @media.

HTML Syntax

syntax.html
<link rel="stylesheet" href="…" media=" query">
<style media=" query">…</style>
<source srcset="…" media=" query">

query is a media type, a media feature query, or a comma-separated list of either.

Attribute Values

Common media types and query patterns you can use:

ValueDescriptionTry it
allDefault. Applies in every context (screen, print, and more)Try it
screenApplies on screens (monitors, phones, tablets)Try it
printApplies when printing or in print previewTry it
(min-width: …)Applies when the viewport is at least that wideTry it
(max-width: …)Applies when the viewport is at most that wideTry it
Combined querye.g. screen and (orientation: landscape)Try it
Deprecated media types.

Older types like aural, tty, tv, and handheld are obsolete. Stick to all, screen, print, and modern feature queries.

Live Preview

Change the media value on a live <style> block. The note shows whether the query matches right now.

The demo box uses a <style media="…"> block. Resize the window to test width queries.

Conditional style

Styles apply when media matches

media="screen" — currently matches

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Screen and Print Styles

Use separate media values so print CSS does not change the screen layout.

HTML
<style media="screen">
  .banner { color: navy; }
  .print-only { display: none; }
</style>

<style media="print">
  .screen-only { display: none; }
  .print-only { display: block; }
</style>

<p class="banner screen-only">Screen styles active</p>
<p class="print-only">Print styles active</p>
Try It Yourself

How It Works

  1. Two style blocks. One targets screen, the other print.
  2. Only matching CSS applies. On screen, print rules are ignored for layout.
  3. Print preview switches context. The browser then uses the print block.
  4. Same idea with <link>. Use media="print" on a separate stylesheet file.

Example 2 — Width Media Query on <style>

Apply a whole style block only when the viewport matches a breakpoint.

HTML
<style media="(max-width: 599px)">
  .box { background: #fecaca; color: #991b1b; }
</style>

<style media="(min-width: 600px)">
  .box { background: #bbf7d0; color: #166534; }
</style>

<p class="box">Resize the window to switch styles.</p>
Try It Yourself

How It Works

  1. Query on the element. media="(max-width: 599px)" gates the whole block.
  2. One match at a time. Narrow and wide queries cover different widths.
  3. Resize to test. The active stylesheet switches as the viewport changes.
  4. Alternative: one CSS file with internal @media rules.

Example 3 — Responsive Image with <source media>

Inside <picture>, pick an image source when a media query matches.

HTML
<picture>
  <source
    srcset="wide.svg"
    media="(min-width: 700px)">
  <img
    src="narrow.svg"
    alt="Demo banner that changes at 700px width"
    width="400"
    height="160">
</picture>
Try It Yourself

How It Works

  1. List sources first. The browser checks each <source> in order.
  2. media decides eligibility. If (min-width: 700px) matches, that srcset is used.
  3. Fallback <img>. Always include an img with alt for accessibility.
  4. Art direction. This is for different crops or layouts, not only file size (see also srcset).

Edge Cases & Pitfalls

Most surprises come from mixing up HTML media with CSS @media, or expecting downloads to stop.

compare

HTML media vs CSS @media

The attribute gates a whole stylesheet or source. @media switches rules inside one CSS file. Both are valid; pick one clear strategy.

performance

Non-matching CSS may still download

Browsers often fetch print or breakpoint sheets anyway, but they usually do not block rendering like the main screen stylesheet.

a11y

Do not rely on speech

Screen readers do not consume author CSS the way visual browsers do. Use semantic markup and ARIA, not media="speech".

picture

Always keep an <img> fallback

Inside <picture>, <source media> alone is not enough. The img provides the default image and accessible name.

legacy

Skip obsolete media types

handheld, tv, tty, and similar types are deprecated. Prefer feature queries like (max-width: …).

Browser Support

media is an HTML attribute for link, style, and source in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

media

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support media on stylesheets and picture sources.

97% Global support (approx.)
Google Chrome 4+
Full support
Mozilla Firefox 4+
Full support
Apple Safari 4+
Full support
Microsoft Edge 12+
Full support
Opera 15+
Full support
Samsung Internet 4+
Full support
Internet Explorer 9+
Supported
media Widely supported

Bottom line: Safe to use for print sheets, breakpoints, and picture art direction. Prefer modern feature queries over obsolete media types.

Key Takeaways

  • Role: apply a stylesheet or source only when a media condition matches.
  • Elements: <link>, <style>, and <source>.
  • Common values: screen, print, and width queries.
  • vs @media: HTML gates a whole sheet; CSS @media gates rules inside a file.

One line: media answers “when should this stylesheet or source apply?”

Frequently Asked Questions

It sets when a stylesheet or picture source should apply. The value is a CSS media type (like screen or print) or a media query (like (min-width: 768px)). If the condition matches, the resource is used.
link (especially for stylesheets), style, and source (inside picture, and also usable with video/audio sources). Default for link and style is all when the attribute is omitted.
They share the same query syntax, but media on link or style decides whether an entire stylesheet applies. @media inside a CSS file switches individual rule blocks within one file.
Use media="print" on a link or style element. Those styles apply when printing or in print preview, and they do not change the normal screen layout.
Often yes. Browsers may still fetch it, but a non-matching media query typically does not block rendering the way the main screen stylesheet does.
No. speech is a valid media type, but screen readers do not apply author CSS like visual browsers. Rely on semantic HTML and ARIA instead.

Next: method

Choose how a form sends data with GET or POST.

method tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful