Further examples on using the main element
Which is the more correct page layout? – was the question posed over on Stack Overflow with regards to the new HTML5 main element. Neither of them as it turned out, but it did make me think that further examples on how to use the main element might be useful.
I wrote about the main element and included two examples of how it might be used. Of course two examples won’t cover everybody’s situation so a few more might help. Bear in mind that these are my interpretation, after reading the specification, on how the main element can be used so are naturally open to interpretation.
Context and content
Before I go into some examples, remember that it’s all about context and content. Let’s briefly turn to the specification (again) to see what it says:
The main element represents the main content of the body of a document or application. The main content area consists of content that is directly related to or expands upon the central topic of a document or central functionality of an application.
Bear this in mind when you’re trying to work out if your header and/or footer should be inside the main element or not, and whether the aside that you’ve added should also be inside or outside.
The question to ask yourself in each case is: is the content of this element relevant to the main content of this document or not?
If it is – it goes inside the main element, otherwise, outside.
Let’s now look at some examples to help illustrate what I mean (some of these examples might overlap the two in my previous post but that doesn’t really matter).
Examples
<header>
<h1>The Border Terrier</h1>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="health.html">Health</a></li>
</ul>
</nav>
</header>
<main role="main">
<h2>Welcome!</h2>
<p>This site is all about the Border Terrier, the best breed of dog that there is!</p>
</main>
<footer>
<small>Copyright © <time datetime="2013">2013</time></small>
</footer>
In this short example, both the header and the footer are outside the main element as they are generic to the website and not specific to the content that is within the main element.
<header>
<h1>The Border Terrier</h1>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="health.html">Health</a></li>
</ul>
</nav>
</header>
<main role="main">
<section>
<header>
<h2>About</h2>
<nav>
<ul>
<li><a href="#basic">Basic</a></li>
<li><a href="#app">Appearance</a></li>
<li><a href="#temp">Temperament</a></li>
</ul>
</nav>
</header>
<section id="basic">
<h3>Basic Information</h3>
<p>The Border Terrier is a small, rough-coated breed of dog of the terrier group, originally bred as fox and vermin hunters</p>
<p>The border terrier was bred to have long enough legs to keep up with the horses and other foxhounds, which traveled with them, and small enough bodies to crawl in the burrows of foxes and chase them out so the hunters had a blank shot. The foxhounds that traveled with them were not small enough to do the Border terrier's job.</p>
</section>
<section id="app">
<h3>Appearance</h3>
<p>Identifiable by their otter-shaped heads, Border Terriers have a broad skull and short (although many be fairly long), strong muzzle with a scissors bite. The V-shaped ears are on the sides of the head and fall towards the cheeks. Common coat colors are grizzle-and-tan, blue-and-tan, red, or wheaten. Whiskers are few and short. The tail is naturally moderately short, thick at the base and tapering.</p>
</section>
<section id="temp">
<h3>Temperament</h3>
<p>Though sometimes stubborn and strong willed, border terriers are, on the whole very even tempered, and are friendly and rarely aggressive. They are very good with children, but may chase cats and any other small pets.</p>
</section>
<footer>
<a href="#basic">Basic</a> - <a href="#app">Appearance</a> - <a href="#temp">Temperament</a>
</footer>
</section>
</main>
<footer>
<small>Copyright © <time datetime="2013">2013</time></small>
</footer>
Here, the same generic header and footer elements remain outside main, but there is an additional header element within the main element as its content is relevant to the content within the main element only as it contains a relevant heading and in-page navigation. The in-page navigation is repeated within a footer which is again within the main element.
<header>
<h1>The Border Terrier</h1>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="health.html">Health</a></li>
</ul>
</nav>
</header>
<main role="main">
<section>
<header>
<h2>About</h2>
<nav>
<ul>
<li><a href="#basic">Basic</a></li>
<li><a href="#app">Appearance</a></li>
<li><a href="#temp">Temperament</a></li>
</ul>
</nav>
</header>
<section id="basic">
<h3>Basic Information</h3>
<p>The Border Terrier is a small, rough-coated breed of dog of the terrier group, originally bred as fox and vermin hunters</p>
<p>The border terrier was bred to have long enough legs to keep up with the horses and other foxhounds, which traveled with them, and small enough bodies to crawl in the burrows of foxes and chase them out so the hunters had a blank shot. The foxhounds that traveled with them were not small enough to do the Border terrier's job.</p>
</section>
<section id="app">
<h3>Appearance</h3>
<p>Identifiable by their otter-shaped heads, Border Terriers have a broad skull and short (although many be fairly long), strong muzzle with a scissors bite. The V-shaped ears are on the sides of the head and fall towards the cheeks. Common coat colors are grizzle-and-tan, blue-and-tan, red, or wheaten. Whiskers are few and short. The tail is naturally moderately short, thick at the base and tapering.</p>
</section>
<section id="temp">
<h3>Temperament</h3>
<p>Though sometimes stubborn and strong willed, border terriers are, on the whole very even tempered, and are friendly and rarely aggressive. They are very good with children, but may chase cats and any other small pets.</p>
</section>
<aside>
<h3>History</h3>
<p>The Border Terrier originates in, and takes its name from the Scottish borders. Their original purpose was to bolt foxes which had gone to ground. They were also used to kill rodents, but they have been used to hunt otters and badgers too.</p>
</aside>
<footer>
<a href="#basic">Basic</a> - <a href="#app">Appearance</a> - <a href="#temp">Temperament</a>
</footer>
</section>
</main>
<footer>
<small>Copyright © <time datetime="2013">2013</time></small>
</footer>
This example is largely the same as the second one except that we have introduced an aside into the fray. The content of the aside is considered to be relevant to the content within the main element, which is all about the Border Terrier, so the aside is placed within the main element.
<header>
<h1>The Border Terrier</h1>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="health.html">Health</a></li>
</ul>
</nav>
</header>
<main role="main">
<h2>Welcome!</h2>
<p>This site is all about the Border Terrier, the best breed of dog that there is!</p>
</main>
<aside class="advert">
<h2>Border Farm Breeders</h2>
<p>We are a certified breeder of Border Terriers, contact us at...</p>
</aside>
<aside class="advert">
<h2>Grumpy's Pet Shop</h2>
<p>Get all your pet's needs at our shop!</p>
</aside>
<footer>
<small>Copyright © <time datetime="2013">2013</time></small>
</footer>
In this final example, two aside elements containg adverts have been placed outside the main element as their content is not specific to the content within main. These asides could be on any page, as they are as generic as the header and footer shown in this example.
I hope these further examples have helped clear a few things up. If not, feel free to comment below!
16 Responses
These examples are great! But one question, when is an article element to be used? Would they go inside the main element, or are they separate?
Hi Mike,
Again it depends on context and content, but it’s likely that if you’re using the article element with your page’s content then it’s likely to be contained within the main element. But again ask yourself the question mentioned above: “is the content of this element relevant to the main content of this document or not?” and act accordingly.
Your <nav>s — no me gusta : (
Why not Stomme?
I don’t see how Home, About, Health is a list but Basic, Appearance and Temperament and just a sequence. Why does one have additional structure while the other doesn’t?
Seems like the architecture of the links/information is
<li>< a href=”index.html”>Home</a></li>
<li><a href=”about.html”>About</a>
<ul>
<li><a href”#basic”>Basic</a></li>
<li><a href=”#app”>Appearance</a> </li>
<li><a href=”#temp”>Temperament</a> </li>
</ul>
</li>
<li><a href=”health.html”>Health</a></li>
I *do* like the idea that, if there are going to be two navs on a page (since normally we’d only want one if it means “primary”), that if one is within <main> and the other outside, that they can mean “primary content navigation” vs “primary page/site navigation”. Two different things, two different contexts.
No idea if my code turned out : )
s/Temperament and/Temperament is
Yeah fair enough. I only didn’t make the in-page navigation a list (which I suppose it is) to show that the content of a <nav> element doesn’t always have to be a list! (And to make the examples a bit shorter).
I think a good example of a non-list nav could be… replacing a div that encompasses some headings, several lists (say, a site selling products and this is the product navigation) and a dedicated product-number search form
Yes but it wasn’t really the focus of this post. I’ll change it to a list so it doesn’t confuse.
Well explained as usual, Ian. I’m confused by your use of
aside,footerand possiblyheaderinsidemainthough.The
mainelement isn’t sectioning content so a nested footer doesn’t make sense in light of the spec saying ,”The footer element represents a footer for its nearest ancestor sectioning content or sectioning root element.”Therefore both the
footerelements in your example represent footers forbody(the nearest ancestor section root).In the case of
aside, it is sectioning content therefore is a subsection of it’s nearest ancestor sectioning content or sectioning root which is thebody. It is not limited to themain.If you paste your example into the outliner Grumpy’s Pet Shop is a subsection of and therefore tangientally related to The Border Terrier. The
maindoes not delineate the content it is related to.Regarding
headerthe spec isn’t quite as explicit as forfooterbut mentions sections with the implication it’s for the page as a whole or subsections created by sectioning content. This HTML5 Doctor article on the header element is clearer about its role within sectioning content.Hi Ian and Derek,
short answer: looks like it needs article or section to fix the document outline.
short advice: when building a page add sectioning elements then wrap a main around the main content.
longer answer: the header/footer/aside/nav/main elements, and in at least one AT (JAWS) the section and article elements, are recognised as landmarks (in supporting browsers or with the addition of the role as a polyfill). This a navigation/semantic structure actually exposed to users that is not related to the document outline, unlike the document outline that is not supported in any browsers and only supported in a borked way by JAWS. So while in a simple example document people may say “hey the addition of main appears redundant”, but it is not as the semantics of main are different from all of the other elements listed, in particular section (a generic region) and article (article role) both of which can and are used sometimes overused in real world documents i.e. they are both more granular than the main semantic. Have a look at 252 HTML5 pages using the section element or 170 HTML5 pages that use the article element to get an idea of what I mean. And thanks to both of you this discussion is really helpful!
Thanks for the informative reply Steve. Based on that then, in all my examples above that have a <header> and <footer> within a <main>, I should actually have all that content wrapped in something like a <div>, <article> or <section>. This does make the <main> appear redundant as you say.
Hmm, I’d prefer to be able to assign an over all <header> and <footer> to the <main> without the need for another element.
Hi Ian,
It’s not redundant as section or article (not div as not a sectioning element) does not signify the main content area of the page, only main does that.
Ok thanks.
I have updated some of the examples to include a <section> where appropriate. Thanks for the feedback and a very useful discussion!
looking at the elements used in this page for example: | HTML: 1 | HEAD: 1 | META: 5 | TITLE: 1 | LINK: 14 | SCRIPT: 8 | STYLE: 2 | BODY: 1 | DIV: 20 | HEADER: 16 | A: 43 | NAV: 1 | OL: 1 | LI: 22 | MAIN: 1 | H1: 1 | TIME: 18 | P: 59 | ABBR: 1 | CODE: 49 | SPAN: 438 | H2: 2 | BLOCKQUOTE: 1 | BR: 143 | PRE: 4 | FOOTER: 2 | SECTION: 1 | H3: 2 | UL: 2 | IMG: 1 | SMALL: 2 | FORM: 2 | LABEL: 5 | INPUT: 9 | TEXTAREA: 1 | ASIDE: 1 |
the addition of the main element is a drop in the code ocean
Great explanation Ian. I’m a HTML5 novice, but not only do I know now where & when to use the main element, but also understand now how to use HTML5 elements with content. Very usefull info.. thank you!