Chapter 18 | The article, section and main elements
On the menu in this chapter we have the following:
article
section
- The
main
element for good measure
First up, the main
element.
main
While the main
element is not part of the group of sectioning elements, but part of the grouping elements, it would be hard to cover article
and section
without introducing main
. While the elements we have covered so far mostly relate to the elements that live around our pageโs content, tangentially related content if you will ๐, section
, and more specifically article
, relate to the meat and potatoes of our page.
The main
content if you will. ๐
As the name suggests, the main
element is used to identify the main content of the page or web application. It has an implicit ARIA role of main which is exposed to assistive technologies, and as in the movie The Highlander, there can only be one per page. Grabbing some excerpts from previous days, the main
element will then be used as follows:
|
|
Live Codepen - The main
element
NOTE: Why add
role="main"
if it is implicit already? The addition of therole
attribute largely depends on your intended audience. If you know that the users of your website or application will be using ever-green browsers, there is no need to specify the role. If however some of your users may be using versions of Internet Explorer 11 or older, therole
attribute is required in order to convey the semantic meaning to assistive technologies.
While there can only be one main
element per document, you could have more than one if the additional main
elements have the hidden
attribute specified. This can be useful to swap out different pieces of “main” content as a user interacts with your webpage, for example:
|
|
Using JavaScript one can toggle on and off the hidden
attribute on the relevant main
element as the user clicks the various links in the table of contents. This ensures that only one main
element is active and visible at a time, making this a valid use case.
Live Codepen - Using multiple main
elements
article
Following close on the heels of main
is the article
element. While it does not define the main content of a page, it is used to contain content on a page that is self contained. In other words, if you take an article
element and its contents out of the page and present it by itself, it should still make sense. The article
element has an implicit role of article
but, can be assigned a custom role of feed
, presentation
, none
, document
, application
, main
or region
, but it probably best to use the main
element when the intended role is main.
The article
element is commonly used as a container for the main article content(no surprise there ๐), but also to contain a list of blog entries for syndication for example:
|
|
As expected, an article
element can contain the other sectioning elements such as header
, footer
, section
, etc., to further semantically structure the content of the article.
section
The section
element is a little controversial. The general feeling is that the section
element is a glorified div
element and could really have been left out of the specification. While that may be true to some extent, it is not an entirely one-sided affair. It is also not a best practice to use a section
element as a container when it merely acts as a style or scripting hook. In those instances, you should definitely opt for the div
element.
The section
element comes into its own when used with an accessible name, as in the following example:
|
|
When the section
element is used with an accessible name as above, it is assigned the ARIA role of region and exposed as such to assistive technologies. While this is the most useful use of the section
element, it is by no means the only way it can be used. The general rule is: “the section element is appropriate only if the elementโs contents would be listed explicitly in the document’s outline.” ~ The Official HTML documentation
Here I agree with Bruce Lawson(see article linked in related reading), and would stick to using the section
element only when I am using it explicitly as a region
.
That was a lot of ground to cover but, you now have a darn good toolbox to semantically order and section your content.
Live Codepen - A complete blog post page example
There is one more set of elements to cover in this (cough)section(cough) before we move on to grouping content elements. Keep making the web awesome! ๐