Chapter 1. The Web Platform

In this chapter, I’m going to talk about the web technologies that you’ll learn in this book, the knowledge you’ll need to get the most from reading it, and the demands and requirements of working on the multi-device Web. This chapter is about making sure we’re on the same page, figuratively speaking—I know we’re literally on the same page because you’re reading this now—before we get into the really technical things in the next chapters.

If you’re keen to get on with the learning you’re probably considering skipping straight to Chapter 2, but I urge you not to as Chapter 1 contains some quite interesting and useful background information, and the less technical subject matter allows me to show off the best examples of my wonderful sense of humor.

A Quick Note About Terminology

Throughout the book, I refer quite often to building websites or sites, but this terminology is just convenient shorthand to avoid repetition. The features you’ll learn from this book are relevant to websites, web applications, packaged HTML hybrid applications—in short, anything that can use HTML, CSS, and JavaScript. But that’s a mouthful, so I mostly just say “websites,” except when I need to be more specific.

I also use “browsers” and “user agents” interchangeably when what I mean is any instance of software that renders web pages or applications. Again, I’m just trying to avoid repetition. Once more, I’m trying to avoid repetition.

Before I begin, I’ll explain some of the presumptions I’m making about you and tell you what you need to know to get the most out of this book. First, let’s talk about you. Whether you’re a professional, you’d like to be, or you’re just someone who enjoys playing around with the Web, you have a working knowledge of HTML, CSS, and JavaScript—not to any deep, intimate level, but enough that you don’t need me to teach you what they are or how to write them.

Perhaps you learned to build websites a while ago and need to bring your skills up to date; maybe you’re learning web development at school and want extra lessons; or perhaps you’re a working developer but don’t get the opportunity to keep up with developments in coding for the Web. Whether any of those descriptions fit, I assume you want to get involved in building websites in a modern way, which work across multiple devices and are sympathetic to the dimensions and capabilities of each device—that’s doubtless why you picked up a book with this title.

This book builds on your knowledge of web development. It’s not a beginner’s guide, but it’s not an advanced book either. Rather, this book is a snapshot of current, new, and near-future features in HTML, CSS, JavaScript, and related technologies, with a bias toward those that are best for building sites in the multi-device world.

As well as that basic knowledge, you need to know your way around the developer tools in your browser, although not in any power-user kind of way. In some of the JavaScript examples, I log results into a tool’s developer console; this is a standardized method of working and is the same if you use the native tools in Chrome, Firefox, IE9+, Opera, Safari, or third-party tools like Firebug. I might, for example, use a line of code like this:

console.log('Hello World');

And the result will be shown in the console; Figure 1-1 shows how this is displayed in Firebug. As I said, I won’t use the console or developer tools much, but if you don’t know how to use them, you should really take the time to learn now.

If you’re still reading this, either you have all the knowledge required to proceed, or you’re getting ready to try to bluff your way through. Regardless, let’s move on to talk about technology.

There’s some confusion over what HTML5 actually is. There’s what the general public (and, for many of us, our clients) believe, and what it actually is. HTML5 is not a brand new platform that we use to build websites; it’s not a rich multimedia environment; it’s not a thing you enable to make your websites work across multiple devices. HTML5 is basically an attempt to evolve the Web to meet the demands of the way we use it today, which has mutated dramatically from its earliest iteration as a simple network of linked documents.

To the public at large, HTML5 has become a shorthand term for a series of related and complementary technologies, including CSS3, SVG, JavaScript APIs, and more. Although some developers are happy to use this broader meaning, I don’t really like this conflation of all the technologies, so I’m happier with calling HTML5 the web platform. I actually prefer Bruce Lawson’s proposed term, New Exciting Web Technologies (NEWT), which is both a cool acronym and has a cute logo, but I have to admit that I’ve lost this battle, so the web platform it is.

The web platform is vast. To see how vast, take a look at http://platform.html5.org/, which lists all of the technologies that are considered part of the platform; the list is really quite impressively long and contains far more than I could ever hope to cover in one book.

