HTML <hgroup>
Tag
The <hgroup>
tag in HTML is used to group a set of headings (<h1>
to <h6>
) when the main heading has subheadings or multiple parts. It is a semantic element that clarifies the relationship between the primary heading and its related subheadings, improving document structure and accessibility. Although it is not fully supported by all browsers, understanding its use can lead to better structured web content.
Syntax
<hgroup>
<h1>Main Heading</h1>
<h2>Subheading 1</h2>
<h3>Subheading 2</h3>
<!-- other headings -->
</hgroup>
Attributes
The <hgroup>
tag does not accept any specific attributes in HTML5. It's a semantic container that works purely based on the context it provides to its child heading elements.
Attribute | Value | Description |
---|---|---|
(None) | (None) | <hgroup> tag doesn't accept any specific attributes. |
Example
<hgroup>
<h1>My Blog Post Title</h1>
<h2>Subtitle explaining the post's topic</h2>
</hgroup>
More Examples
Basic Usage
The most basic use of <hgroup>
involves grouping a main title with a subtitle. This establishes a clear hierarchical relationship between the two.
<hgroup>
<h1>The Art of Web Development</h1>
<h2>A Comprehensive Guide</h2>
</hgroup>
Multiple Subheadings
When a main heading has multiple levels of subheadings, you can group all of them within an <hgroup>
for better semantic understanding.
<hgroup>
<h1>Advanced CSS Techniques</h1>
<h2>Understanding Selectors</h2>
<h3>Specificity and Cascade</h3>
</hgroup>
Complex Scenarios
In more complex layouts, like articles with multiple sections each with their own headings, <hgroup>
can be beneficial to encapsulate the title and subtitle of that section.
<article>
<hgroup>
<h1>Section 1: Introduction</h1>
<h2>Overview of the topic</h2>
</hgroup>
<p>...</p>
<hgroup>
<h1>Section 2: Deep Dive</h1>
<h2>Detailed analysis of the topic</h2>
</hgroup>
<p>...</p>
</article>
Combining with other Semantics
The <hgroup>
can work along with other semantic tags like <header>
for example, to provide more semantic meaning to a specific section of your document.
<header>
<hgroup>
<h1>Blog Title</h1>
<h2>Subtitle</h2>
</hgroup>
<nav>
<!-- Nav links -->
</nav>
</header>
Browser Support
The <hgroup>
tag is not fully supported by all browsers. While most modern browsers render it correctly, older versions may not.
| Browser | Support |
|—————–|———–|
| Chrome | Yes |
| Edge | Yes |
| Firefox | Yes |
| Safari | Yes |
| Opera | Yes |
| Internet Explorer| No (deprecated) |
Notes and Tips
- Accessibility: Using
<hgroup>
can improve the accessibility of your web pages by clearly marking related headings for assistive technologies. - SEO: Although search engines primarily look for
<h1>
to<h6>
, using<hgroup>
can help establish a clearer hierarchical structure in your content, which can indirectly benefit SEO. - Alternatives: While
<hgroup>
can be useful for explicit heading grouping, the way you use<h1>
to<h6>
in conjunction with proper sectioning (<article>
,<section>
,<aside>
) can often achieve similar or better results with more widespread browser support. - Avoid excessive nesting: While you can nest
h
tags within an<hgroup>
, avoid over-complicating your structure. Ensure the headings you group together are semantically related. - Use with caution: Note that the
<hgroup>
tag is not as widely adopted or crucial as other HTML semantic tags. For many cases, you can often achieve the desired effect using proper heading hierarchy and semantic sectioning elements (<header>
,<footer>
,<article>
,<aside>
,<nav>
,<section>
) - Consider the hierarchy: If you're using
<hgroup>
, think carefully about the hierarchy and if the subheadings are truly part of the main heading and shouldn't exist independently. If they should be independent, sectioning may be a better approach. - Browser Testing: Always test your code in multiple browsers to ensure consistent rendering and accessibility.