DEV Community

Mbaziira
Mbaziira

Posted on • Originally published at mbaziira.hashnode.dev

What Are HTML Meta Tags And What Is Their Importance?

When starting a simple web development project, you usually prefer plain HTML, CSS, and JavaScript rather than a front-end framework such as React or Vue. The most familiar step that follows is creating an HTML file and generating a boilerplate.

The code in the boilerplate only includes the essential tags you need for an HTML document. And among these things are meta tags inside the head section of the document. Now, it's usual that you pay little or no attention to these tags. Many developers do not know what they are or their importance.

In this article, you will learn what HTML Meta tags are, their importance to a web page, and how to use them in your HTML Markup. You will also learn which meta tags to prioritize in your Markup and why.

Prerequisites

For you to follow along in this tutorial, you only need the knowledge of HTML Basics.

What are HTML Meta Tags?

Meta tags are HTML elements that provide metadata about your HTML document. Metadata is data that describes other data. So, to put it plainly, HTML meta tags provide information about your HTML document or web page.

The information that meta tags provide is mainly for browsers such as Chrome and Safari and Search engines such as Google and Bing. Browsers and Search engines use this information to understand the content on your web page.

Meta tag information is not visible to website visitors. To include meta tags to your HTML document, you need to place them between the opening and closing head tags. Meta tags are void elements by nature.

A void element is an element in HTML that cannot have any child nodes (i.e., nested elements or text nodes). Void elements only have a start tag; end tags must not be specified for void elements. (MDN)

Below is an HTML boilerplate generated using Emmet with the head and body tags of the document. We place the meta tags inside the <head> tag.

HTML Boiler plate

HTML meta tags convey information by use of attributes. This is where you put the information you want them to bear.

Examples of HTML Meta Tags

Below are some of the meta tags in HTML.

  • Viewport meta tag
  • Charset meta tag
  • Author meta tag
  • Description meta tag

We will look at each of the above meta tags in detail in the coming chapters.

HTML Meta-related Elements

There are HTML elements that represent metadata but are not meta tags. These elements include:

  • Title tag - <title></title>
  • Style tag - <style></style>
  • Script tag - <script></script>
  • Link tag - <link>
  • Base tag - <base>

Meta tags carry metadata that cannot be represented by the above HTML Meta-related elements.

Attributes Accepted By Meta Tags

As mentioned earlier, meta tags use attributes to convey information and can take in any global one. However, they are most associated with the following four:

name
The name attribute provides information that relates or applies to the entire HTML document. You use it with the content attribute in terms of name-value pairs.

The name attribute gives the metadata name, while the content attribute gives its value.

content
The content attribute provides the value for the metadata of the name or http-equiv attributes.

http-equiv
The full name for this attribute is http-equivalent.

The values it takes include content-security-policy, content-type, default-style, and refresh.

This is because all the allowed values are names of particular HTTP headers. (MDN)

charset
The charset sets the character encoding of the HTML document. It usually takes in the value utf-8 because it is currently the only valid character encoding for HTML5 documents.

Advantages Of HTML Meta Tags

HTML meta tags have tons of benefits for your web page. Let's take a look at some of them.

  • Meta tags can improve the Search Engine Optimization (SEO) ranking of your website by providing search engines with more information about your website's content, layout, and target.

  • Open Graph meta tags can improve social sharing by supplying valuable descriptive media information about the web page's content.

  • You can use meta tags to specify how different browsers should render your web pages, which ensures that your content doesn't break on various devices.

  • You can use meta tags to protect your web applications from common security vulnerabilities by specifying security-related metadata like content security policies.

Common HTML Meta Tags And Their Uses

We'll now look at each meta tag individually in more details and its use. Let's dive in!

Charset Meta Tag

The charset meta tag in HTML defines the character encoding of a web page.

A character set is an encoding to let computers know how to recognize characters such as letters, numbers, punctuation marks, and whitespace. - MDN

A character encoding is a system that maps bytes to text. Character encodings came to be because computers understand only binary data, 1s and 0s.

The charset meta tag tells browsers which character encoding to use for a web page, thus enabling text to be displayed correctly even on different devices.

If a web page renders with the wrong character encoding, it might produce broken characters that we refer to as Mojibake.