Instead, I’ll concentrate on the core, the technologies I feel are sufficient and useful for authoring websites that work across multiple devices: HTML5, CSS3, SVG, Canvas, and some device APIs. I’ll explain each of these as I get to them throughout the course of the book, but first I want to clarify in more detail what is meant by HTML5 and CSS3.

The Real HTML5

HTML5 is an iteration of HTML4.01 with some new features, a few deprecated or removed features, and some modified behaviors of existing features. Its aim is to standardize the many common hacks and design patterns that developers have used throughout the years and to expand in order to meet the demands of the modern Web, which is as much (if not more) about applications as it is about documents; indeed, the original proposal for what became HTML5 was called Web Applications 1.0.

New features in HTML5 include ways to structure documents for providing meaning and accessibility; I cover this in Chapter 2. HTML5 also has a whole range of new form functionality and UI controls that make it easier to build applications, which we’ll look at in Chapter 8. And HTML5 includes what many people still associate with it—native (without plug-ins) video, which is covered in Chapter 9.

Two main groups are working on HTML5, and their roles and responsibilities are broadly this: The WHATWG (you don’t need to know what that acronym means), a consortium of browser makers and “interested parties,” through the main spec editor Ian Hickson, creates a “living spec” of HTML—basically a versionless specification that constantly incorporates new features and updates existing ones; and the W3C (World Wide Web Consortium), the Web’s standards body, takes snapshots of this spec to create numbered versions, ensuring compatibility of implementation by the browser vendors.

The situation is, in fact, a bit more complex than that and plenty of political wrangling is going on, but that’s of interest only to standards wonks and shouldn’t make any practical difference to you.

The W3C has proposed, although not confirmed as I write this, that HTML5 (the W3C snapshot) be brought to Recommendation status—that is, “done”—by 2014, with HTML5.1 to follow in 2016. HTML5 would also be broken into separate modules rather than a single monolithic spec, so work can progress on different aspects without delaying the whole. These dates don’t really matter to you, however; all you need to know is when HTML5 is in browsers and ready to use.

The HTML5 Template

As someone with basic working knowledge of HTML, you’re familiar with fundamental page markup. But things have changed a little bit in HTML5—not much, but enough to mention. The following code block shows the basic template that I’ll use for all of the examples in this book (you can also see this in the example file template.html):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<title></title>
</head>
<body></body>
</html>

Most of it should be familiar to you, but I will discuss two points of interest. First is the Doctype. This is a remnant from the days when you had to tell the browser which type of document you were writing: strict HTML, transitional HTML, XHTML1.1, and so on. In HTML5, that’s no longer necessary—there is only one flavor of HTML—so the Doctype declaration really isn’t needed any more. In theory, that is.

Modern browsers tend to have three rendering modes: quirks mode emulates the nonstandard rendering of Internet Explorer 5, which is required for compatibility with legacy pages on the Web; standards mode is for modern, standards-compliant behavior; and almost standards mode is standards mode with a few quirks.

To know which mode to use, the browser looks to the Doctype. You always want to use standards mode, so the Doctype in HTML5 is the shortest possible that triggers standards mode:

<!DOCTYPE html>

The second point of interest, and the only other change to the standard HTML5 template, is the meta tag, which declares the range of Unicode characters used to render the text on the page—UTF-8 is the default used across the Web, so this is what you’ll use in most cases. The meta tag uses the charset attribute:

<meta charset="utf-8">

That’s really it. If a client ever asks you to “make their website HTML5,” you can update those two tags and charge them a fortune for it. (Please don’t; that was just a joke.)

I could have included plenty of other options, which I’ve left out for the sake of clarity and simplicity. The popular HTML5 Boilerplate website provides a comprehensive template, so look through the documentation to see what the template does—but please keep in mind it should be a starting point, not used verbatim.

In addition to the changes to the core template, HTML5 has one or two new best practices that you should consider implementing. HTML5 has been written to take advantage of the many different ways developers write code, so these shouldn’t be considered hard-and-fast rules, but in my opinion, they’ll make your code easier to write and maintain.

The first best practice is that you are no longer required to use the type attribute when calling the most common external resources. Using HTML4.01 or XHTML, you had to declare a type for each link, script, or style tag:

