semantic-html5-elements-page-structure

Please get more semantic with HTML5 page structure

The <head>

The HTML5 doctype declaration it’s very short:

<!doctype html>

So, it’s required by browsers that need the presence of a doctype to trigger standards mode, and this string does this reliably.
Then we need to define the document’s character encoding (it is recommended UTF-8). This should be in the first 512 bytes of the document.

You’ll notice that tag is much shorter than that we have used up until now:

Also, the attribute is not quoted (charset=”utf-8″) and the tag isn’t self-closed (<meta charset=utf-8 />).

HTML5 is not an XML language, so you don’t need to do those things, but you can if you prefer. All of these are equally valid HTML5.

The following document is valid, even if it has no <html> tag, no <head>, and no <body>:

<!doctype html>
Here is some text

This is because the browsers add missing elements in the DOM 
(IE seems to swap <title> and <meta>, however). 
Nevertheless, omitting these elements from your markup is likely 
to confuse your co-workers, so it's better to add them.

Is validation a necessary step?

Because HTML5 defines a consistent DOM for any bad markup (miss out tags like <html> , <head> and <body>) you could ask yourself if validation actually matters any more.

Validation is a good tool to ensure the quality of your code (no bad markups, no missing DOM elements) but is not a goal in itself. The goal is semantic markup: ensuring that the elements you choose define the meaning of your content as closely as possible, and don’t describe presentation.
You can use http://html5.validator.nu or http://validator.w3.org

New HTML5 structural elements

HTML5 brings 28 new elements, many of them inspired by the class and Id names most commonly used by developers.
Here is a sample blog home page marked up in HTML 4:

<header>
    <h1>Title one</h1>
</header>
<nav>
    <h2>Menu</h2>
    <ul>
        <li><a href="#">Option one</a></li>
        <li><a href="#">Option one</a></li>
    </ul>
</nav>
<article>
    <h2>Article 1</h2>
    Text write here.
</article>
<article>
    <h2>rticle 2</h2>
    Text write here.
</article>
<footer>This is copyright.</footer>

Then apply this simple CSS to it:

#sidebar {float:left; width:20%;}
.post {float:right; width:79%;}
#footer {clear:both;}

The HTML 4 structure of our blog:
html4 blog home page

There is nothing at all wrong with this markup (it’s working well in HTML5 also), but most of the structure is entirely unknown to a browser, as the only HTML element we can use for these important page landmarks is the semantically neutral: div.
In this case, HTML5 gives us new elements that unambiguously denote landmarks in a page.
So, we’ll rewrite our page to use some of these elements:

<header>
    <h1>Title one</h1>
</header>
<nav>
    <h2>Menu</h2>
    <ul>
        <li><a href="#">Option one</a></li>
        <li><a href="#">Option one</a></li>
    </ul>
</nav>
<article>
    <h2>Article 1</h2>
    Text write here.
</article>
<article>
    <h2>rticle 2</h2>
    Text write here.
</article>
<footer>This is copyright.</footer>

The HTML5 structure of our blog:

html5 blog home page

Yet in HTML5 there is no <content> element, so a search engine will know that the first piece of content that’s not in a <header>, <nav>, <aside>, or <footer>, is the beginning of the main content, regardless of whether it’s contained in an <article>, a <div>, or any direct descendant of the <body element.

When to use the new HTML5 structural elements

<header>

The specification says:

The header element represents a group of Introductory or navigational aids … Note: A header element Is Intended to usually contain the section’s heading (an h1-h6 element or an hgroup element), but this Is not required. The header element can also be used to wrap a section’s table of contents, a search form, or any relevant logos.

The first thing to note is that a header element is not required; its value is that it groups “Introductory or navigational” elements, so here’s an example:

<header><a href="/"><img src="logo.png" alt="home" /> My personal blog </a></header>

Many websites have a title and a tagline or subtitle, so the main heading and subtitle can be grouped into one logical unit (<strong>hgroup</strong> element):

<!-- The header can also contain navigation -->
<header><a href="/"><img src="logo.png" alt="home" /></a><hgroup>My personal blog
<h2>Text, text and text</h2>
</hgroup></header>

<nav>

This element is designed to mark up navigation (a collection of links, usually). The spec suggests that the “legal” links (copyright, contact, privacy policies, etc.) from the footer should not be wrapped in a <nav>:

It is common for footers to have a short list of links to various pages of a site, such as the terms of service, the home page, and a copyright page. The footer element alone is sufficient for such cases, without a nav element.

The <nav> elements contained in the new <aside> element:

can be used for typographical effects like pull quotes or sidebars, for advertising, for groups of nav elements, and for other content that is considered separate from the main content of the page.
<aside><nav>
<h2>News</h2>
<ul>...</ul>
</nav><nav>
<h2>Recent comments</h2>
<ul>...</ul>
</nav><section>
<h2>Follow me</h2>
<a href="#">twitter</a> <img src="..." alt="" />messages
</section></aside>

<footer>

It is defined in the spec as representing:

a footer for its nearest ancestor sectioning content or sectioning root element.” (“Sectioning content” includes article, aside, nav, section, and “sectioning root elements” are blockquote, body, details, fieldset, figure, td).

There can be more than one footer on a page, and as the spec says:

When the nearest ancestor sectioning content or sectioning root element is the body element, then it applies to the whole page.

<article>

The article element represents a component of a page that consists of a self-contained composition in a document, page, application, or site and that is intended to be independently distributed or reusable, e.g., in syndication.

A blog post, a tutorial, a news story, comic strip, or a video with its transcript all fit perfectly into this definition.

email-newsletter

Dev'Letter

Professional opinion about Web Development Techniques, Tools & Productivity.
Only once a month!

Any suggestion on what to write next time ? Submit now

Opinions

avatar
550
  Subscribe  
Notify of

Related Posts