- HTML Tutorial
- HTML - Home
- HTML - Introduction
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - media Attribute
The HTML media attribute is used to specify a hint of the media for which the linked resource was designed.
By using the media attribute, you can apply different styles to different devices, screen sizes, or conditions. Its value is a media query, which is default to all if the media attribute is missing.
The media attribute is allowed with an <source> if its parent element is an <picture> element but is not allowed if its parent element is an <audio> or <video> element.
Syntax
<tag media = "value"></tag>
Value of media attribute can be.
- media = "all" ( Suitable for all devices )
- media = "screen" ( Intended primarily for screens )
- media = "speech" ( Intended for screen readers )
- media = "print" ( Intended for print preview mode or printed documents )
If you want to mention multiple hints for linked resource you can use boolean operators, the keyword 'and' specifies AND operator, keyword , (comma) specifies OR operator and keyword 'not' specifies NOT operator.
Applies On
Below listed elements allow using of the HTML media attribute
Element | Description |
---|---|
<a> | HTML <a> tag is used attach hyperlink in document. |
<area> | HTML <area> tag specifies the areas of the image, mapping that can be clicked on or are active areas that are linked to by hyperlinks. |
<link> | HTML <link> tag specifies relationship between the current document and an external resource. |
<source> | HTML <source> denotes the absence of both a closing tag and any content in the tag. |
<style> | HTML <style> tag is used to introduce CSS styles inside HTML file. |
Examples of HTML media Attribute
Below examples will illustrate the HTML media attribute, where and how we should use this attribute!
Define media Attribute
In this example we are using the media attribute with the 'link' element to apply different CSS stylesheets for different screen sizes. For screen widths upto 599 px stylesheet 'mobile.css' is used and for screen width 600 px and above 'style.css' is used.
<!DOCTYPE html> <html lang="en"> <head> <title> Media Attribute Example </title> <link rel="stylesheet" href="style.css" media="screen and (min-width: 600px)"> <link rel="stylesheet" href="mobile.css" media="screen and (max-width: 599px)"> </head> <body> <h1>Hello, World!</h1> <p> This is an example of using the media attribute. </p> </body> </html>
Specific style based on Screen Size
In this example we used media attribute with style tag. In the output, The background color of the body will be light blue for screens that are at least 600 pixels wide and the background color of the body will be light coral for screens that are less than 600 pixels wide.
<!DOCTYPE html> <html lang="en"> <head> <title> Media Attribute Example </title> <style media="screen and (min-width: 600px)"> body { background-color: lightblue; } </style> <style media="screen and (max-width: 599px)"> body { background-color: lightcoral; } </style> </head> <body> <h1>Hello, World!</h1> <p> This is an example of using the media attribute with style elements. </p> </body> </html>
Link with media Attribute
In this example media attribute is used with the anchor tag to conditionally load alternate versions of a resource based on media conditions. Here we used to media attribute of anchor tag to load a printable version and mobile friendly version of same page.
<!DOCTYPE html> <html lang="en"> <head> <title> Media Attribute with Anchor Tag </title> </head> <body> <h1>Hello, World!</h1> <p> This is an example of using the media attribute with an anchor tag. </p> <a href="/html/index.htm" media="print"> Printable Version </a> <br> <a href="/html/index.htm" media="screen and (max-width: 599px)"> Mobile Version </a> </body> </html>
Media attribute with Cliakable Area
Here we used media attribute with the area tag within an image map to provide alternate resources for different media types or conditions.
<!DOCTYPE html> <html lang="en"> <head> <title> Media Attribute with Area Tag </title> </head> <body> <h1>Image Map Example</h1> <p> Hover over the regions of the image to see different links based on media conditions. </p> <img src="/html/images/media-attribute.jpg" usemap="#example-map" alt="Example Image Map"> <map name="example-map"> <!-- Link for general screens --> <area shape="rect" coords="0,0,182,272" href="/html/index.htm" alt="Default Link"> <!-- Link for print media --> <area shape="rect" coords="180,0,373,141" href="/html/index.htm" alt="Print Link" media="print"> <!-- Link for small screens --> <area shape="rect" coords="180,145,372,270" href="/html/index.htm" alt="Mobile Link" media="screen and (max-width: 599px)"> </map> </body> </html>
Media attribute with Source Tag
Here in this example we used media attribute with source tag to change images based on screen width. It is not supported on source element anymore.
<!DOCTYPE html> <html lang="en"> <head> <title> Media Attribute with Picture Element </title> </head> <body> <h1>Responsive Image Example</h1> <p> The image changes based on the screen width. </p> <picture> <!-- Image for screens that are at least 800 pixels wide --> <source srcset="/html/images/html.jpg" media="(min-width: 600px)"> <!-- Image for screens that are less than 800 pixels wide --> <source srcset="/css/images/css.jpg" media="(max-width: 599px)"> <!-- Image for browsers that do not support the <picture> element --> <img src="/html/images/html.jpg" alt="Example Image"> </picture> </body> </html>
Supported Browsers
Attribute | |||||
---|---|---|---|---|---|
media | Yes | Yes | Yes | Yes | Yes |
To Continue Learning Please Login
Login with Google