<link href="foo.css" rel="stylesheet" type="text/css">
<script src="foo.js" type="text/javascript"></script>

But when working on the Web, CSS and JavaScript are the de facto default resource types used with these tags, so writing them out every time is a little redundant. Therefore, you can now drop them, making your code a little cleaner while still being understood perfectly well by the browser:

<link href="foo.css" rel="stylesheet">
<script src="foo.js"></script>

The only time you need to use the tags is when you’re not using default CSS or JavaScript; for example, some releases of Firefox have experimental implementations of recent versions of JavaScript, and for safety’s sake they require that you include a flag on the type attribute if you want to use it:

<script src="foo.js" type="application/javascript;version=1.8"></script>

HTML5 is also very forgiving of syntax. Whether your preference is to use all lowercase characters, quote your attribute values, or close empty elements, HTML5 is happy to parse and understand them. That being the case, all of these are equal:

<img src=foo.png>
<img src=foo.png />
<IMG SRC="foo.png"/>

Some attributes, known as Boolean attributes, have only true or false values; their presence is presumed to mean true unless otherwise specified, so you don’t need to supply a value—unless you’re using an XML-like syntax where values are required, in which case you use the name of the attribute itself. This means both of these are the same:

<input type="checkbox" checked>
<input type="checkbox" checked="checked">

My own preference is to use all lowercase, all quoted, but not to close empty elements:

<img src="foo.png">

This is the style I use throughout the book, as I find it neater and easier to work with, and the text editor I use has syntax highlighting, which makes looking through the code nice and clear. You can use whichever system you want, but be consistent to help with maintainability.

As HTML5 is to HTML4.01, so CSS3 is to CSS2.1: an evolutionary iteration that standardizes some existing features that are implemented slightly differently across browsers, and introduces a whole new set of features to make CSS fit for purpose in a world where web browsers can be embedded anywhere.

The first CSS3 features to make it into browsers were largely presentational and based on hacks that developers had been using for years: using fonts from any source, rounded corners, and drop shadows on text and boxes. Next to land were a range of new selectors that made document traversal for styling much easier, and more dynamic effects such as two- and three-dimensional transitions and transitional animations (you can read more about these in The Book of CSS3, by this author, from this publisher).

But beyond the many glittery visual effects, the real revolution of CSS3 has come through media queries, a syntax that allows you to provide styles to browsers based on their dimensions and capabilities, the first step toward true multi-device styling. I cover media queries in Chapter 3, along with a range of other CSS properties that are useful for building responsive and adaptive websites.

The next big challenge for CSS to solve is the issue of layout—that is, to enable layouts that are truly sympathetic to the capabilities of the user agent viewing them. These include properties for dynamic user interfaces and CSS-controlled grid systems, which you’ll read more about in Chapter 4.

CSS3 is not a single spec as CSS2.1 was, where everything is described in the same document; it’s far too big and complex for that. Instead CSS3 is modular—a series of shorter, more specific specs that can be implemented by browsers in a modular way. As with HTML5, the idea of waiting until CSS3 is “ready” before using it is pretty foolish, as some modules will be ready and implemented long before others.

CSS modules are given level numbers to show how many iterations they’ve been through; some are already at level 4, and they could well be implemented before others that are at level 3. This doesn’t mean, however, that one day we’ll have a CSS4; there won’t be. CSS3 is a shorthand term for “everything more recent than CSS2.1,” and one day that distinction will be dropped and everything will be just CSS.

Vendor-Specific Prefixes

When browsers implement features in an experimental or prestandard way, they try to make them safe by using vendor-specific prefixes to avoid compatibility problems with standardized property names. Consider, for example, that a CSS Apes module proposes a new property called gorilla, and both Firefox and WebKit implement it experimentally but slightly differently. If both used the same property name, the effect would be different in each browser; instead, they avoid those potential conflicts by using a vendor prefix:

-moz-gorilla: foo;
-webkit-gorilla: foo;

In principle, the system is great, but in reality, things have gotten somewhat confused. Among other problems, some prefixed properties became so widely used by developers that other browser makers felt the need to implement their rivals’ vendor prefixes too, which is justifiable but kind of makes the whole thing nonsensical.

