Use Only One <main> on a Page
That’s it. That’s the meat of the post. The title covers it all. You don’t need to read any further. You are, of course, welcome to continue since I spent all this time writing it.
For those who aren’t familiar with
<main>, the element is intended to be a container for the primary content of your page. It’s already implemented in browsers, so you can use it today. The W3C HTML 5.1 draft specification describes it thusly:
The main content area of a document includes content that is unique to that document and excludes content that is repeated across a set of documents such as site navigation links, copyright information, site logos and banners and search forms (unless the document or applications main function is that of a search form).
Richard Clark explains the element in more human-readable detail over at HTML5 Doctor.
Multiple Content Areas
If you are in a scenario where you have a blog index, or an article index, you don’t need multiple
<main> elements. You can wrap the overall index in the
<main> and each abstract can be wrapped in
<body> <header></header> <main> <h1>Blogtastic!</h1> <article> <h2>Locally-Sourced Cupcake Flavor Profiles</h2> </article> <article> <h2>Artisanal Cocktail Mixers</h2> </article> <article> <h2>Beard Waxing Tips</h2> </article> </main> <footer></footer> </body>
Why It Matters
You may wonder is why it’s important to keep the
<main> to one instance per document.
As an element,
<main> exists to provide a direct mapping for the ARIA landmark role of the same name. The other landmark roles already have corresponding elements with matching native semantics, but the
main role did not.
If you’ve read my blog long enough, you may know that ARIA is a spec designed to add important accessibility information to a page for users, both with and without assistive technology (AT). In this case it allows a user to jump to the meat of the page with a single keystroke, much like a “skip navigation” link does on many sites.
Having more than one
<main> can be confusing to users of AT. While the element is relatively new, anecdotal examples show a potential trend of confusing users. Screen readers don’t announce the number of
<main> elements, so if there is more than one a user may not know if he/she is in the first, the second, or even that there are more.
This means landmark navigation can prove problematic if a user presses a key to go to the
<main>, assumes he/she is on the only one, and then pops back into the navigation area to move to another page. In this scenario, entire swaths of also-primary content would be missed by the user.
While the ARIA spec says you should not use more than one
role="main" on your page, the HTML specification takes this a step further and says you must not use more than one
Authors must not include more than one
mainelement in a document.
The HTML validator will also throw an error if there is more than one
<main> on a page.
Potential for Confusion
The WHATWG spec differs from the W3C spec in that not only does it allow multiple
<main> elements on a page, it encourages it.
For the reasons outlined above you should disregard this. There is an open bug report to have the WHATWG definition align with the W3C definition.
In the interests of full disclosure, I am an invited expert to the W3C HTML Working Group and I voted in favor of
<main> as originally proposed (with only one instance per page). I also wrote about my frustrations with the different approaches by WHATWG, W3C and browsers on implementing it back in 2012.
Update: April 20, 2016
It looks like Microsoft Edge is also expecting a single
<main> per page, based on the wording, though I doubt the browser will break if multiples are erroneously used.
For example, using the
<main>element, developers can provide a hint to assistive technology indicating where the primary content is. This makes it easier for their users to get to this content quickly from anywhere on the page. This experience will light up in Microsoft Edge and Narrator, and in other browsers, with assistive technologies supporting those roles.
Update: April 9, 2017
HTML 5.2 will likely tweak the use of
<main> a bit to allow more than one on a page:
What is important to note is that this change only allows one
<main> to be visible at a time. The others must be hidden.
Update: January 5, 2018
WHATWG issue 100, Consider aligning WHATWG main element definition with W3C definition, which has sat untouched since October 2015, got some activity this morning.
First, the accessibility tag was added.
Second, there appears to finally be some acknowledgment that, two years on, accessibility practitioners are not changing their informed opinion to align with WHATWG assertions:
FWIW, I created #3326 to take some steps in a direction that I hope is more agreeable. Hopefully we can iterate from there; it’s not meant to represent the endpoint here, just an improvement.
I think there is something to be said for being able to use
<section>similar to how
<footer>work there, but if AT is not going to play ball I’m not sure that’s really worth pursuing. This might be worth investigating a bit though, as it would satisfy end users and the needs of styling without having to resort to a
<div>with a class or some such.
I also agree that the dominant advice (unless we can get AT to change per above) should be to have a single
<main>per document. Otherwise the user experience is just not great, as pointed out by many in this thread. The argument that ease of styling also matters is valid, but ultimately end users trump web developers so they’ll have to use another
It does seem like there may be a case for multiple
<main>s when combined with
hidden, as W3C’s fork of HTML already acknowledges (though I’m less clear on if the exact enforcement there is correct).
Note the assertions that assistive technology refuses to
play ball with WHATWG’s definition, that styling needs are still a concern, that the
dominant, not correct, advice is to avoid multiple
<main>s, and the closing swipe at the W3C.
Even the related pull request (Align
<main> a bit with contemporary AT) frames an interesting reason for the update:
…there’s no reason to believe AT will change…
Update: January 15, 2018
Per this morning’s comment, a new pull request has been made (Restrict the main element to be used once per document #3354).
[…] Используйте только один <main> на странице […]