For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance Contents....................................................................................................................v About the Author..................................................................................................xvii About the Technical Reviewer.............................................................................xviii Acknowledgments.................................................................................................xix Introduction............................................................................................................xx ■ Chapter 1: Moving to HTML5................................................................................1 ■ Chapter 2: A Rollover Picture Gallery................................................................39 ■ Chapter 3: Backgrounds....................................................................................55 ■ Chapter 4: Rollover Menus.................................................................................83 ■ Chapter 5: Moving to CSS3..............................................................................125 ■ Chapter 6: Audio, Video, and Slide Shows.......................................................141 ■ Chapter 7: Rounded Corners............................................................................169 ■ Chapter 8: Drop Shadows................................................................................191 ■ Chapter 9: Create Collages and Galleries.........................................................227 ■ Chapter 10: Add PayPal...................................................................................245 ■ Chapter 11: Secure Feedback Methods...........................................................261 ■ Chapter 12: Monitor Mayhem..........................................................................279 ■ Chapter 13: Appearance and Usefulness.........................................................293 ■ Chapter 14: Accessibility.................................................................................313 iii ■ Chapter 15: Dump Those Deprecated Items.....................................................337 ■ Chapter 16: Search Engine Optimization.........................................................357 ■ Chapter 17: Printing, Counting, and Redirecting.............................................377 ■ Chapter 18: Validation.....................................................................................399 ■ Chapter 19: Troubleshooting............................................................................415 ■ Appendix: Quick Reference, Techniques, and Useful Data...............................429 Index.....................................................................................................................451 iv Introduction This book concentrates on the practical application of HTML5 to projects that are currently not well documented. The book focuses on projects that will enhance your web sites; therefore, you will not find a history of HTML5 nor will you see a detailed discussion on things that are not commonly used by the great majority of web designers, such as APIs (application programming interface). Several books are available on the history of HTML5 and on APIs, so duplicating them is pointless. The aim of this book is to help you produce attractive and useful web sites by combining the advantages of HTML5 with exciting techniques that were previously poorly documented. The Origin of this Book Although I have designed many web sites over many years, I often had moments when I said to myself “how on earth do I do that?” or “how on earth did they do that?” The subjects of these “how on earth?” moments concerned techniques that were not usefully covered by any manuals that I could find. So, I assembled a collection of how-to techniques consisting of the results of my research and my practical experiments. Best of all, I compiled a collection of templates based on these techniques. These were extremely useful time-savers. The resulting volume (stored on my hard drive) was so useful that I decided it should be shared in book form with other designers. My templates were created in HTML5 and they include, where necessary, hacks to enable Internet Explorer 7 and 8 to understand HTML5 semantic tags.My quest for solutions was like an archaeological dig. I had evidence that the treasure was buried. Having eventually unearthed it, I found that these precious artifacts were fragmented and widely scattered; they were also in poor condition. My task was to assemble the fragments, then clean and polish them for public display. One problem remained, however, because many fragments were missing. I had to re-create these through trial and error. This book was born out of frustration. Most manuals, forums, and web sites give snippets of code on the topics covered in this book, but then the web designer is left to work out how to apply that code in the real world. That means sifting through many sources to piece together sufficient information. The practical application then requires more time for trial-and-error testing. Practical, fully-worked examples of these tools are often as rare as hen’s teeth. Busy web designers should not have to plough through pages of theory or history only to discover that no practical applications are provided. Nor should they have to read a verbose paragraph five times to extract some meaning from it. It is true that five of the book’s topics are already covered in hefty, single-subject manuals, but busy designers may not wish to buy a boatload of single-topic manuals and spend time trawling through them. In this book, these manuals are summarized in one chapter each, and the chapters are more than adequate for designers who wish to quickly create something such as a PayPal page or an accessible site, a web site optimized for search engines, or to acquire an introduction to HTML5 and CSS3. I tried to avoid those problems in this book by providing the following: • Practical and useful real-world examples • Screenshots of the end results • The markup in the form of fully-worked examples xx ■ INTRODUCTION • Step-by-step guidance in plain English to explain difficult items • Time-saving summaries of some single-topic manuals • Downloadable templates for users to adapt in their own web sites Is this Book for You? • Do you want to understand and use the enhancements provided by HTML5 and CSS3? • Are you an IT instructor or trainer looking for a set text that answers your students’ questions on HTML5, provides you with many ready-made projects, and gives students a valuable resource for their personal libraries? • Are you an IT student wishing to advance beyond the basic principles of HTML4, and CSS2? • You could spend many days and weeks searching the internet for how-to techniques. Would you rather have a collection of fully-worked examples of these otherwise hard-to-find tools? • Some of the projects in this book could eventually be constructed by piecing together snippets from a number of single-topic computer textbooks and web sites. Would you prefer to have them ready-constructed and described step-by- step in one book? • Many single-topic web design manuals are big and verbose. It takes time to trawl though them. Would you like the practical content adequately summarized in one chapter? • You will find several ways of doing the same job by searching for a technique on the internet or in books. I tested several of the techniques and chose the most straightforward methods that do the job well. Do you think the tried and tested techniques in this book will save you research and testing time? • Would you like a downloadable toolkit consisting of free, easily-adaptable templates? • Do your clients ask you to add enhancements to their web sites, but you’re not sure how to do it? • Do you need to know how best to update your client’s older web sites because they contain deprecated markup or because they are not accessible to the disabled? • Perhaps your prospective client used a paint-by-numbers kit, that is, a content management system (CMS) such as Joomla!, Textpattern, WordPress, or CMS Made Simple. Would you prefer to take full control of his web sites? This book will help you to break free from the limitations of the CMS templates and software. (Although if the web site is a blog or is database driven, the CMS approach may be a better choice.) xxi ■ INTRODUCTION What this Book Does and Does Not Cover The great majority of web site owners want a web site for the following reasons: • To sell a service or a product • To provide information • To publicize an organization such as a charity, a church, or a society This book caters to the great majority of web site owners. The book does not cover techniques that are adequately described and illustrated in readily available resources. It does, however, summarize four bulky manuals that are very time-consuming to read and extract information. This book does not discuss RSS feeds, nor does it have much discussion on JavaScript-driven APIs. The great majority of owners would be horrified if users could tinker with their web sites. Business owners would not have the time or inclination to moderate entries in blogs. The only interactive elements covered in this book are the truly practical ones, such as methods of payment (for example PayPal), secure feedback forms, page-printing buttons, audio and video controls, go-back buttons, and accessibility features. This book does not deal with blogs or database-driven web sites using SQL or MySQL. To learn how to take control of a CMS blog, I recommend Blog Design Solutions (Apress, 2006). The Layout Each chapter is self contained so that generally you can complete a task without jumping from chapter to chapter. I had to compromise occasionally when deciding where to locate a sub-topic; for example, where should I put CSS3 rounded corners: in the CSS3 chapter or in the chapter on rounded corners? I eventually placed it in the chapter on rounded corners. The chapters are in no special order and they do not have to be read in a particular order. Just dive in anywhere to discover what you want to learn. However, if you have little knowledge of HTML5, start with the first chapter. It will introduce you to HTML5 and it provides worked examples to practice on. I avoided unproductive detail such as the history of HTML5 or how a particular technique evolved. Instead, I concentrate on practical application. Some topics do need the how explained—such as how screen readers help the blind to read a web site—but my general aim has been to “cut to the chase” (as film producers say), that is, cut the waffle and get to the exciting bit as soon as possible. My hope is that by collecting and presenting the tools, techniques, summaries, and templates in one book, web designers, students, and teachers will be spared many hours of research and testing. The Level of Skill Required The instructions in this book are intermediate level; that is, it is assumed that you already have a working knowledge of HTML4 and CSS2. HTML5 and CSS3 Many HTML5 and CSS3 manuals have been published recently. They are good at describing the history behind the new recommendations and they tend to concentrate on less commonly used items such as APIs and the new canvas element. Worked examples in HTML5 were scarce, and very few were relevant to the real world. Most of the manuals dealt extensively with APIs, something that the vast majority of web site owners know nothing about and would not want anyway. This book emphasizes the practical xxii ■ INTRODUCTION aspect of HTML5, CSS2, and CSS3. I have cut to the chase and provided fully-worked projects covering all the most useful new features. ■ Note CSS3 and HTML5 are not dependent on each other; they are totally separate recommendations. Updates for CSS3 At the time of writing, Mozilla Firefox, Safari, and Chrome needed -mozkit- and -webkit- hacks in order to support some CSS3 features. When newer versions of these browsers are released, they may no longer need the hacks. Minimum Use of Scripts I have tried to reduce the use of scripts to a minimum. If there is a CSS solution, this will be used instead of a script. HTML5 and CSS3 offer script-free solutions for several new web site enhancements. The latest versions of most browsers support them. PHP Some PHP is used in two of the projects—visitor counters and forms—but no knowledge of PHP is required. Even though the script is fully explained, you can skip the explanations and simply insert your own details in the templates where indicated in the PHP markup. JavaScript To enable some HTML5 features to work in IE 7 and IE 8, a snippet of JavaScript is essential. This script is available for you to download from the book’s Apress web site. A small number of topics in this book use a little JavaScript; for example, CSS2 rounded corners (Chapter 7), enciphered email addresses (Chapter 11), and audio and video (Chapter 6), but no knowledge of JavaScript is required. The JavaScript files can be downloaded from the companion web site and placed in the root folder of your web site. Eventually, rounded corners will be achievable without scripts by using CSS3. Some browsers already support this technique. Cross-browser audio and video will eventually be achieved without scripts by using the new HTML5 <audio> and <video> tags. Meanwhile, a fallback solution is provided to enable audio and video to play using IE 7 and IE 8. Conventions Used in this Book The words code, listing, and markup are used in the book to mean the same thing. Code that should be replaced by the reader’s own markup is shown in bold italics. For instructional purposes, bold adds emphasis to some markup. The words client and web site owner are used synonymously to mean a person (or organization) commissioning you to produce a web site. The words tag and element are also used interchangeably. xxiii ■ INTRODUCTION Resources Many helpful books and free software programs are mentioned in this book. Wherever possible, freeware or open-source software has been chosen and fully tested. ■ Tip Install a text editor such as Notepad ++, which has several enhancements compared to MS Notepad. When a piece of CSS markup cannot be contained on one line, a left-pointing arrow is used to indicate that the two lines belong together; for example: #header { width:920px; height:180px; padding:0; margin: 20px auto; (cid:1) border:10px white solid; background: url(images/header3.jpg) no-repeat; } Using the Book’s Markup and Templates Most of the worked examples are practical templates that readers can view and download from the companion web site. Readers can easily and quickly adapt these examples for their own use. No permission is required for using the markup or the templates in a web site. Permission will be required if you include the markup examples in media for sale, that is, printed matter or a CD. If you use markup examples in a web site offering instruction on web design, permission is required and you will be asked to acknowledge where you found the code. The attribution should give the source, as follows: Practical HTML5 Projects by Adrian West. Copyright 2012 Adrian West. Published by Apress Media, LLC. ISBN 978-1-4302-4275-8. If you think that your particular use of the book’s markup is not covered by this paragraph, please contact [email protected]. WYSIWYG Web Design Programs WYSIWYG programs can be an excellent way of quickly starting a web page and a good way of learning the grammar and syntax of HTML. But don’t always believe what you see. Sometimes layouts, when viewed in the design pane of a WYSIWYG program, are not what you expect to see. Until the WYSIWYG programs catch up, this particularly applies to pages using HTML5 or XHTML5 DOCTYPES. Don’t struggle with the WYSIWYG layout, images, or menu bars; instead, test the page in Internet Explorer, Mozilla Firefox, Safari, Chrome, and Opera. You may be pleasantly surprised to see the layout, images, and menus displayed properly despite their odd appearance in the WYSIWYG editor. I use MS Expression Web. It has an excellent error checker. Pressing the F9 key in code view reveals and explains the errors step-by-step. However, any WYSIWIG editor may need either updating or upgrading to error check HTML5 pages. Web Design Programs and CMS Web design programs and content management systems (CMS) claim that you don’t need to learn HTML. They are the paint-by-numbers kits in the world of web site design. xxiv ■ INTRODUCTION I agree that you can design web sites using these programs without ever using or understanding HTML or CSS; however, you will be forced to use the templates that thousands of others are using. Your ability to adapt or fine-tune your web sites will be severely limited because most CMS use proprietary markup as well as JavaScript and PHP. All CMS sites load an enormous amount of baggage into your root folder; this may swallow up most of your available hosting capacity. For example, a basic five-page web site using HTML5 and CSS2 results in only two folders and six files. Using a CMS package for the same web site results in 17 folders with an average of 30 files in each, plus ten PHP files and several additional files for administering the web site. If you need to move a CMS site to another web master, you may have difficulty finding someone willing to take on the learning curve necessary to grapple with the complexities of fine-tuning a CMS web site. Encouraging beginners to use CMS (or online web site generators) is like saying to someone, “So, you’re going to France on business for three months? Don’t bother to learn French, just take a translator with you.” Should beginners wish to fine-tune a CMS web site, they will eventually need to learn HTML, JavaScript, and PHP. Web sites that can be designed online can be even more restrictive. These are mainly JavaScript-based, resulting in even less designer control. Online methods and CMS sites also use JavaScript navigation menus that prevent search engines from probing past the home page. You will never be in full control of your web design process unless you learn some HTML and CSS. If you want to produce unique, lean, clean, easily managed web sites, then HTML and CSS are the only way. HTML is not difficult, 40 words of HTML language will suffice, and all of them are English words or abbreviations of English words. Several free WYSIWYG web design programs are available to help you learn the syntax and grammar. Public libraries usually have HTML and CSS manuals on their shelves. Which Browsers? Currently, a majority of ordinary users surf with Internet Explorer because it comes free with Windows. A recent computer magazine poll showed that Internet Explorer, Mozilla Firefox, and Chrome are equally popular. Computer magazines cater to a small proportion of the population, so their survey results are rather biased. When considering IE, we need to be aware of the four versions in use. I assumed we could dismiss the ten-year-old IE 6. Any people still using IE 6 deserve what they see on their screens. However, I had a shock when I designed a web site for a group supporting my local public library. The county library’s computers were still using IE 6 despite its great age and vulnerability. The web site looked a mess on library computers; I fixed this with an IE conditional hack, (details of these hacks are provided in this book). Now I always check to see which browsers my clients are using. User inertia ensures that older browsers and operating systems will be in use for the next five years. Many businesses and home users will stay with Windows XP, IE 7, and IE 8 until at least 2014. They will continue to use IE 7 and IE 8 because IE 9 is not compatible with XP. People buying new computers from mid-2011 will have IE 9 pre-installed. Over the next decade, IE 9 and later versions may increasingly be the most-used versions of Internet Explorer. Internet Explorer 9, which was released in March 2011, will be compatible with the most useful new HTML5 and CSS3 features. People who are savvy enough to download and use Mozilla Firefox, Opera, Safari, and Chrome will normally continue to download the latest versions. Designers should, therefore, test on the most recent versions. For the present, so that your web sites reach the maximum audience, make sure they work in the five most popular browsers. They are, in order of importance, Internet Explorer 8 and IE 9+, Chrome, Mozilla Firefox, Safari for Windows, and Opera. They may not (and need not) display in an identical manner, but as long as they communicate the web site’s message effectively, small differences are not important. After 2015 you may able to forget about hacks for IE 8. xxv