Learn HTML

HTML: h2 tag

HTML: h2 tag

In this series of learning HTML, we will teach you how to use the HTML h2 tag with proper syntax and lots of examples. Let’s start with the HTML h2 tag.

 

 

Description of HTML h2 tag

The HTML h2 tag is used to define a sub-heading on a web page. It stands for “Heading 2” and is typically used to break up the content of a page into sections with headings that are less important than the main heading (h1) but more important than lower-level headings such as h3, h4, and so on.

Here’s an example of how the h2 tag might be used in HTML code:

<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my Web Page</h1>
<h2>About Me</h2>
<p>This is a paragraph about me.</p>
<h2>My Hobbies</h2>
<p>These are my hobbies:</p>
<ul>
<li>Reading</li>
<li>Playing guitar</li>
<li>Hiking</li>
</ul>
</body>
</html>

In this example, the h2 tag is used to define two sub-headings on the page: “About Me” and “My Hobbies”. These headings are followed by paragraphs of content that provide more information about the sections they belong to. When this code is rendered in a web browser, the text “About Me” and “My Hobbies” will be displayed in a slightly smaller font than the main heading, but still larger than normal body text, and will help break up the page into smaller sections.


Syntax of HTML h2 tag

The proper syntax for the HTML h2 tag is as shown below:

<html>
<head>
<title>
H2 Tag Example
</title>
</head>
<body>
<section>

<p>Paragraph </p>
<h2>H2 TAG Sub-subsection Title</h2>

</section>

</body>
</html>

h2 paragraph html code

 

Sample Output

h2 paragraph result


Attributes

The HTML h2 tag supports a few attributes that can be used to further define and modify the appearance and behavior of the heading. Here are some of the most commonly used attributes:

  1. class: The class attribute is used to specify one or more CSS classes that will be applied to the heading. This allows you to style the heading using CSS.

Example:

<h2 class="subheading">Subheading Text</h2>
  1. id: The id attribute is used to specify a unique identifier for the heading. This can be used to link to the heading from other parts of the same page or from other pages.

Example:

<h2 id="subheading">Subheading Text</h2>
  1. style: The style attribute is used to apply inline CSS styles to the heading.

Example:

<h2 style="color: blue;">Subheading Text</h2>
  1. title: The title attribute is used to provide additional information about the heading when the user hovers over it. This information is displayed in a tooltip.

Example:

<h2 title="This is a subheading">Subheading Text</h2>

These are some of the most commonly used attributes of the HTML h2 tag. However, it’s important to note that the h2 tag has fewer attributes than the higher-level heading tags, as it is intended for less prominent headings.

Attribute Description HTML Compatibility
align Used for the text Alignment. It is deprecated in HTML 4.01 and Obsolete in HTML5.

Note

Here are some important notes on the HTML h2 tag:

  1. The h2 tag is used to define a major heading on a web page, and it is smaller than h1 but larger than h3.
  2. The h2 tag should be used to structure content into meaningful sections, and to indicate a new topic or theme.
  3. Headings should be used to structure content and make it more accessible to users. As such, the h2 tag should be used after the h1 tag.
  4. When using headings, it is important to use them consistently throughout the page, and to avoid skipping levels (e.g. going from h1 to h3 without using h2).
  5. Headings should be descriptive and meaningful, and should accurately reflect the content of the section they are defining. Using vague or misleading headings can confuse users and make the content less accessible.
  6. It is important to use headings for their semantic meaning, rather than just for styling purposes. Using larger font sizes or bold/italic text can achieve similar visual effects without affecting the structure and accessibility of the page.
  7. It is also important to note that headings should not be used solely for decorative or aesthetic purposes, and they should always be relevant to the content that follows.

Browser Compatibility

The HTML h2 tag has essential support with the following browsers:

  1. Chrome
  2. Internet Explorer (IE)
  3. Opera
  4. Safari (WebKit)
  5. Firefox (Gecko)
  6. Android
  7. Firefox Mobile (Gecko)
  8. Edge Mobile
  9. Opera Mobile
  10. Safari Mobile

Examples of HTML h2 tag

We will discuss the HTML h2 tag below, exploring examples of how to use the HTML h2 tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.

  • HTML5
  • HTML4
  • XHTML

We will discuss the HTML h2 tag below, with some examples of using the h2 tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Strict, XHTML 1.0 Transitional, and XHTML 1.1.

  1. HTML5
  2. HTML4
  3. XHTML

 

 

When you create a new web page in HTML5, your HTML h2 tag might look like this:

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>HTML5 Example by www.home4cloud.com</title>

</head>

<body>

<h1>Heading 1</h1>

<p>This is the content that would appear under Heading 1.</p>

<h2>Heading 2</h2>

<p>This is the content that would appear under Heading 2.</p>

</body>

</html>

In this above HTML5 Document example, we have created the HTML h2 tag with the text “Heading 2” that follows the HTML h1 tag.

 

HTML 4.01 Transitional Document

If you create a new HTML webpage in HTML 4.01, your HTML h2 tag might look like this, as shown below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>HTML5 Example by www.home4cloud.com</title>

</head>

<body>

<h1>Heading 1 for home4cloud</h1>

<p>This is the content that would appear under Heading of h1 html tag</p>

<h2>Heading 2 for home4cloud</h2>

<p>This is the content that would appear under Heading of h2 html tag</p>

</body>

</html>

In this above HTML 4.01 Transitional Document example, we have created the HTML h2 tag with the text “Heading 2” that follows the HTML h1 tag.

 

 

XHTML 1.0 Transitional Document

If you create a new HTML webpage in XHTML 1.0 Transitional, your HTML h2 tag might look like this, as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>XHMTL 1.0 Transitional Example by www.home4cloud.com</title>

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>HTML5 Example by www.home4cloud.com</title>

</head>

<body>

<h1>Heading 1 for home4cloud</h1>

<p>This is the content that would appear under Heading of h1 html tag</p>

<h2>Heading 2 for home4cloud</h2>

<p>This is the content that would appear under Heading of h6 html tag</p>

</body>

</html>

In this above XHTML 1.0 Transitional Document example, we have created the HTML h2 tag with the text “Heading 2” that follows the HTML h1 tag.

 

XHTML 1.0 Strict Document

If you create a new HTML webpage in XHTML 1.0 Strict, your HTML h2 tag might look like this, as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>XHTML 1.0 Strict Example by www.home4cloud.com</title>

<head>

<metahttp-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>HTML5 Example by www.home4cloud.com</title>

</head>

<body>

<h1>Heading 1 for home4cloud</h1>

<p>This is the content that would appear under Heading of h1 html tag</p>

<h2>Heading 2 for home4cloud</h2>

<p>This is the content that would appear under Heading of h2 html tag</p>

</body>

</html>

In this above XHTML 1.0 Strict Document example, we have created the HTML h2 tag with the text “Heading 2” that follows the HTML h1 tag.

 

 

XHTML 1.1 Document

If you create a new HTML webpage in XHTML 1.1, your HTML h6 tag might look like this, as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>XHTML 1.1 Example by www.home4cloud.com</title>

<head>

<metahttp-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>HTML5 Example by www.home4cloud.com</title>

</head>

<body>

<h1>Heading 1 for home4cloud</h1>

<p>This is the content that would appear under Heading of h1 html tag</p>

<h2>Heading 2 for home4cloud</h2>

<p>This is the content that would appear under Heading of h2 html tag</p>

</body>

</html>

In this above XHTML 1.1 Document example, we have created the HTML h2 tag with the text “Heading 2” that follows the HTML h1 tag.

About the author

Home4Cloud