Something seriously cool has happened in web development! Hi and welcome to the sample chapters of HTML5 & CSS3 for the Real World. You might assume that the person writing this foreword is a designer or a front-end developer, at the forefront of these new technologies. Well, that’s not the case. Let me reveal something about myself … I’m not a techie and I know nothing about code. Yes, I work at SitePoint and have copied and pasted my way through creating a few HTML emails, but that’s about as far as my skills go. So, you’re thinking, “Why are you writing this if you don’t know anything about HTML5 & CSS3?” Well, let me tell you what I do know. Last year, I started noticing lots of talk about these new technologies. I really didn’t pay much attention. But the chatter continued, and shortly after we decided to start work on an HTML5 & CSS3 book due to the huge demand from our customers. Now, it’s not unusual for the people I work with to get excited about new books. But there was so much enthusiasm for this upcoming book and its topic, I had to find out why. Over the next few months, I got involved in conversations around the office. When people spoke about all the cool new things you can do using HTML5 & CSS3, I listened. Most of it made no sense and my eyes glazed over, but there were a few times that I found myself thinking, “Hey, that sounds pretty awesome.” Then, last week, I asked our Sys Admin to do something for me and he needed to watch some instructional videos to get the job done, but they were not working. It had something to do with Flash. The comment that left my mouth afterwards surprised me just as much as it surprised him—“Well, that won’t be a concern for much longer since HTML5 allows for native video.”I still can’t believe I said it. I was so proud of myself for knowing that. I’ve worked at SitePoint for a few years now and have watched new technologies come and go, but have yet to see anything quite like this. HTML5 and CSS3 will change the way our designers and developers work for the better. One of our designers explained it like this: “We’ve been locked in, trying to get petty tasks done with so much effort. HTML5 & CSS3 allows me to finally do cool stuff and have some fun.” Well, that sounds pretty amazing to me. Read on and make up your own mind. Regards, Melinda Szasz HTML5 & CSS3 for the Real World (www.sitepoint.com) viiiWhat’s in This Excerpt Chapter 1: Introducing HTML5 and CSS3 Before we tackle the hands-on stuff, we’ll present you with a little bit of history, along with some compelling reasons to start using HTML5 and CSS3 today. We’ll also look at the current state of affairs in terms of browser support, and argue that a great deal of these new technologies are ready to be used today—so long as they’re used wisely. Chapter 2: Markup, HTML5-style In this chapter, we’ll show you some of the new structural and semantic elements that are new in HTML5. We’ll also be introducing The HTML5 Herald, a demo site we’ll be working on throughout the rest of the book. Think divs are boring? So do we. Good thing HTML5 now provides an assortment of options: article, section, nav, footer, aside, and header! Chapter 7: CSS3 Gradients and Multiple Backgrounds When was the last time you worked on a site that didn’t have a gradient or a background image on it? CSS3 provides some overdue support to developers spending far too much time wrangling with Photoshop, trying to create the perfect background gradients and images without breaking the bandwidth bank. Now you can specify linear or radial gradients right in your CSS without images, and you can give an element any number of background images. Time to ditch all those spare divs you’ve been lugging around. Chapter 11: Canvas, SVG, and Drag and Drop We devote the book’s final chapter to, first of all, covering two somewhat compettin technologies for drawing and displaying graphics. Canvas is new to HTML5, and provides a pixel surface and a JavaScript API for drawing shapes to it. SVG, on the other hand, has been around for years, but is now achieving very good levels of browser support, so it’s an increasingly viable alternative. Finally, we’ll cover one more new JavaScript API—Drag and Drop—which provides native handling of drag-and-drop interfaces.What’s in the Rest of the Book Chapter 3: More HTML5 Semantics Continuing on from the previous chapter, we turn our attention to the new way in which HTML5 constructs document outlines. Then we look at a plethora of other semantic elements that let you be a little more expressive with your markup. Chapter 4: HTML5 Forms Some of the most useful and currently applicable features in HTML5 pertain to forms. A number of browsers now support native validation on email types like emails and URLs, and some browsers even support native date pickers, sliders, and spinner boxes. It’s almost enough to make you enjoy coding forms! This chapter covers everything you need to know to be up to speed writing HTML5 forms, and provides scripted fallbacks for older browsers. Chapter 5: HTML5 Audio and Video HTML5 is often touted as a contender for the online multimedia content crown, long held by Flash. The new audio and video elements are the reason—they provide native, scriptable containers for your media without relying on a thirdpaart plugin like Flash. In this chapter, you’ll learn all the ins and outs of putting these new elements to work. Chapter 6: Introducing CSS3 Now that we’ve covered just about all of HTML5, it’s time to move onto its close relative CSS3.We’ll start our tour of CSS3 by looking at some of the new selectors that let you target elements on the page with unprecedented flexibility. Then we’ll follow up with a look at some new ways of specifying color in CSS3, incluudin transparency.We’ll close the chapter with a few quick wins—cool CSS3 features that can be added to your site with a minimum of work: text shadows, drop shadows, and rounded corners. Chapter 8: CSS3 Transforms and Transitions Animation has long been seen as the purview of JavaScript, but CSS3 lets you offload some of the heavy lifting to the browser. Transforms let you rotate, flip, skew, and otherwise throw your elements around. Transitions can add some subtlety to the otherwise jarring all-on or all-off state changes we see on our sites.We wrap up this chapter with a glimpse of the future; while CSS keyframe HTML5 & CSS3 for the Real World (www.sitepoint.com) xForeword Heard of Sjoerd Visscher? I’d venture to guess you haven’t, but what he considered a minor discovery is at the foundation of our ability to use HTML5 today. Back in 2002, in The Hague, Netherlands, Mr. Visscher was attempting to improve the performance of his XSL output. He switched from createElement calls to setting the innerHTML property, and then realized that all the unknown, non-HTML elements were no longer able to be styled by CSS. Fast forward to 2008, and HTML5 is gaining momentum. New elements have been specified, but in practice Internet Explorer versions 6-8 pose a problem, as they fail to recognize unknown elements; the new elements are unable to hold children and CSS has no effect on them. This depressing fact was posing quite a hindrance to HTML5 adoption. Now, half a decade after his discovery, Sjoerd innocently mentions this trick in a comment on the blog of the W3C HTMLWorking Group co-chair, Sam Ruby: “BTW, if you want CSS rules to apply to unknown elements in IE, you just have to do document.createElement(elementName). This somehow lets the CSS engine know that elements with that name exist.” Ian Hickson, lead editor of the HTML5 spec, was as surprised as the rest of theWeb. Having never heard of this trick before, he was happy to report: “This piece of informmatio makes building an HTML5 compatibility shim for IE7 far easier than had previously been assumed.” A day later, John Resig wrote the post that coined the term “HTML5 shiv.” Here’s a quick timeline of what followed: ■ January 2009: Remy Sharp creates the first distributable script for enabling HTML5 element use in IE. ■ June 2009: Faruk Ateş includes the HTML5 shiv in Modernizr’s initial release. ■ February 2010: A ragtag team of superstar JavaScript developers including Remy, Kangax, John-David Dalton, and PorneL collaborate and drop the file size of the script.■ March 2010: Mathias Bynens and others notice that the shiv doesn’t affect pages printed from IE. It was a sad day. I issue an informal challenge to developers to find a solution. ■ April 2010: Jonathan Neal answers that challenge with the IE Print Protector (IEPP), which captured the scope of the HTML5 shiv but added in support for printing the elements as well. ■ April 2010: Remy replaces the legacy HTML5 shiv solution with the new IEPP. ■ February 2011: Alexander Farkas carries the torch, moving the IEPP project to GitHub, adding a test suite, fixing bugs, and improving performance. ■ April 2011: IEPP v2 comes out. Modernizr and the HTML5 shiv inherit the latest code while developers everywhere continue to use HTML5 elements in a crossbroowse fashion without worry. The tale of the HTML5 shiv is just one example of community contribution that helps to progress the open web movement. It’s not just the W3C or the browsers who directly affect how we work on the Web, but people like you and me. I hope this book encourages you to contribute in a similar manner; the best way to further your craft is to actively share what you learn. Adopting HTML5 and CSS3 today is easier than ever, and seriously fun. This book presents a wealth of practical information that gives you what you need to know to take advantage of HTML5 now. The authors—Alexis, Louis, and Estelle—are wellresppecte web developers who present a realistic learning curve for you to absorb the best practices of HTML5 development easily. I trust this book is able to serve you well, and that you’ll be as excited about the next generation of the Web as I am. —Paul Irish jQuery Dev Relations, Lead Developer of Modernizr and HTML5 Boilerplate April 2011 HTML5 & CSS3 for the Real World (www.sitepoint.com) xxiiChapter1 Introducing HTML5 and CSS3 This chapter gives a basic overview of how we arrived where we are today, why HTML5 and CSS3 are so important to modern websites and web apps, and how using these technologies will be invaluable to your future as a web professional. Of course, if you’d prefer to just get into the meat of the project that we’ll be building, and start learning how to use all the new bells and whistles that HTML5 and CSS3 bring to the table, you can always skip ahead to Chapter 2 and come back later. What is HTML5? What we understand today as HTML5 has had a relatively turbulent history. You probably already know that HTML is the predominant markup language used to describe content, or data, on the World Wide Web. HTML5 is the latest iteration of that markup language, and includes new features, improvements to existing features, and scripting-based APIs. That said, HTML5 is not a reformulation of previous versions of the language—it includes all valid elements from both HTML4 and XHTML 1.0. Furthermore, it’s been designed with some primary principles in mind to ensure it works on justabout every platform, is compatible with older browsers, and handles errors gracefullly A summary of the design principles that guided the creation of HTML5 can be found on the W3C’s HTML Design Principles page1. First and foremost, HTML5 includes redefinitions of existing markup elements, and new elements that allow web designers to be more expressive in the semantics of their markup. Why litter your page with divs when you can have articles, sections, headers, footers, and more? The term “HTML5” has additionally been used to refer to a number of other new technologies and APIs. Some of these include drawing with the