HTML <meta> Tag
The HTML <meta>
tag is used to define metadata about an HTML document. Metadata provides information about the document such as its character set, author, description, keywords, and viewport settings. This tag is crucial for improving search engine optimization (SEO), accessibility, and the responsiveness of web pages.
The <meta>
tag is a void element, meaning it does not have a closing tag, and it is placed within the <head>
section of an HTML document.
Basic Syntax of HTML <meta> Tag
The basic structure of the <meta>
tag is:
<meta name="name" content="value">
Here, name
specifies the type of metadata, and content
provides the corresponding value.
Common Attributes of HTML <meta> Tag
- name: Specifies the name of the metadata. Common values include
description
,keywords
, andauthor
. - content: Specifies the value of the metadata.
- charset: Specifies the character encoding for the document (e.g.,
UTF-8
). - http-equiv: Provides header-like information (e.g.,
content-type
,refresh
). - viewport: Configures how the page is displayed on mobile devices.
Setting the Character Encoding using meta tag
The charset
attribute specifies the character encoding for the document:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Character Encoding Example</title>
</head>
<body>
<p>This document uses UTF-8 encoding.</p>
</body>
</html>
Explanation: The charset="UTF-8"
attribute ensures the document supports a wide range of characters, including special symbols.
Setting the Page Description
The description
metadata provides a brief summary of the page’s content, which is often displayed in search engine results:
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="This is a tutorial on the HTML meta tag.">
<title>Meta Tag Example</title>
</head>
<body>
<p>Learn about the HTML meta tag and its attributes.</p>
</body>
</html>
Explanation: This meta tag provides a brief description, improving the page’s visibility in search engine results.
Configuring the Viewport for Mobile Devices
The viewport
metadata controls the page’s layout and scaling on mobile devices:
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Design Example</title>
</head>
<body>
<p>This page is optimized for mobile devices.</p>
</body>
</html>
Explanation: The viewport
meta tag ensures the page scales correctly on various screen sizes, improving responsiveness.
Setting Refresh or Redirect
The http-equiv="refresh"
attribute allows you to refresh or redirect a page automatically:
index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="5; url=https://www.example.com">
<title>Redirect Example</title>
</head>
<body>
<p>You will be redirected in 5 seconds.</p>
</body>
</html>
Explanation: This meta tag refreshes the page or redirects to the specified URL after 5 seconds.
Practical Applications of the <meta> Tag
- SEO Optimization: Improve search engine visibility by adding
description
andkeywords
metadata. - Responsive Design: Ensure proper scaling and layout on mobile devices with the
viewport
tag. - Character Encoding: Set the character encoding for multilingual content.
- Automatic Redirection: Redirect users to another URL or refresh the page.
- Custom Headers: Provide custom header-like information using
http-equiv
.