HTML5: Create a link with a media attribute?
Solution :
HTML Code :
<!-- Declare the document type as HTML5 -->
<!DOCTYPE html>
<!-- Start the HTML document -->
<html>
<!-- Start the head section containing metadata about the document -->
<head>
<!-- Set the character encoding to UTF-8 -->
<meta charset="utf-8">
<!-- Set the title of the document to "Create a link with a media attribute" -->
<title>Create a link with a media attribute</title>
<!-- Close the head section -->
</head>
<!-- Start the body section containing the content of the document -->
<body>
<!-- Create a hyperlink with a specified URL and media attribute -->
<a href="https://www.w3resource.com" media="print, handheld">Media Attribute</a>
<!-- Close the body section -->
</body>
<!-- Close the HTML document -->
</html>
Explanation:
The above HTML code defines a basic HTML5 document with metadata in the head section and a body containing a hyperlink. The special addition here is the use of the 'media' attribute within the hyperlink. This specifies that the linked resource is designed for "print" and "handheld" media types.
Here's a brief explanation of each part of HTML code:
- <!DOCTYPE html>:
- This declaration specifies that the document type is HTML5, indicating the HTML version being used.
- <html>:
- This tag marks the beginning of an HTML document.
- <head>:
- The 'head' section contains metadata about the HTML document, such as character encoding and title.
- <meta charset="utf-8">:
- This meta tag sets the character encoding to UTF-8, a widely used character encoding standard.
- <title>How to create a hyperlink</title>:
- The title tag sets the HTML document title, which is often displayed in the browser's title bar or tab.
- </head>:
- Closes the HTML document's 'head' section.
- <body>:
- The body section contains the actual HTML document content.
- <p><a href="https://www.w3resource.com/">w3resource.com</a></p>:
- Create a paragraph (<p>) containing a hyperlink (<a>) with the text "w3resource.com" and a link (href) to "https://www.w3resource.com/".
- </body>:
- Closes the HTML document's body section.
- </html>:
- Mark the end of the HTML document.
Live Demo:
See the Pen a_media-answer by w3resource (@w3resource) on CodePen.
View the example in the browser
Supported browser
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics