Open In App

HTML media attribute

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The HTML media attribute is employed to specify the media or device the coupled document is optimized for. This attribute specifies that the target URL is designed for special devices like iPhones, speech, or print media. 

syntax

<element media="value">

Supported Tags

ElementDescription
<a>Represents a hyperlink, linking to another resource.
<area>Defines a clickable area inside an image map.
<link>Specifies the relationship between a document and an external resource.
<source>Specifies multiple media resources for media elements like <audio> and <video>.
<style>Contains CSS rules to style HTML elements.

HTML media attribute Examples

Example1: In this example we demonstrates the use of the media attribute, which is incorrect for anchor elements. Instead, it uses the target attribute to specify whether links open in the same or a different tab. 

index.html
<!DOCTYPE html>
<html>
<head>
    <title>HTML media Attribute</title>
</head>
<body>
    <h2>HTML media Attribute</h2>
    <a href="https://ide.geeksforgeeks.org/community" target="_self">
        Click to open in the same tab
    </a>
    <br />
    <a href="https://www.geeksforgeeks.org/community" target="_blank">
        Click to open in a different tab
    </a>
</body>
</html>

Note: The HTML iframe syntax is correct, the https://geeksforgeeks.org website does not allow itself to be embedded in iframes on other domains. This is due to protective headers (X-Frame-Options, CSP) that prevent clickjacking and preserve content security. As a result, the iframe appears broken, even though the code itself is valid. You can use some other website link to check the output.

Example2: In this example the media attribute specifies conditions (such as screen width) under which different image sources defined in the <source> element are applied for responsive design. 

html
<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" 
          content="width=device-width, 
                     initial-scale=1.0" />
</head>

<body>
    <picture>
        <source media="(min-width: 900px)"
            srcset=
"https://media.geeksforgeeks.org/wp-content/uploads/20190328185307/gfg28.png" />
        <source media="(min-width: 600px)"
            srcset=
"https://media.geeksforgeeks.org/wp-content/uploads/20190809013546/gfg_350X350.png" />
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190521140445/gfglogo2.png" 
             style="width: auto" />
    </picture>
</body>

</html>

Output: Change the browser size. 

HTML media attribute

Supported Browsers: The browsers supported by HTML media attribute are listed below: 


Next Article

Similar Reads