👀 Live Preview
Resize the window to see breakpoint-based styling (simulates media on <style>):
Current width: —px

The media attribute tells the browser when a stylesheet or other linked resource should apply. You set it on <link rel="stylesheet">, <style>, and <source> (inside <picture>). The value is a CSS media query list—such as screen, print, or (min-width: 768px). If the condition matches the current device or context, the resource is used; otherwise it is skipped. This lets you load print-only CSS, target specific breakpoints, or pick responsive image sources without affecting every viewport.
When styles apply.
Primary elements.
Common media types.
e.g. min-width.
Responsive images.
Update at runtime.
media AttributeThe primary purpose of media is to tie a resource to a specific rendering context. A screen stylesheet keeps your normal layout; a print stylesheet can hide navigation and simplify colors for paper. A link with media="(min-width: 768px)" loads desktop CSS only when the viewport is wide enough, which can reduce work on small screens.
On <picture>, the same attribute on <source> picks which image file the browser should use at each breakpoint. The idea is one HTML document that adapts its presentation or assets to the user’s device, print job, or viewport size.
@media in CSSmedia on a <link> or <style> gate-keeps an entire stylesheet. @media rules inside a CSS file switch individual rule blocks. Modern sites often use one CSS file with internal @media, but separate print sheets and conditional <link media> remain useful.
Add media with a valid media query list on link, style, or source:
<head>
<link rel="stylesheet" href="screen.css" media="screen">
<link rel="stylesheet" href="print.css" media="print">
<link rel="stylesheet" href="desktop.css" media="(min-width: 768px)">
<style media="(max-width: 767px)">
nav { display: none; }
</style>
</head>
<picture>
<source srcset="hero-wide.jpg" media="(min-width: 800px)">
<img src="hero.jpg" alt="Hero">
</picture>@media).<link> and <style> is all if omitted.<link rel="stylesheet">, <style>, and <source> in <picture>.linkElement.media = "(min-width: 768px)".type attribute (MIME type) or rel on <link>.The media attribute accepts CSS media types, keywords, and full media queries:
all — Default; applies in all contexts (screen, print, etc.).screen — Styles for screens (monitors, phones, tablets).print — Styles used when printing or in print preview.speech — Intended for speech synthesizers; rarely used for author CSS in practice.(min-width: 768px) — Applies when viewport is at least 768px wide.(max-width: 599px) — Applies on narrow viewports (mobile-first breakpoints).screen and (orientation: landscape) — Combines type and feature queries.document.getElementById("printSheet").media = "print";
document.getElementById("responsiveLink").media = "(min-width: 1024px)";| Use case | Markup | Notes |
|---|---|---|
| Screen layout | <link media="screen" …> | Normal browsing |
| Print layout | <link media="print" …> | Hide nav, simplify colors |
| Desktop-only CSS | media="(min-width: 768px)" | Conditional load |
| Embedded breakpoint | <style media="(max-width: 599px)"> | Inline conditional block |
| Responsive image | <source media="(min-width: 800px)" …> | Inside <picture> |
| JS update | link.media = "all" | Runtime change |
| Element | Supported? | Notes |
|---|---|---|
<link rel="stylesheet"> | Yes | Most common use — external CSS |
<style> | Yes | Conditional embedded CSS |
<source> in <picture> | Yes | Pick image by viewport |
<link rel="preload"> (as appropriate) | Yes | Preload only when media matches |
<input>, <img>, <a> | No | Not a valid attribute on these |
media attribute vs @media in CSS| Approach | Where it lives | What it controls |
|---|---|---|
media on <link> | HTML <head> | Whether entire external stylesheet loads/applies |
media on <style> | HTML document | Whether embedded stylesheet block applies |
@media in CSS | Inside .css file | Individual rule sets within one file |
media on <source> | <picture> | Which image candidate to use |
Screen vs print styles, responsive breakpoint blocks, and changing link.media with JavaScript.
Resize the window to see breakpoint-based styling (simulates media on <style>):
Current width: —px
Link separate CSS files for on-screen viewing and printing:
<head>
<link rel="stylesheet" href="styles.css" media="screen">
<link rel="stylesheet" href="print.css" media="print">
</head>The browser evaluates each link’s media value. Only matching stylesheets participate in layout for that context. Print styles do not affect normal screen rendering.
Apply CSS only when the viewport matches a breakpoint:
<style media="(max-width: 599px)">
.sidebar { display: none; }
</style>
<style media="(min-width: 600px)">
.sidebar { display: block; width: 240px; }
</style>Media queries use the same syntax as CSS @media. Many teams prefer one file with internal @media rules, but splitting blocks or files by media can keep concerns separated.
Change when a stylesheet applies at runtime:
<link id="dynamicSheet" rel="stylesheet" href="theme.css" media="screen">
<script>
document.getElementById("dynamicSheet").media = "(min-width: 768px)";
</script>Assign a new media query string to link.media or style.media. The browser re-evaluates whether the sheet applies. The old reference showed changing to speech for screen readers—that is not a reliable accessibility strategy.
media="speech" — Screen readers are not styled through author CSS the way visual browsers are. Use semantic HTML, headings, labels, and ARIA where needed.media queries should not remove essential information from mobile users unless an equivalent path exists.prefers-reduced-motion belong in CSS; combine with thoughtful media usage for layout breakpoints.Query on link/style.
Viewport, print, etc.
CSS or image used.
Right styles per device.
The media attribute on <link>, <style>, and <source> is fully supported in all modern browsers. Media query syntax follows CSS Media Queries Level 3+ support in each engine.
Chrome, Firefox, Safari, and Edge honor media on stylesheets and picture sources.
Bottom line: Safe for print sheets, responsive links, and picture sources in production.
media="print" for dedicated print stylesheets<picture> for responsive images@media when it keeps maintenance simplermedia="speech" improves screen reader UXmedia with type on <link>media defaults to allThe media attribute connects HTML resources to the user’s context—screen, print, viewport width, and more. On stylesheets it controls when CSS applies; on <source> it helps pick the right image. Used well, it keeps pages fast, printable, and responsive.
Start with clear screen and print splits, learn media query syntax, and know when a single CSS file with internal @media is enough. Update link.media in JavaScript only when you truly need runtime control.
mediaBookmark these before wiring conditional CSS.
Context gate for CSS.
MeaningMain elements.
ScopeClassic split.
TypesSame as @media.
SyntaxRuntime updates.
Dynamic<style> block, or <source> in <picture> should apply. Values include screen, print, and queries like (min-width: 768px).<link rel="stylesheet">, <style>, and <source> inside <picture>. Not valid on arbitrary elements like <div> or <input>.media attribute controls an entire stylesheet. @media inside CSS toggles rule sets within one file.<link rel="stylesheet" href="print.css" media="print"> or <style media="print">…</style>. Rules apply in print preview and when printing.linkElement.media = "(min-width: 768px)" on an HTMLLinkElement, or styleElement.media on HTMLStyleElement.Practice screen vs print styles, responsive queries, and dynamic link.media in the Try It editor.
5 people found this page helpful