Documento 1 de HTML5
Documento 1 de HTML5
1 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
1 Introduction to HTML5
2 de 20
5
6
7
8
9
10
11
12
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
<title>title</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
...
</body>
</html>
instead of:
<link href="file.css" rel="stylesheet" type="text/css"/>
We will not go into extra details about the <link> element, but the fact that the type attribute is becoming
optional shows the current direction taken by HTML5: towards greater simplicity.
Look also at the way we included a javascript file in our page:
<script src="script.js"></script>
3 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
Here again, the type attribute has been omitted. Just as a reminder, the old way to do the same
thing: <script type="text/javascript" src="script.js"></script>
History
As web site layouts evolved, HTML5 structuring elements like lists, paragraphs, tables, etc. showed
their limits. Today, many web sites offer navigation menus, tabbed panels, headers, footers, and so on.
The way these "parts"' are implemented relies heavily on <div> and <span> elements with
different id and class attributes, lots of CSS and lots of JavaScript code to apply custom styles and behaviors.
There are some problems however, with this approach:
id and class names differs from one developer to another, from one country to another, etc
Even with same ids and class names, the css rules may be different,
JavaScript libraries became heavier and heavier over the years,
Web pages became heavier and heavier over the years!
These elements could not be handled by the web browser natively...
Even if there are differences between ids, classes and css/js implementations, there are also common behaviors,
common layouts, common "way of doing things" that could be guessed at first glance by a human.
So different studies have been conducted in order to identify the most popular ids, class names, widgets, etc used on
the web:
According to http://dev.opera.com/articles/view/new-structural-elements-in-html5/, "During the creation of HTML5,
the editor Ian Hickson used Google's tools to mine data from over a billion web pages, surveying what ID and class
names are most commonly used on the real world web. You can see one of the surveys published at Google Code:
Web Authoring Statistics: Classes. Opera did a similar study, of 3.5 million URLs, calling it MAMA. MAMA had a
smaller URL set, but looked at a larger and wider variety of web page statistics. For more information, take a look
at MAMA common attributes, MAMA's id list, and MAMA's class list. For more options, go to the MAMA home
page."
4 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
elements (click on the element's name to go to the W3C specification about this element)
<header>
<footer>
<nav>
Section that contains the main navigation links (within the document or to other
pages).
<article>
Independent content, which can be individually extracted from the document and
syndicated (RSS or equivalent) without penalizing its understanding. Typically a
blog post.
<section>
Generic section used to group different articles into different purposes or subjects,
or to define the different sections of a single article. Generally used with a header.
<time>
<aside>
Section whose content is not necessarily directly related to the main content that
surrounds it, but can provide additional information.
<hgroup>
Used to wrap more than one heading if you only want it to count as a single heading
in the page's heading structure. News from 2 April 2013: the <hgroup> element
removed from the HTML5 specification.
<figure> and
<figcaption>
Used to encapsulate a figure as a single item, and contain a caption for the figure,
respectively.
And there is no <content> element even though the <div class="content"> was very popular. Instead,
the HTML5 group decided that anything not embedded in one of the elements from the above table is "default
content". If the content is of a type that corresponds to one of the elements from the table, i.e. if the content is an
article, it should be embedded between <article> and </article>.
5 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
This example is just one of the different ways to organize a blog. For this basic example, we have designed the web
site using a <header> element that contains the "Simple HTML5 blog" text that appears on top of the page:
The new <header> element
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML5 blog</title>
</head>
<body>
<header>
<h1>Simple <span>HTML5</span> blog</h1>
</header>
...
header {
color: #007e99;
font-size: 2.5em;
padding: 20px 50px
}
header span {
color: #722
}
The navigation menu just below the header is a <nav> element. For the purpose of this example we haven't
provided any value for the hyperlinks...
The <nav> element
1
2
3
4
5
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML5 blog</title>
</head>
6 de 20
6
7
8
9
10
11
12
13
14
15
16
17
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
And here is the CSS we used in this example for the <nav> element:
CSS for the <nav> elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
nav {
font-size: 1.5em;
margin: 5px 0;
padding: 20px 50px
}
nav li {
display: inline;
margin: 0 15px
}
nav li:first-child {
margin-left: 0
}
* html nav ul {
margin-left: -15px
}
nav span, nav a {
padding: 3px 15px 4px
}
nav span {
background: #722;
color: #fff
}
Now, we have one big <section> element that contains a set of <article> elements:
The <section> element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<section>
<article>
...
</article>
<article>
...
</article>
<article>
...
</article>
</section>
section {
float: left;
padding: 35px 0;
position: relative;
width: 70%
}
section article {
margin: 0 50px 40px;
padding: 25px 0 0;
position: relative
7 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
}
section header {
font-size: 1em;
padding: 0;
}
section h1 {
font-size: 2.3em;
}
Note that the H1, article, article header, etc. will be styled using these rules.
Next, in each article in the section we have a header (to display the article title), paragraphs (article
content), an so on. Example for the first blog article :
The <article> element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<section>
<article>
<header>
<h1><a href="">Information about this example</a></h1>
<h2>Some subtitle in the header</h2>
This example is a modified version of <a href="http://netstream.ru/htmlsamples/html5-blog/index.html">
http://netstream.ru/htmlsamples/html5-blog/index.html</a>
</header>
<p>Try to move the mouse on different elements. The structure will be highlighted and you will be able
to see the different inclusions of elements one in each other. If you move the cursor to this sentence,
it will be highlighted in dark grey, showing the presence of an <article> element, surrounded by a
<section> element (light grey), etc. So we have some articles in a single section element. The page
title at the top is a <header> element, while the tag cloud on the right is a <aside> element. The
main menu on top (with Blog, About, Contact) is a <nav> element.</p>
<figure>
<img src="http://www.fredcavazza.net/files/2009/09/html5_structure.png" alt="Example of HTML5 structuring
tags" />
<figcaption>
Fig. 1 : an example of how new structuring elements could be used. This page put a <nav> on top, and
does not have headers and footer for each article, like in this figure, but it could... By the way,
this is a <figcaption> inside a <figure> element...
</figcaption>
</figure>
</article>
...
</section>
Notice also the way we included a figure using the new "HTML5" way, using a <figure>..</figure>
element that embedded a <img src=.../> element together with a <figcaption> element.
Here is the CSS for the <figcaption> element we have used in the example (we did not apply any
style to the <figure> element):
CSS for the <figcaption> element
1
2
3
4
5
figcaption {
font-style:italic;
font-size: 0.8em;
width: 100%
}
8 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
After the long <section> element that contains all the blog articles displayed in the page, we added
the HTML code for the tag cloud that is displayed on the right of the page, "aside" ! This is done using
-you already guessed it- an <aside> element:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
We are not going to show the complete CSS here as it uses some tricks to display the list as a "real tag
cloud" that uses JavaScript for handling events, etc.
CSS for the <aside> element
1
2
aside {
float: right;
9 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
We used a float:right CSS rule to put the tag cloud on the right...
Here is the result:
Finally, we added a <footer> element after the tag cloud definition, for displaying a page footer:
The <footer> element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
...
<body>
...
<section>
...
</section>
<aside>
...
</aside>
<footer>
<p>© 2009 Some blog</p>
</footer>
</body>
</html>
footer {
clear: both;
color: #777;
padding: 10px 50px
}
10 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
<article id="id1">
<section id="id1part1">
<h2>Introduction</h2>
</section>
<section id="id1part2">
<h2>My travel to India</h2>
</section>
<section id="id1part3">
<h2>Return to France</h2>
</section>
</article>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
11 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
</footer>
</article>
In that case, the nav element proposes navigation links to the next or previous blog post, as well as a
link to contact the author of the blog post. Notice also that we used in that example a <footer>
element in the blog post.
Note: this page has been created by Moodle, a CMS-like software for creating online courses. The current version
12 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
does not use HTML5 structuring elements such as <section>, <article>, <header> etc. On this outline "Towards
more simplicity" is a <h1>, "A minimal HTML5 document" is a <h2>, etc...
But, before HTML5 we were stuck with only 6 hierarchy levels and in case of content syndication into RSS streams
or inclusion of some HTML parts into another HTML document, we could get "bad header hierarchies". Imagine
that we extract HTML content with only <h5> and <h6> headers and put this content inside another document or
into a RSS stream with only <h1> and <h2> headers. Outlining algorithms may be confused and/or the table of
contents that will be generated will have "missing hierarchy levels".
The new structuring elements of HTML5 solve this problem, and for the first time, an algorithm for outlining a
document is included in the specification.
Let's get back to our blog example : http://jsbin.com/agoyoj/7/edit
We will now look at the "outline" of our blog document. Since in September 2012 no web browser has a native
implementation of the outlining algorithm, we can try several tools:
Geoffrey Sneddon's on-line HTML5 outliner,
A browser extension such as this one for Chrome: Google HTML5 outliner, or the HTML5 Outliner Opera
Extension or this Firefox extension
Use a bookmarklet like the one also proposed on the Google HTML5 outliner page,
So, try one of these solutions with the example. You should get something like this (for the screenshots, we used the
Chrome extension):
Hmmm, there are some "Untitled NAV" and "Untitled SECTION" entries in there: we will see how to fix that later...
The interesting part is that the <header> elements have been taken into account. A good practice with HTML5 is to
have <header> elements for each "title part" of your document and hierarchize them. In our example, "Simple
HTML5 blog" is a <header> at the top of the hierarchy of <header> elements, while the whole "Information about
this example" is two levels further down in the hierarchy. And the "Untitled..." parts show that both <section> and
<article> elements are also parts of this hierarchy.
So... let's add <header> elements to fix these missing parts, and we will continue the discussion...
Fixed version: http://jsbin.com/agoyoj/9/edit
Parts that have been modified:
Fixed version with a header in the <nav> element
1
2
3
<!DOCTYPE html>
<html>
<head>
13 de 20
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
We just added a <header> in the <nav> element and in the <section> element. So here is the "fixed" result:
14 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
Source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</body>
</html>
Notice that we have two different <h1> elements in the <header> of the <section>.
Here is the result and the corresponding outline:
1
2
3
4
5
6
7
8
9
Note that we also added a <h2> element in the header, just to get a more complete outline in our example. Here is
the result:
Examples with the table of contents inserted directly in the page (no need for a browser
extension)
In these examples, we included directly in the HTML document a minified JavaScript version of the code that is run
by the HTML5 Outliner browser extension. The code comes from the "minifiedJS" link in the HTML5 Outliner
(h5o) page.
Look at the previous example, with a link for displaying the table of contents in a <aside> element in the main
<section>:
15 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
We also added in in a <aside> element, but not included in a <section>, in the blog example:
Online version: http://jsbin.com/agoyoj/73/edit
Before:
After:
16 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
17 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
18 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
The second form is certainly machine generated while the first one looks more than something a WYSIWYG editor
may produce.
If we look at this example now:
1 <body>
2
<h1>Apples</h1>
3
<p>Apples are fruit.</p>
4
<section>
5
<h2>Taste</h2>
6
<p>They taste lovely.</p>
7
<section>
8
<h3>Sweet</h3>
19 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
9
<p>Red apples are sweeter than green ones.</p>
10
</section>
11
</section>
12
<section>
13
<h2>Color</h2>
14
<p>Apples come in various colors.</p>
15
</section>
16 </body>
It uses h2 directly in <section> elements... this is not recommended in case of syndication. A preferred way is:
1 <body>
2
<h1>Apples</h1>
3
<p>Apples are fruit.</p>
4
<section>
5
<h1>Taste</h1>
6
<p>They taste lovely.</p>
7
<section>
8
<h1>Sweet</h1>
9
<p>Red apples are sweeter than green ones.</p>
10
</section>
11
</section>
12
<section>
13
<h1>Color</h1>
14
<p>Apples come in various colors.</p>
15
</section>
16 </body>
With multiple <h1> in each sectioning parts. In that cas h1..h6 are used for hierarchizing inside sectionning
elements. And nested sectioning contents (articles in sections or sections in sections or articles, etc.) add also
hierarchy levels, breaking the 1-6 levels of h.. barrier.
Document produced by a WYSIWYG editor vs a web site:
If we look again at the first example:
1 <body>
2
<h1>Let's call it a draw(ing surface)</h1>
3
<h2>Diving in</h2>
4
<h2>Simple shapes</h2>
5
<h2>Canvas coordinates</h2>
6
<h3>Canvas coordinates diagram</h3>
7
<h2>Paths</h2>
8 </body>
That is what a typical WYSIWYG editor in CMS/Blog would produce, the content between <body>..</body> will
need to be integrated in the web site layout, where <header>, <footer>, <article> and <section> will be used. It may
become:
1 <section>
2 <header>
3
<hgroup>
4
<h1>Posts from March 2013</h1>
20 de 20
http://classroom.w3devcampus.com/mod/book/tool/print/index.php?id=...
5
<h2>.../h2>
6
</hgroup>
7
<p>Whatever description....</p>
8 </header>
9 <article>
10
<h1>Let's call it a draw(ing surface)</h1>
11
<h2>Diving in</h2>
12
<h2>Simple shapes</h2>
13
<h2>Canvas coordinates</h2>
14
<h3>Canvas coordinates diagram</h3>
15
<h2>Paths</h2>
16 </artcile>
17 </section>
etc... sectioning elements and headers have been mainly designed for CMS/Blog softwares. The new way of
organizing h1..h6 and the way they interact with sectionning elements is much more simple to handle for blog/cms
developers.