Mojibake

Format of a charset meta tag:

<meta charset="character_set_name">
Enter fullscreen mode Exit fullscreen mode

Example

<head>
   <meta charset="UTF-8"/>
</head>
Enter fullscreen mode Exit fullscreen mode

UTF-8 character encoding is used widely for its universal language support.

Author Meta Tag

The author meta tag provides information on the author of a web page, usually the name or social media profile. This meta tag is optional.

Format

<meta name="author" content="information on author"/>
Enter fullscreen mode Exit fullscreen mode

Example

<head>
   <meta name="author" content="Mbaziira Ronald"/>
</head>
Enter fullscreen mode Exit fullscreen mode

Http-Equiv Meta Tag

The http-equiv meta tag specifies an HTTP-Header for the HTML document. The HTTP-Header provides information to the browser on how to handle the HTML document, for example, the cache settings of the document.

Format

<meta http-equiv="HTTP-Header-name" content="HTTP-Header-value"/>

Enter fullscreen mode Exit fullscreen mode

Example

<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
Enter fullscreen mode Exit fullscreen mode

Below are some of the header names (on the left) and some of their values (on the right) this meta tag takes in:

Content-type: application/json and image/jpeg.

X-UA-Compatible: IE=edge

Refresh: 5 (Any positive figure)

Cache-Control: no-cache

Let's take a look at a few details on these header names

X-UA-Compatible header
You will usually see the http-equiv meta tag in the below example among the meta tags when you generate an HTML boilerplate.

<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
Enter fullscreen mode Exit fullscreen mode

This particular header name X-UA-Compatible and its value IE=edge specify how the Internet Explorer browser should handle the HTML document. Microsoft laid the browser to rest in 2022.

The header tells internet explorer which version of the browser to use to open the HTML document. The tag tells the browser to use Internet Explorer's latest version to render the HTML document, regardless of the one a user has on his machine.

Around the time of writing this article, Microsoft had laid Internet Explorer to rest, the browser for which this meta tag aims.

Refresh header
One of the few tricks beginners usually like to play is with the refresh header which specifies after how many seconds the HTML web page should refresh.

<meta http-equiv="refresh" content="5">
Enter fullscreen mode Exit fullscreen mode

Below is how it would like in the HTML file

Refresh header

It can also redirect to a specific URL after reloading.

<meta http-equiv="refresh" content="6;url=https://twitter.com">
Enter fullscreen mode Exit fullscreen mode

Viewport Meta Tag

The viewport meta tag is one of the most familiar meta tags. It sets the height, width, and zoom levels of the web page's viewport on mobile devices.

A viewport is the part of the document you're viewing which is currently visible in its window (or the screen, if the document is being viewed in full screen mode). - MDN

Format

<meta name="viewport" content="width=device-width, initial-scale=1" />
Enter fullscreen mode Exit fullscreen mode

The viewport meta tag is handy for optimizing web pages by specifying height, width, and zoom properties in its content attribute, as most web pages are not mobile screen optimal.

Among the properties it takes in, let's look at the following in more detail.

Width
The width property controls the width of the viewport. Typically, developers assign the value of device-width equal to 100vw (viewport width). It ignores negative values.

height
The height property controls the height of the viewport. It has the value of device-height which is 100% of the viewport height. It also ignores negative values.

initial-scale
This property controls the zoom level of the viewport when it is first loaded. It has a default value of 1, a minimum value is 0.1, and a maximum of 10.

Description Meta Tag

This meta tag provides a summary of the content on the web page. The summary is usually 160 characters or less. Search engines use this summary as a snippet that shows up in the search results together with the title of the web page.

Format

<meta name="description" content="Write description of what your web page is about here">
Enter fullscreen mode Exit fullscreen mode

The image below shows an example of a description on one my articles Google shows to the viewer during search.
Meta tag description example

You can check out this google article on how to create unique descriptions for your web pages.

Open Graph Meta tags

Open Graph meta tags (also known as OG meta tags) control the appearance of a web page when you share the URL on social media platforms like Twitter and LinkedIn.

Let's take a look at the examples in the images below. The previews are of two of my Hashnode articles, What Exactly Are Sentence Fragments and How To Create Responsive and Customizable UI Components Using Tailwind, shared on Twitter. The noticeable features of the previews are the articles' images, links, titles, and descriptions.

