## HTML `` media Attribute
The `media` attribute of the `` (anchor) tag specifies the media type or device for which the target URL is optimized. This attribute informs the browser and user agents about the intended target device (such as a smartphone, a screen reader, or a printer) so they can handle the linked resource more efficiently.
---
## Definition and Usage
* **Purpose:** The `media` attribute defines the target device or media type optimized for the linked document.
* **Dependency:** This attribute is only functional and valid when the `href` attribute is present on the `` tag.
* **HTML5 Feature:** The `media` attribute on the `` tag was introduced in HTML5.
---
## Browser Support
The `media` attribute is supported by all major modern web browsers:
* Google Chrome
* Mozilla Firefox
* Safari
* Microsoft Edge
* Opera
*Note: While browsers support the syntax, the actual behavior (such as pre-fetching or styling based on the media query) depends on the browser's implementation and the specific device context.*
---
## Syntax
```html
Link Text
```
### Example
The following example demonstrates a link optimized specifically for high-resolution printing:
```html
Print-Optimized Version (300 DPI)
```
---
## Media Query Operators
You can combine multiple conditions in the `media` attribute using logical operators:
| Operator | Description |
| :--- | :--- |
| `and` | Specifies an **AND** operator. Both conditions must be true. |
| `not` | Specifies a **NOT** operator. Negates the query. |
| `,` (comma) | Specifies an **OR** operator. Either condition can be true. |
---
## Target Devices (Media Types)
The following values specify the general category of device for which the destination URL is designed:
| Value | Description |
| :--- | :--- |
| `all` | **Default.** Suitable for all devices. |
| `aural` | Speech synthesizers and screen readers. |
| `braille` | Braille tactile feedback devices. |
| `handheld` | Handheld devices (typically small screens, limited bandwidth). |
| `projection` | Projectors. |
| `print` | Print preview mode or printed pages. |
| `screen` | Computer screens, tablets, and smartphones. |
| `tty` | Teletypes and similar media using a fixed-pitch character grid. |
| `tv` | Television-type devices (low resolution, limited scrolling capability). |
---
## Media Features (Values)
Media features allow you to target more specific characteristics of the output device. Most of these features can be prefixed with `min-` or `max-` to express "greater than or equal to" and "less than or equal to" constraints.
| Feature | Description | Example |
| :--- | :--- | :--- |
| `width` | Specifies the width of the target display area. | `media="screen and (min-width:500px)"` |
| `height` | Specifies the height of the target display area. | `media="screen and (max-height:700px)"` |
| `device-width` | Specifies the physical width of the target device screen/paper. | `media="screen and (device-width:500px)"` |
| `device-height` | Specifies the physical height of the target device screen/paper. | `media="screen and (device-height:500px)"` |
| `orientation` | Specifies the orientation of the target display.
Possible values: `"portrait"` or `"landscape"`. | `media="all and (orientation: landscape)"` |
| `aspect-ratio` | Specifies the width-to-height ratio of the target display area. | `media="screen and (aspect-ratio:16/9)"` |
| `device-aspect-ratio` | Specifies the physical device-width to device-height ratio. | `media="screen and (device-aspect-ratio:16/9)"` |
| `color` | Specifies the bits per color component of the target display. | `media="screen and (color:3)"` |
| `color-index` | Specifies the number of colors the target display can handle. | `media="screen and (min-color-index:256)"` |
| `monochrome` | Specifies the bits per pixel in a monochrome frame buffer. | `media="screen and (monochrome:2)"` |
| `resolution` | Specifies the pixel density of the target display/paper (in `dpi` or `dpcm`). | `media="print and (resolution:300dpi)"` |
| `scan` | Specifies the scanning method of a TV display.
Possible values: `"progressive"` or `"interlace"`. | `media="tv and (scan:interlace)"` |
| `grid` | Specifies whether the output device is a grid (like a TTY terminal) or bitmap.
Possible values: `"1"` for grid, `"0"` otherwise. | `media="handheld and (grid:1)"` |
---
## Considerations and Best Practices
1. **Informational Purpose:** The `media` attribute on `` tags is primarily advisory. It helps search engines, browsers, and assistive technologies understand what kind of resource lies behind the link, but it does not prevent a user on a different device from clicking the link.
2. **SEO and Performance:** Search engine crawlers can use this attribute to better index device-specific versions of your pages.
3. **Contrast with ``:** Do not confuse the `` tag's `media` attribute with the `` tag's `media` attribute. While `` actively controls when a stylesheet is applied, `` merely describes the target destination.
π Categories
- β‘ JavaScript (1589)
- π PHP (872)
- π Python3 (810)
- π HTML (691)
- βοΈ C# (650)
- π Python (594)
- β Java (552)
- βοΈ PyTorch (534)
- π§ Linux (472)
- βοΈ C (432)
- π¦ jQuery (406)
- π¨ CSS (377)
- π XML (259)
- π¦ jQuery UI (231)
- π― Bootstrap (220)
- βοΈ C++ (215)
- π °οΈ Angular (205)
- π HTML DOM (201)
- π΄ Redis (188)
- π Web Building (142)
- π Vue.js (141)
- π R (131)
- πΌ Pandas (124)
- ποΈ SQL (105)
- βοΈ Docker (86)
- βοΈ TypeScript (73)
- βοΈ Highcharts (70)
- π AI Agent (70)
- βοΈ React (68)
- π Node.js (65)
- βοΈ Machine Learning (60)
- π Git (59)
- π΅ Go (58)
- π Markdown (58)
- π’ NumPy (55)
- π§ͺ Flask (54)
- βοΈ Scala (53)
- ποΈ SQLite (52)
- π JSTL (52)
- βοΈ VS Code (51)
- π MongoDB (49)
- π Perl (48)
- π Ruby (47)
- π Matplotlib (47)
- βοΈ Uncategorized (46)
- π Swift (46)
- ποΈ PostgreSQL (46)
- βοΈ Data Structures (46)
- π Playwright (46)
- π iOS (45)
- ποΈ MySQL (44)
- βοΈ LangChain (43)
- π FastAPI (40)
- βοΈ Ionic (38)
- π Design Patterns (37)
- βοΈ Eclipse (37)
- π¨ CSS3 (34)
- π Lua (34)
- βοΈ Codex (34)
- πΈ Django (32)
- βοΈ OpenCV (32)
- π Rust (31)
- π JSP (31)
- βοΈ Claude Code (31)
- π Pillow (30)
- βοΈ OpenCode (28)
- π AI Skills (27)
- π Flutter (26)
- π Maven (26)
- π¨ Tailwind CSS (25)
- π§ TensorFlow (25)
- π Servlet (24)
- π Dart (23)
- π Assembly (23)
- βοΈ Memcached (22)
- βοΈ SVG (22)
- βοΈ Electron (22)
- π NLP (22)
- π Regex (21)
- π Android (20)
- π£ Kotlin (19)
- π Julia (19)
- π SOAP (17)
- π Selenium (17)
- π PowerShell (17)
- π Sass (16)
- π HTTP (16)
- π Zig (15)
- π AI (15)
- π AJAX (14)
- π Swagger (14)
- βοΈ Scikit-learn (13)
- βοΈ ECharts (13)
- βοΈ Chart.js (13)
- βοΈ Cursor (13)
- βοΈ SciPy (12)
- π RDF (12)
- π Ollama (12)
- π Next.js (12)
- π Plotly Dash (12)
- π JSON (11)
- π RESTful API (11)
- π WSDL (9)
- βοΈ CMake (8)
- π Firebug (7)
- π Nginx (6)
- βΈοΈ Kubernetes (6)
- π Jupyter (6)
- π LaTeX (4)
- π UniApp (4)
- ποΈ SQL Server (1)
YouTip