Robert Bue
Robert Bue

Reputation: 1914

Correct markup for headers

How do I semanticly markup the headers in the article as presented in the picture below? I often run into this problem; where I have a header that is visually first, but is a subheading, intro header or less important then the main header. I could put it after the main header and move it above with CSS, but that isn't always a ideal solution.

Another question: is there any special way to markup lead paragraphs?

headermarkup Not allowed?

<article>
<h2>New prodcut</h2>
<h1>Launching our new x-series</h1>
<p class="lead">Lorem ipsum dolor sit amet...</p>
<p>Integer varius, turpis sit amet accumsan...</p>
...
</article>

Upvotes: 4

Views: 1685

Answers (3)

CodeManX
CodeManX

Reputation: 11865

The HTML Standard as of 2023 recommends the use of the <hgroup> element for subheading, alternative titles, and taglines:

<article>
  <hgroup>
    <p>New product</p>
    <h1>Launching our new x-series</h1>
    <p>Lorem ipsum dolor sit amet...</p>
  </hgroup>
  <p>Integer varius, turpis sit amet accumsan...</p>
  ...
</article>

Permitted content:

  1. Zero or more <p> elements,
  2. followed by one <h1> , <h2>, <h3>, <h4>, <h5>, or <h6> element,
  3. followed by zero or more <p> elements.

See: 4.3.7 The hgroup element

While not explicitly mentioned, I would consider the lead paragraph to be content related to a heading and would thus place it in the <hgroup> as well.

Setting classes like <p class="lead"> is not required thanks to the <hgroup> element, which lets you target the subheading and the lead paragraph in CSS easily:

hgroup > p:first-child { /* or  hgroup p:has(+ h1)  for the <p> before <h1> */
  font-size: 16pt;
}

hgroup h1 + p {
  font-size: 14pt;
  font-style: italic;
}

Upvotes: 0

unor
unor

Reputation: 96567

Don’t use a heading element (h1-h6) for the subheading. This was once possible with the hgroup element, but it got removed.

Now the HTML5 spec has a section about Subheadings, subtitles, alternative titles and taglines. Use a p element for the subheading, and group both, the heading and the subheading in a header element:

<article>
  <header>
    <p>New product</p>
    <h1>Launching our new X-series</h1>
  </header>
</article>

If you think the lead paragraph should also be part of the header (instead of the main content), you can put it in the header, too.

Upvotes: 3

Adam Zuckerman
Adam Zuckerman

Reputation: 1641

While what you are doing is syntactically allowed, I think it will case confusion for low vision users.

How about doing something like this:

<div class="newproduct>New Product</div>
<div class="articletitle">Launching our new x-series</div>
Lorem ipsum....

If you want to use some newer HTML, I would recommend you look into article and section tags.

Upvotes: -1

Related Questions