Saturday, April 27, 2013

HTML5 Outline Introduction

In HTML5, the document outline is defined by the arrangement of your <h1,2,3,4,5,6> tags and new sectioning elements. The div tags do not affect it. For example, see the following document that describes the job list.

You may think the outline will be affected by div tags and then generate the following outline:

  1. I WANT YOU!
    1. Backend Engineer
      1. Java
      2. Hadoop
      3. Algorithm
    2. Frontend Engineer
      1. HTML
      2. CSS
      3. JavaScript

Unfortunately the actual document outline is

  1. I WANT YOU!
    1. Backend Engineer
      1. Java
      2. Hadoop
      3. Algorithm
      4. Frontend Engineer
        1. HTML
        2. CSS
        3. JavaScript

Because "Frontend Engineer" is located in the <h3> tag, it belongs to the closest h tag that is higher than itself. In this example, it belongs to "Backend Engineer" located in the h2 tag. So we must carefully arrange our h tags for generating the correct document outline. The following code will generate the correct outline:

And the other problem is that you are limited to 6 <h1,2,3,4,5,6> levels. The limitation makes merging several documents is hard: including a sub document in a main document means changing the level of the headings is a must if you want to keep the outline.

HTML5 provides section elements <section>, <article>, <aside> and <nav> that help you create more feasible structure to describe the document outline. The 1st important thing you should know:

Each section element can have its own heading hierarchy. Therefore, even a nested section can have an <h1>.

We can rewrite the previous example using section elements.

And this document will have the same outline.

  1. I WANT YOU!
    1. Backend Engineer
      1. Java
      2. Hadoop
      3. Algorithm
    2. Frontend Engineer
      1. HTML
      2. CSS
      3. JavaScript

Now the structure of the section elements determines the outline, not their heading rank, but the headings are still useful. The 2nd important thing is:

The first HTML heading element (one of <h1,2,3,4,5,6>) defines the heading of the current section element.

What happen when we add other heading elements in the same section element? They will create implied sections to keep compatibility with HTML4. The 3rd important thing is:

The first element of heading content in an element of sectioning content represents the heading for that section. Subsequent headings of equal or higher rank start new (implied) sections, headings of lower rank start implied subsections that are part of the previous one. In both cases, the element represents the heading of the implied section.

The following code shows the case of "Subsequent headings of equal or higher rank start new (implied) sections" :

The <section> elements' parent changes from <article> to the new implied section created by <h1> (A Fast learner):

  1. I WANT YOU!
    1. Backend Engineer
    2. A Fast learner
      1. Java
      2. Hadoop
      3. Algorithm

The following code shows the other case of "Subsequent headings of lower rank start implied subsections that are part of the previous one" :

The <h3> (A Fast learner) element creates a implied section that is a part of the <article> element:

  1. I WANT YOU!
    1. Backend Engineer
      1. A Fast learner
      2. Java
      3. Hadoop
      4. Algorithm

For section elements without a child heading element, we call them "Untitled Sections". <aside> and <nav> are often untitled sections. For accessibility, each section element should have one heading. If you do not want the heading to show on the page, you can hide it by CSS.

The elements <blockquote>, <figure>, <details>, <fieldset>, and <td> own separate outlines. Headings within these elements will not show in the main outline. They are sectioning root elements. For example, see the following code:

The heading within the blockquote element is not show in the outline:

  1. I WANT YOU!
    1. Backend Engineer
      1. Java
      2. Hadoop
      3. Algorithm

A well-defined document outline will be more accessible for search engines and other agents. So it is worth thinking about your document outline. You can install HTML5 Outliner Chrome Extension to check your document outline. And you can find more information about HTML5 outline on the following pages:

  1. Document Outlines
  2. Sections and Outlines of an HTML5 Document

No comments:

Post a Comment