Sweet Saw
  • Home

Archives

  • January 2022

Categories

  • Blogging
Sweet Saw
Sweet Saw
  • Home
BBlogging

HTML5 Doctor

  • byadmin
  • January 7, 2022
  • No comments

Recently,

was formally added to the W3C HTML specification. Now that the dust has settled, it’s about time we dive in to find out where and when it’s appropriate to use
. Let’s get started.

History

The inclusion of a main element (or similar) has long been debated in the working groups with authors and others often questioning why we had new elements such as

,

, and

but no element to accurately describe the primary content of a page.

Steve Faulkner, an accessibility consultant and new doctor in residence, undertook the hard graft of carrying out research, gathering data and use cases, and speaking to implementers to get them interested. As Steve explains, he talked to:

as many implementers (both general browser implementers and accessibility implementers), developers, authors and users as I could and got advice and input from them. I went where the various people hung out; on IRC, mailing lists, twitter, blogs, conferences — anywhere. Steve Faulkner

This led to his writing an extension specification for

and a thorough use case rationale.

The proposal was accepted in November 2012, and

was then rolled into the HTML5.1 specification. Recently, it was added to the HTML5 specification following no objections. Lets have a look at how the spec describes
.

W3C Specification

You should still use the ARIA role until all browsers map the role to the

element.

The primary purpose of

is to map ARIA’s landmark role main to an element in HTML. This will help screen readers and other assistive technologies understand where the main content begins. The W3C spec describes
as representing:

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. W3C HTML Editors Draft

Since the

element is now included in the HTML specification, the element has reverted to its HTML4 definition:

The body element represents the content of the document. W3C HTML Editors Draft

More Details

One important facet of

is that it can only be used once per page. Jeremy Keith wrote to the working group to understand why this is the case (rather than allowing multiple
s). Although I won’t go into detail, the discussion is an interesting read.

As per the spec, the W3C validator throws an error if you attempt to use multiple

elements per document.

Another stipulation of

is that it can’t be used as a descendant of an

,

,

,

, or

element.

Because

isn’t sectioning content, it doesn’t affect the document outline in the same way

,

, or

do.

Getting Going

Just as with the introduction of many other new HTML5 elements, not all browsers recognise

or have preset styles for it. You’ll need to ensure it displays as a block level element in your CSS:

For the time being, you’ll also need to use JavaScript to create the element for older versions of IE:

Of course, if you use the html5shiv,

is now baked in directly.

Implementing
on HTML5 Doctor

The easiest way to implement

is to replace that

that you’ve likely got with an id or class of something like main or content .

So what does that look like in practice? Well, here’s our pre-

markup on HTML5 Doctor:

[…]
[…]
[…]

And here’s what it’s like now:

We’ll get around to removing the id=”content” at some point too.

[…]

[…]

[…]

Yes, it’s really that simple. With any luck, it’ll take you less than five minutes to implement.

The WHATWG Version

The WHATWG definition of

differs from the W3C version in that the element has no meaning. It’s merely a styling hook (like a

with a new name) and represents its children.

The main element can be used as a container for the dominant contents of another element. It represents its children. WHATWG HTML

We recommend using the W3C version of

as described above.

Browser Support

Firefox 21, Chrome 26, and a WebKit r140374 have all implemented basic support for

.

They have all mapped the ARIA role=”main” to the

element so assistive technologies can now recognise the
element without issue.

Summary

As you’ve seen, it’s super easy to get up and running with

. It only takes a few minutes, so now really is the time to start adding it to the sites you’re developing.

If you aren’t sure when or where to use

, be sure to ask us in the comments and one of us will try to help you out.

Further reading

admin

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

— Previous article

PROJECT APPROACH

Next article —

Avon Grove School District approves 5-year charter renewal

Recent Posts

  • 10 Easy Blues Guitar Licks For Beginners
  • What Is The Definition Of Circadian Rhythm (Body Clock)?
  • Google / Googletest Public
  • Gift Guide: Holiday Baking Gifts And Essentials
  • 22 Plants To Grow In Your Fall Vegetable Garden

Recent Comments

No comments to show.
Sweet Saw
Designed & Developed by Code Supply Co.