Did you find this useful?
Socialize it today.


HTML5 Semantic Markup and Page Layout

Thursday, October 04, 2012

HTML5 introduces a whole new set of semantic elements. Semantic markup is HTML that introduces meaning rather than presentation. When semantic elements are used on a page, the browser can interpret their purpose within the structure of the page. For example, when the HTML5 <footer> element is used on a page, the browser (as well as web spiders) will be more clear on the purpose of the contents of the element. This allows browsers and other user agents to handle the content in a specific manner. Contrast that with a common example that you may use when developing a web page based on HTML4.01. In the footer example, you would use a <div> element and style the element in a manner that resembles a typical footer. However, using the <div> element does not convey any clear meaning about its contents.

In the previous article in this series, you were introduced into some of the new HTML5 elments. Let's take a look at how some of these elements can be used in an HTML5 document.


html5 semantics

The image above shows a typical web page that can leverage 6 new HTML5 elements. The header and footer elements are self-explanatory. The nav element can be used to create a navigation bar. The section and article elements can be used to group your content. Finally, the aside element can be used for a sidebar of related links. Let us take a closer look at the actual markup.

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Page Title</title>
    <link href="style.css" rel="stylesheet" />
</head>
<body>
    <header>
        <hgroup>
            <h1>Main Header</h1>
            <h2>Subheader</h2>
        </hgroup>
    </header>
    <nav>
        <ul>
            <li><a href="#">Option 1</a></li>
            <li><a href="#">Option 2</a></li>
            <li><a href="#">Option 3</a></li>
        </ul>
    </nav>
    <section>
        <article>
            <header>
                <h1>Article #1</h1>
            </header>
            <section>
                This is the first article.
            </section>
        </article>
        <article>
            <header>
                <h1>Article #2</h1>
            </header>
            <section>
                This is the second article.  
            </section>
        </article>
    </section>
    <aside>
        <section>
            <h1>Links</h1>
            <ul>
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
            </ul>
        </section>
        <figure>
            <img src="#" alt="Image1" width="#" height="#" />
            <figcaption>My Image</figcaption>
        </figure>
    </aside>
    <footer>Footer Information</footer>
</body>
</html>


Supporting New HTML5 Elements

At the time of this writing, browsers do not have full support for HTML5 specific elements, so they just treat them as user defined tags when they are encountered in a page. All major browsers, except Internet Explorer will render the unrecognized element as a inline element, and give web developers the freedom to style them. So, aside from IE, web developers would be able to safely include these new elements in their web projects. Therefore, we only need to be concerned with handling these elements for IE browsers. Since IE does not know how to apply styles to them, we can use some JavaScript to ensure that we can mitigate this issue. There is a well documented technique online that solves this problem. The simple technique just requires that you create a DOM element with the same name as the tag. Once that is established, IE will honor the styling. Prior to version IE9 there was little to no support for HTML5 elements. Since IE still has a very high percentage of usage, it is important to ensure that your webpage functions correctly when accessed by IE. This is where the JavaScript comes in handy.

HTML5Shiv

HTML5Shiv is a JavaScript workaround that is used to enable the styling of HTML5 elements in versions of IE prior to version 9. To enable the HTML5Shiv workaround, place the following section of code into the head element. This will load the HTML5Shiv from the web server on the condition that the browser being used is a version of IE earlier than version 9.

<!--[if lt IE 9]>
     <script src="html5shiv.js"></script>
<![endif]-->

Internet Explorer prior to version 9 will process the statement. Other non-IE browsers will treat the conditional script as a comment. You can either download the JavaSript file or point directly to it on Google's site.

<!--[if lt IE 9]>
     <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

HTML5: The Missing Manual The Definitive Guide to HTML5 HTML CSS and JavaScript