HTML5 Step by Step

Faithe Wempen

Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 Copyright © 2011 Faithe Wempen Complying with all applicable copyright laws is the responsibility of the user. All rights reserved. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without express written permission of O’Reilly Media, Inc. Printed and bound in Canada. 2 3 4 5 6 7 8 9 10 11 TG 6 5 4 3 2 1 Microsoft Press titles may be purchased for educational, business or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Visit our website at microsoftpress.oreilly.com. Send comments to mspinput@ microsoft.com. Microsoft, Microsoft Press, ActiveX, Excel, FrontPage, Internet Explorer, PowerPoint, SharePoint, Webdings, Windows, and Windows 7 are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the author, O’Reilly Media, Inc., Microsoft Corporation, nor their respective resellers or distributors, will be held liable for any damages caused or alleged to be caused either directly or indirectly by such information. Acquisitions and Development Editors: Russell Jones and Kim Spilker Production Editor: Kristen Borg Production Services: Octal Publishing, Inc. Technical Reviewer: Joydip Kanjilal Indexing: Lucie Haskins Cover: Karen Montgomery Compositor: Octal Publishing, Inc. Illustrator: Robert Romano

978-0-735-64526-4

To Margaret

iii

Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv What Is HTML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Understanding HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Understanding Cascading Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Why Learn HTML in Notepad? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Choosing an HTML Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Why Code in HTML5? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Minimum System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Using the Practice Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii Conventions and Features in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix What Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx



Part 1

Getting Started with HTML

1

Editing and Viewing HTML Files

3

Opening a Web Page in Notepad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Adding the Data File Location to the Favorites List . . . . . . . . . . . . . . . . . . . . . . . 5 Opening a File from Windows Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Previewing a Web Page in a Web Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Making, Saving, and Viewing Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

What do you think of this book? We want to hear from you! M crosoft s nterested n hear ng your feedback so we can cont nua y mprove our books and earn ng resources for you. To part c pate n a br ef on ne survey, p ease v s t:

microsoft.com/learning/booksurvey    v

vi    Contents



2

Setting Up the Document Structure Specifying the Document Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating the HTML, Head, and Body Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Paragraphs and Line Breaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying a Page Title and Metatags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishing a File to a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



3

Formatting Text by Using Tags Creating Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying Bold and Italic Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying Superscript and Subscript Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Monospace and Preformatted Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatting a Block Quotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring View Settings in Internet Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



4

Using Lists and Backgrounds Creating Bulleted and Numbered Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nesting Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing the Bullet or Number Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying the Start of a Numbered List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Definition Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inserting Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inserting Horizontal Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Choosing Background and Foreground Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying a Background Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying a Foreground Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying a Background Image File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



5

Creating Hyperlinks and Anchors Hyperlinking to a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Partial Paths and Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Relative and Absolute Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting a Target Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13 14 15 17 19 22 23

25 26 29 31 32 37 40 45

47 48 49 50 51 54 55 57 60 60 61 61 62 65

67 68 68 69 70

Contents   vii



Hyperlinking to an E-Mail Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Hyperlinking to Anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hyperlinking to Other Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Part 2



6

71 75 78 81

Style Sheets and Graphics Introduction to Style Sheets

85

Understanding Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Constructing Style Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Creating Styles for Nested Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Creating Classes and IDs for Applying Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Applying Styles to Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Creating and Linking to External Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100



7

Formatting Text by Using Style Sheets

103

Specifying a Font Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying a Font Size and Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying Bold and Italics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying Strikethrough and Underlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Inline Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adjusting Spacing Between Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



8

Formatting Paragraphs by Using Style Sheets

104 107 111 114 117 119 123

125

Indenting Paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying a Border to a Paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying a Border Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Border Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying Border Width and Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatting Border Sides Individually . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting All Border Attributes at Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying the Horizontal Alignment of a Paragraph . . . . . . . . . . . . . . . . . . . . . . . . Specifying Vertical Space within a Paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

126 130 130 131 131 132 133 135 137 139

viii    Contents



9

Displaying Graphics

141

Selecting a Graphics Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preparing Graphics for Web Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inserting Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arranging Elements on the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controlling Image Size and Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hyperlinking from Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Thumbnail Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Including Alternate Text for Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Figure Captions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Part 3



10

11

Creating Navigational Aids

165

Creating Division-Based Layouts

12

Creating Tables

166 167 171 174 180 183

185

Understanding HTML5 Semantic Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Beginning to Think in Divisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Divisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an HTML5 Semantic Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Positioning Divisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Floating a Division to the Right or Left . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Positioning a Division on the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatting Divisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



153 155 158 158 161

Page Layout and Navigation Planning Your Site’s Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Text-Based Navigation Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Graphical Navigation Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Image Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Redirecting to Another URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



142 143 144 147 149

186 187 188 190 192 192 193 197 203

205

Creating a Simple Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Specifying the Size of a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Specifying the Width of a Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

Contents   ix



Merging Table Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Using Tables for Page Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229



13

Formatting Tables

231

Applying Table Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Applying Borders by Using Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Applying Borders by Using Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Applying Background and Foreground Fills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Changing Cell Padding, Spacing, and Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Setting Cell Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Setting Cell Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Setting Horizontal and Vertical Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249



14

Creating User Forms

251

Creating a Basic Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Text Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Special Field Types for E-Mail and Web Addresses . . . . . . . . . . . . . . . . . . . . . . Creating a Text Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Submit or Clear Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Default or Placeholder Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Check Boxes and Option Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Additional Input Types in HTML5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding CGI and Other Advanced Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



15

Incorporating Sound and Video

252 253 254 255 255 256 259 262 267 268 269

271

What’s New with Audio and Video in HTML5? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HTML Multimedia Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multimedia Formats and Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Codecs: Decoding the Video and Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Which Format to Choose? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File Size and Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Encoding Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

272 273 273 274 275 275 276

x    Contents

Embedding Video Clips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing the