The main element
Back in I wrote about the proposed HTML5 main element. This new element was recently added to the HTML5 specification.
Actually main has so far only been added to the W3C HTML5 specifcation, the WHATWG have yet to do so.
It has also been added to the next version of the HTML5 specification: HTML 5.1.
Background
This element is the brainchild of Steve Faulkner who has worked tirelessly in putting together an excellent extension specification which now forms the definition of the main element within the W3C HTML5 specification. Steve also produced a thorough use case rationale for the inclusion of the main element.
Definition
The W3C specification defines the main element as being used to represent the main content section of the body of a document or application.
It also goes on to define what is meant by a “main content section”, which consists of content that is directly related to or expands upon the central topic of a document or central functionality of an application.
Facts
- There should only be one
mainelement per web document. - The
mainelement is not sectioning content and therefore doesn’t affect the document outline. - It is recommended to use the WAI-ARIA
role="main"role on the main element until such time as user agents implement the required role mapping.
Example usage
As has already been mentioned, the purpose of the main element is to contain the main content or topic of a web document.
If, for example, the web document contains a blog entry, then the entire content of that blog entry should be wrapped within a main element.
<main role="main">
<h1>The Coypu</h1>
<section>
<h2>Introduction</h2>
<p>The ‘Coypu’ (or ‘Swamp Beaver’ as it is elsewhere known) is a rodent, related to the porcupine. It is an aquatic animal, making burrows in the banks of streams and feeding on plants in or near to the water.</p>
</section>
<section>
<h2>Location</h2>
<p>The coypu's original home is in South America but due to it's beautiful long soft coat, the coypu has been farmed in many parts of the world including Britain, where in particular, it had escaped and established itself
successfully in the countryside of Norfolk and North Suffolk to become a major pest by damaging riverbanks and crops.</p>
</section>
<section>
<h2>Appearance</h2>
<p>The coypu distinctly resembles an otter and the word ‘Nutria’ is Spanish for otter. An adult coypu may weigh up to 9kg and grow to 600mm long, it is a powerful swimmer with webbed rear feet. It can hold and manipulate things using it’s long-clawed front feet. It’s tail is long and scaly, but not in anyway flattened.</p>
</section>
</main>
Any navigation that allows jumps to areas within the blog entry’s content should also be contained within the main element.
<main role="main">
<header>
<h1>The Coypu</h1>
<nav>
<a href="#intro">Introduction</a>
<a href="#loc">Location</a>
<a href="#app">Appearance</a>
</nav>
</header>
<section id="intro">
<h2>Introduction</h2>
<p>The ‘Coypu’ (or ‘Swamp Beaver’ as it is elsewhere known) is a rodent, related to the porcupine. It is an aquatic animal, making burrows in the banks of streams and feeding on plants in or near to the water.</p>
</section>
<section id="loc">
<h2>Location</h2>
<p>The coypu's original home is in South America but due to it's beautiful long soft coat, the coypu has been farmed in many parts of the world including Britain, where in particular, it had escaped and established itself successfully in the countryside of Norfolk and North Suffolk to become a major pest by damaging riverbanks and crops.</p>
</section>
<section id="app">
<h2>Appearance</h2>
<p>The coypu distinctly resembles an otter and the word ‘Nutria’ is Spanish for otter. An adult coypu may weigh up to 9kg and grow to 600mm long, it is a powerful swimmer with webbed rear feet. It can hold and manipulate things using it’s long-clawed front feet. It’s tail is long and scaly, but not in anyway flattened.</p>
</section>
</main>
Got it? Good.
18 Responses
Pingback: The main element | Ian Devlin | HTML5 News | Scoop.it
hi,
thanks for this article.
usualy in this case i use the “article” tag instead of “main”.
Do you think that i should switch for “main” now ?
what is the difference between main & article ?
thanks
this is interesting, the is in fact very appropriate. One question though, what is that “role” thing? what makes it different from “div” or “class”?
Thank u for writing this article
If you currently have an article where you think you would have your main, wrap the article in the main element.
If you replace article with main, you lose the sectioning properties and “stand alone content” meanings of the article element and end up with a broken outline.
What is the article tag for then?
I actually never liked article since it describes the content too much. Some sort of listing doesn’t has an article but it does has a “main”. I also don’t like “aside”, since it’s not always on “the – side”.
Main and Secondary would be better in the first place I guesse than article and aside.
Pingback: When can I use the <main> element? - Techniek en Code
@mukenge, “role” is part of the WAI-ARIA specification. There’s a nice write-up on the use of landmark roles at http://www.paciellogroup.com/blog/2010/10/using-wai-aria-landmark-roles/
Those coypus sound like they’re really taking over Britain.
@Jason: re aside: it’s nothing to do with the side of anything. I believe it’s taken from theatre: when a character speaks something separate from the conversation(s) going on between characters, usually to the audience. Main would for example wrap your page’s main blog post, while aside would be “pssst! hey, get the archives here!”
@mukenge: by the way, so far as CSS is concerned, you can use roles for styling just as you use other attributes or classes:
div[role="main"] {special, dramatic styles;}
so they can do even more nice things for a developer.
Pingback: Bruce Lawson’s personal site : Reading List
Pingback: Reading list « barryvan
Pingback: Today’s Readings | Aaron T. Grogg
Pingback: Further examples on using the main element | Ian Devlin
Is it ok to include a sidebar inside the main-element as well? The reason being that I like to keep the number of elements used to a minimum and putting both the main-content and the sidebar inside the main-element will allow me to dispose of an extra div for positioning.
John, it would be ok to include the sidebar in the
mainelement if the contents of the sidebar relate to the contents of themainelement. Perhaps my other post on further examples on using themainelement might help?@Ian: Consider a “standard” layout. A page with a header containing a logo and navigantion, a footer containing links etc.
And between them an article or a list of products as main content, and a sidebar containing things like categories, tag-cloud, featured item, ads, etc.
Would you say both the main-content and the sidebar could be placed inside the main-element in such cases? The rule ” if the contents of the sidebar relate to the contents of the main element” is a bit vague to me. The word “relate” can mean anything since relationships between things can always be redefined to fit the need.
John, from the description you’ve provided, it sounds to me like the sidebar contains content that is shared across pages in the same way as the header with the logo and navigation are. If this assumption is correct then the sidebar would not be contained within the
mainelement.@Ian: thanks, I was afraid of that. Unfortunately this means that instead of replacing a div with the main-element, it means adding the main-element resulting in an extra element. More so because it is not a sectioning-element
Oh well, such is life.