Browser makers are trying to bring this system under control, but on occasion using vendor-prefixed properties will be close to unavoidable. In most cases, I use only unprefixed properties in my code examples and make a note in Appendix M of where vendor prefixes need to be used.

You should understand by now that the multi-device web is unknowably vast and varied, that the range of browsers running on those devices is immense, and that even within those browsers there is a variety of versions and implementations (I hope you understand that, as most of the introduction was dedicated to trying to get that point across). That being the case, some of the features in this book may well not be implemented or may be implemented slightly differently.

Rather than covering the levels of implementation in the text, I treat every new feature as if it’s fully implemented and make a note of real-world implementation oddities and curiosities in Appendix M.

Also, cutting-edge standard proposals are subject to change, even when experimental implementations have already shipped in some browsers (the Grid Layout module featured in Chapter 4 was updated while I wrote this book), so by the time you read this, some of the syntax in the book may already be out-of-date. This is an unfortunate risk when working with evolving standards in dead tree publishing, but I try to mitigate it by noting features that may be at risk of changing and by keeping a list of errata and updates on the companion website, http://modernwebbook.com/.

You’ll probably want to follow a few online resources to learn about levels of implementation, although most tend to be focused on desktop and mobile browsers. Can I Use… shows levels of support for a wide range of technologies in recent past, current, and future versions of popular browsers, whereas HTML5 Please shows how safe it is in general to use cutting-edge features, mostly CSS3 and JavaScript (which is why the name annoys me a little).

The HTML5 Test is a site that tells you how many features from the HTML5 spec your browser supports, but also very usefully keeps records of implementation levels across many different browsers and devices, including TV and games console browsers, and also allows you to do side-by-side comparisons of up to three different browsers. The site is limited to only HTML5 support, however.

With the device landscape the way it is, the only way to be sure that the sites you build work across multiple devices is to test. Test at the start of the project, test at the end, and test at every opportunity all the way through. If you’re planning a multi-device project, factor in testing to take up to 40 to 50 percent of build time. Seriously.

You can’t find a substitute for testing on actual devices, so start building a library of as many devices as you can. If you work near other agencies, consider pooling resources so you can get a broader range. In many cities, open device labs are being assembled, with a range of devices donated by local developers and companies that are available for anyone to use. Search online for your nearest lab, or consider getting involved with creating one at your company or place of work.

And don’t stick to only mobile and tablet testing; think about games console browsers if you’re targeting your sites at a younger audience (research suggests that some one in four teens in the US use their games console browser to go online) or TV devices if your sites are aimed at a leisure market.

If you can’t get access to actual devices, some dedicated tools are available, and most (if not all) OS creators and/or device manufacturers have free-to-download software development kits (SDKs) with device emulators. In the mobile and tablet space, Android, Windows Phone, and Blackberry all have SDKs, and doubtless many more besides. Apple’s Xcode, available through the App Store, features an iOS Simulator that lets you switch between device and OS versions for testing.

Once set up, many of these SDKs also allow you to connect physical devices via USB to do debugging via a paired browser, but an easier way to do this is with Opera’s Mobile Emulator; once opened and connected to an Opera desktop version, you can use the developer tools on the desktop to debug the page on the mobile. If you need to use WebKit—and as it’s the dominant multi-device engine, why wouldn’t you?—software called weinre lets you connect a version of Chrome or Safari on the desktop with Android, iOS, or Blackberry emulators/simulators.

Adobe has a tool called Edge Inspect, which synchronizes the Chrome browser with any device running the Edge Inspect app (currently available on iOS and Android), allowing you to preview your site on many different devices simultaneously and use the Web Inspector for remote debugging.

This chapter provided you with all the information you need to get started in modern web development. I disambiguated the common meaning of HTML5 and introduced you to the web platform. You learned what HTML5 is useful for and how to start writing it, and you also had a brief introduction to CSS3.

The chapter’s key messages are in the latter parts: First, always keep up-to-date about the levels of implementation of web platform features across common browsers; and second, test, and then test, test, test some more, and when you think you have no more testing to do, test again. Then once more for good luck.

With all of that explained, let’s roll our sleeves up and get to work.