HTML <hgroup> Tag
<hgroup> element allows you to attach a subtitle to a title without the subtitle being added to the document outline.
For example, you might have a level 1 heading, followed by a subheading in a level 2 heading. In this instance, the level 2 heading is different to the other level 2 headings in the document because it is an extension of the level 1 heading (i.e. it is a subheading of the heading).
Avoid this element if you need to make your documents W3C-compliant. See below for a "W3C-compliant" option.
<hgroup> tag is written as
</hgroup> with the grouped headings enclosed between the start and end tags.
Before we look at the examples, let's make sure we understand the problem first.
You could quite easily use two headings to represent a title and its subtitle without using the
<hgroup> tag. Like this:
However, this would result in both headings being added to the document outline. This means that, semantically, the two headings start their own section or subsection. Therefore, the subtitle will represent a new section. This may or may not be your intention.
So if your intention is for the subtitle/subheading/tagline etc to be added to the document outline, you don't need to use the
<hgroup> tag. You can simply use another heading element (
<h6>) to mark up the subtitle/subheading.
Because the headings are not contained within the
<hgroup> element, the 2nd heading will appear in the document outline.
If it is not your intention to add the subtitle to the document outline (start a new section), you have 3 options:
- use the
<hgroup>tag (not W3C-compliant)
- use a non-heading element for the subtitle (W3C-compliant)
- include the subtitle in the same heading element as the title (W3C-compliant)
<hgroup> Tag Usage
By enclosing the heading elements inside the
<hgroup> element, the
<h2> element is not added to the document outline.
Remember, the purpose of the
<hgroup> element is to mask the subtitle/subheading etc from the document outline (this prevents it from appearing in a table of contents).
HTML5 (i.e. the W3C version of HTML) doesn't support the
<hgroup> element. Only the WHATWG version (HTML Living Standard) supports this element. Therefore, if you need to make your documents W3C-compliant, you won't be able to use this element.
Also, W3C explicitly states that
must not be used to markup subheadings, subtitles, alternative titles and taglines unless intended to be the heading for a new section or subsection.
If you do not want the subtitles to be included the document outline (table of contents) and they are not intended to signify the start of a new section, check out the examples below.
You could also use CSS to style the subtitle if desired.
You could include the subtitle in the same heading as the title, separated by a colon.
You could take the previous example a step further by adding a
<br> tag and a
<span> tag to separate the subtitle from the title. This will enable you to separate the styles between the title and subtitle.
Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
<hgroup> element accepts the following attributes.
The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the
<hgroup> tag , as well as with all other HTML tags.
For a full explanation of these attributes, see HTML 5 global attributes.
Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.
Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.
For more detail, see HTML event handler content attributes.