Sentence fragments article image

Tailwind article image

OG meta tags take in the property attribute, which specifies the open graph property related to the content. It is one of the indicators that a meta tag is an Open Graph meta tag. Some properties have extra optional properties that you can use alongside them.

For the above previews to be possible, the OG meta tags use the Open Graph Protocol. The Open Graph Protocol has four expected properties for every web page if you are to use it.

These are:

  • og:title
  • og:type
  • og:image
  • og:url

Open Graph meta tags

The above OG meta tags would resemble something like in the image below when in the HTML Markup.

OG meta tags

The Open Graph protocol enables any web page to become a rich object in a social graph. - Open Graph Protocol

OG Title

The OG Title meta tag specifies the title for the web page to display in the web page preview on social media platforms.

Format

<meta property="og:title" content="Web page title">
Enter fullscreen mode Exit fullscreen mode

Example

<meta property="og:title" content="How To Create Responsive And Customizable UI Components Using Tailwind">
Enter fullscreen mode Exit fullscreen mode

The image below shows the OG Title of an article shared on Twitter.

OG title

OG Type

The OG Type Meta tag defines the type of content the web page contains. It can take in a couple of types but these are the most common ones:

  • website
  • article
  • music
  • video
  • book

It helps social media platforms understand the type of content you are sharing.

Format

<meta property="og:type" content="type_value" />
Enter fullscreen mode Exit fullscreen mode

Example

<meta property="og:type" content="article" />
Enter fullscreen mode Exit fullscreen mode

OG Description

The OG Description meta tag provides a summary or brief description of what the web page is about when you share it on social media platforms.

OG Description

Format

<meta property="og:type" content="write og description here">
Enter fullscreen mode Exit fullscreen mode

Example

<meta property="og:type" content="Learn how to create customizable, responsive, and captivating UI components using Tailwind CSS">
Enter fullscreen mode Exit fullscreen mode

OG Image

The OG image meta tag specifies the image you want to display when you share your web page on social media platforms.

Format

<meta property="og:image" content="image_url">
Enter fullscreen mode Exit fullscreen mode

Example

<meta property="og:image" content="https://example.com/ogimage.jpg">
Enter fullscreen mode Exit fullscreen mode

The OG image meta tag can have more properties to which you can attach more metadata. These include:

  • og:image:width
  • og:image:height
  • og:image:alt

Each of these properties is put in its meta tag and not the main one with the property of og:image.

It would be like this:

<meta property="og:image" content="https://example.com/ogimage.jpg"><meta property="og:image:width" content="400" />
<meta property="og:image:height" content="300" />
<meta property="og:image:alt" content="A cover with the title of the article" />
Enter fullscreen mode Exit fullscreen mode

The og:image:width and og:image:height are in pixels.

OG Image

OG Url

The OG Url meta tag specifies the canonical URL for the web page.

A canonical URL is the URL of the best representative page from a group of duplicate pages. - Source

Why a canonical URL, I hear you ask? Well, it's because pages can have duplicates, and among these, you would like a particular web page URL to be indexed by search engines rather than one of the others such that its ranking is not affected. For articles, it is usually the original post.

Format

<meta property="og:url" content="canonical URL" />
Enter fullscreen mode Exit fullscreen mode

Example

<meta property="og:url" content="https://mbaziira.hashnode.dev/how-to-create-customizable-and-responsive-ui-components-using-tailwind"/>
Enter fullscreen mode Exit fullscreen mode

OG Site name

The OG Site name meta tag indicates the name of the website, brand, or company. This OG site name meta tag is optional.

Format

<meta property="og:site_name" content="Write site name, company or brand name here">
Enter fullscreen mode Exit fullscreen mode

Example

<meta property="og:site_name" content="Tweetos">
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are more advantages and examples of HTML meta tags than the ones we have covered here. I implore you to take the knowledge you have gained to the next level by practicing using meta tags in your Markup.

You will understand the importance of meta tags and how they work much better when you start using them in your projects.

For any questions or anything you wish to comment on, kindly leave it in the comments below. Thanks for taking a read. See you soon!

Top comments (0)