The HTML media attribute
Quick answer
The HTML media attribute specifies the media query the resource applies to. It is used on the <link>, <source> and <style> elements.
Overview
The media attribute applies the resource to matching media only. It is used on document head elements like <meta>, <link> and <script>.
It belongs to the document head — metadata, stylesheet and script loading, encoding and resource hints. These attributes shape how the page is interpreted and how efficiently its resources load.
Syntax
<link rel="stylesheet" href="print.css" media="print">
Values
| Value |
|---|
A media query, e.g. (min-width: 600px) or print. |
Best practices
- Declare the character encoding with <meta charset="utf-8"> first in the <head>.
- Load scripts with defer (or as modules) so they do not block parsing.
- Protect third-party resources with integrity and crossorigin (Subresource Integrity).
- Use resource hints like preload deliberately, paired with the right as value.
Frequently asked questions
What does the media attribute do?
Applies the resource to matching media only.