This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Hello world!
Every HTML5 document begins with the document type declaration , and it does not matter if you write it in uppercase or lowercase. The second innovation you will notice is the shortened way of specifying the encoding— <meta charset="UTF-8">. The rest, like html, head, title, or body, you will be familiar with from HTML4, which leads us to the question: What is really new in HTML5?Hello world!
You probably cannot see much difference between this and the HTML version. That is due to the fact that we have not made full use of the permitted level of simplification in HTML code for the first Hello world! example. In lazy HTML5, this markup would have been sufficient: <meta charset=utf-8>Hello world! We can leave out quotation marks for attributes if the attribute value does not contain any spaces or any of the symbols " ' > / =. Tags can be written in uppercase or lowercase; sometimes they can even be omitted as in the preceding example. If you are not sure, the validator can once again help you out. Regarding implementation of the new HTML5 parser, Mozilla has taken the lead. Henri Sivonen’s Parser, which is also the basis of http://validator.nu, is included with Firefox 4.
1.4 Can I Start Using HTML5 Now? Yes and no. HTML5 is not finished yet by any stretch of the imagination, but unlike previous practice, the development of the HTML5 standard is taking place hand in hand with its implementation. Who would have thought that Internet Explorer 9 (IE9) would offer SVG and Canvas, or that Google would start offering HTML5 videos on YouTube? Many of the new features can be used now, provided you can choose your browser. HTML5 can be used in a company’s internal intranet as well as on your private homepage that only selected friends can access. With Firefox, Chrome, Opera, and Safari, four great browsers are already supporting a wide range of HTML5, and IE9 has finally ended Microsoft’s long hesitation in supporting web standards in 2011. Browser manufacturers and their developers are now actively participating in forming the standard. They implement new specification drafts first in test versions as proof of concept and then post their feedback and suggestions for improvements in the WHATWG or the W3C. This makes them important parts of the development cycle. Anything that cannot be implemented is removed from the specification, whereas other components are adapted and finally implemented.
1.4 Can I Start Using HTML5 Now?
Early adopters of HTML5 are well advised to familiarize themselves with the individual browser’s release notes, as trends in response to the question What will come next? will most likely emerge here: zz
https://developer.mozilla.org/en/HTML/HTML5
zz
http://www.opera.com/docs/changelogs
zz
http://webkit.org/blog
zz
http://googlechromereleases.blogspot.com
zz
http://ie.microsoft.com/testdrive/info/ReleaseNotes
The timeline of the development of HTML5-relevant specifications in combination with the milestones of browser releases indicate with their shorter and shorter release intervals that standardization and implementation are closely linked (see Figure 1.6). It will be interesting to see how the two areas continue to develop. You can find an up-to-date version of the timeline at the following URL: http://html5.komplett.cc/code/chap_intro/timeline.html?lang=en
Figure 1.6 Timeline of specifications and browser releases
17
18
Chapter 1—Overview of the New Web Standard
Summary This chapter begins with a bit of historical background and then provides a highlevel overview of the changes the HTML5 specification brings to web development. In addition to a look behind the scenes of the specification development, our main focus is on the long list of new elements, attributes, and APIs. Two brief Hello world! examples demonstrate the basic frame of a website encoded in HTML5 and XHTML5, and last but not least we address the question: Can I start using HTML5 now? The answer is yes, albeit with minor reservations. But now we will move on to the practical application of HTML5. Let’s first start with a big chunk of innovations: more structure and semantics for documents!
2 Structure and Semantics for Documents Both the previously mentioned MAMA survey conducted by Opera and Google’s study of Web Authoring Statistics of 2005 (http://code.google.com/webstats) conclude that it was common practice at that time to determine the page structure of web sites with the class or id attribute. Frequently used attribute values were footer, content, menu, title, header, top, main, and nav, and it therefore made sense to factor the current practice into the new HTML5 specification and to create new elements for structuring pages. The result is a compact set of new structural elements—for example, header, hgroup, article, section, aside, footer, and nav—that facilitate a clear page structure without detours via class or id. To illustrate this, we will use a fictitious and not entirely serious HTML5 blog entry to risk a look ahead to the year 2022 (see Figure 2.1). But please concentrate less on the content of the post and focus instead on the document structure.
19
20
Chapter 2—Structure and Semantics for Documents
Figure 2.1 The fictitious HTML5 blog
Before analyzing the source code of the HTML5 blog in detail, here are a few important links, for example, to the specification HTML: The Markup Language Reference—subsequently shortened and referred to as markup specification at http://www.w3.org/TR/html-markup. Here, Mike Smith, the editor and team contact of W3C HTML WG, lists each element’s definition, any existing limitations, valid attributes or DOM interfaces, plus formatting rules in CSS notation (if to be applied)—a valuable help that we will use repeatedly. The HTML5 specification also contains the new structural elements in the following chapter: http://www.whatwg.org/specs/web-apps/ current-work/multipage/sections.html The .html and .css files to go with the HTML5 blog are of course also available online at: zz
http://html5.komplett.cc/code/chap_structure/blog_en.html
zz
http://html5.komplett.cc/code/chap_structure/blog.css
At first glance, you can see four different sections in Figure 2.1—a header, the article, the footer, and a sidebar. All the new structural elements are used in these four sections. In combination with short CSS instructions in the stylesheet blog. css, they determine the page structure and layout.
2.1 Header with “header” and “hgroup”
2.1 Header with “header” and “hgroup” In the header we encounter the first two new elements: header and hgroup. Figure 2.2 shows the markup and the presentation of the header:
Figure 2.2 The basic structure of the HTML5 blog header
The term header as used in the markup specification refers to a container for headlines and additional introductory contents or navigational aids. Headers are not only the headers at the top of the page, but can also be used elsewhere in the document. Not allowed are nested headers or a header within an address or footer element. In our case the headline of the HTML5 blog is defined by header in combination with the logo as an img element and two headings (h1 and h2) surrounded by an hgroup element containing the blog title and a subtitle. Whereas it was common practice until now to write the h1 and h2 elements directly below one another to indicate title and subtitle, this is no longer allowed
21
22
Chapter 2—Structure and Semantics for Documents
in HTML5. We now have to use hgroup for grouping such elements. The overall position of the hgroup element is determined by the topmost heading. Other elements can occur within hgroup, but as a general rule, we usually have a combination of tags from h1 to h6. We can glimpse a small but important detail from the markup specification: The guideline is to format header elements as display: block in CSS, like all other structural elements. This ensures that even browsers that do not know what to do with the new tags can be persuaded to display the element concerned correctly. We only need a few lines of code to teach Internet Explorer 8 our new header element, for example: Of course there is also a detailed JavaScript library on this workaround, and it contains not only header, but also many other new HTML5 elements. Remy Sharp makes it available for Internet Explorer at http://code.google.com/p/ html5shim. NOTE
In computer language, the term shim describes a compatibility workaround for an application. Often, the term shiv is wrongly used instead. The word shiv was coined by John Resig, the creator of jQuery, in a post of that title (http://ejohn. org/blog/html5-shiv). It remains unknown whether he may in fact have meant shim.
As far as CSS is concerned, the header does not contain anything special. The logo is integrated with float:left, the vertical distance between the two headings h1 and h2 is shortened slightly, and the subtitle is italicized.
2.2 Content with “article” The article element represents an independent area within a web page, for example, news, blog entries, or similar content. In our case the content of the blog entry consists of such an article element combined with an img element to liven things up, an h2 heading for the headline, a time and address element for the
2.2 Content with “article”
date it was created and the copyright, plus three paragraphs in which you can also see q and cite elements for quotations of the protagonists. Because the content element is now lacking, although it ranked right at the top in web page analyses by Google and Opera, it did not make it into HTML5 for some reason. Our blog entry is embedded in a surrounding div (see Figure 2.3). So nothing stands in the way of adding further articles: