v
What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey
Contents
Acknowledgments . . . xiiiIntroduction . . . 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 . . . . 3Adding 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
2 Setting Up the Document Structure 13
Specifying the Document Type . . . . 14
Creating the HTML, Head, and Body Sections . . . . 15
Creating Paragraphs and Line Breaks . . . . 17
Specifying a Page Title and Metatags . . . . 19
Publishing a File to a Server . . . . 22
Key Points . . . . 23
3 Formatting Text by Using Tags 25
Creating Headings . . . . 26Applying Bold and Italic Formatting . . . . 29
Applying Superscript and Subscript Formatting . . . . 31
Using Monospace and Preformatted Text . . . . 32
Formatting a Block Quotation . . . . 37
Configuring View Settings in Internet Explorer . . . .40
Key Points . . . . 45
4 Using Lists and Backgrounds 47
Creating Bulleted and Numbered Lists . . . .48Nesting Lists . . . . 49
Changing the Bullet or Number Character . . . . 50
Specifying the Start of a Numbered List . . . . 51
Creating Definition Lists . . . .54
Inserting Special Characters . . . . 55
Inserting Horizontal Lines . . . . 57
Choosing Background and Foreground Colors . . . .60
Specifying Colors . . . .60
Applying a Background Color . . . . 61
Applying a Foreground Color . . . . 61
Specifying a Background Image File . . . . 62
Key Points . . . . 65
5 Creating Hyperlinks and Anchors 67
Hyperlinking to a Web Page . . . .68Using Partial Paths and Filenames . . . .68
Using Relative and Absolute Paths . . . . 69
Setting a Target Window . . . . 70
Contents vii
Hyperlinking to an E-Mail Address . . . . 71
Creating and Hyperlinking to Anchors . . . . 75
Hyperlinking to Other Content . . . . 78
Key Points . . . . 81
Part 2
Style Sheets and Graphics 6 Introduction to Style Sheets 85
Understanding Styles . . . .86Constructing 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 . . . .104Specifying a Font Size and Color . . . .107
Applying Bold and Italics . . . .111
Applying Strikethrough and Underlining . . . . 114
Creating Inline Spans . . . . 117
Adjusting Spacing Between Letters . . . .119
Key Points . . . .123
8 Formatting Paragraphs by Using Style Sheets 125
Indenting Paragraphs . . . .126Applying a Border to a Paragraph . . . .130
Specifying a Border Style . . . .130
Setting Border Padding . . . .131
Specifying Border Width and Color . . . .131
Formatting Border Sides Individually . . . .132
Setting All Border Attributes at Once . . . .133
Specifying the Horizontal Alignment of a Paragraph . . . .135
Specifying Vertical Space within a Paragraph . . . .137
Key Points . . . .139
9 Displaying Graphics 141
Selecting a Graphics Format . . . . 142
Preparing Graphics for Web Use . . . . 143
Inserting Graphics . . . .144
Arranging Elements on the Page . . . . 147
Controlling Image Size and Padding . . . .149
Hyperlinking from Graphics . . . .153
Using Thumbnail Graphics . . . .155
Including Alternate Text for Graphics . . . .158
Adding Figure Captions . . . .158
Key Points . . . . 161
Part 3
Page Layout and Navigation 10 Creating Navigational Aids 165
Planning Your Site’s Organization . . . .166Creating a Text-Based Navigation Bar . . . .167
Creating a Graphical Navigation Bar . . . . 171
Creating an Image Map . . . . 174
Redirecting to Another URL . . . .180
Key Points . . . .183
11 Creating Division-Based Layouts 185
Understanding HTML5 Semantic Tags . . . .186Beginning to Think in Divisions . . . .187
Creating Divisions . . . .188
Creating an HTML5 Semantic Layout . . . .190
Positioning Divisions . . . .192
Floating a Division to the Right or Left . . . .192
Positioning a Division on the Page . . . .193
Formatting Divisions . . . .197
Key Points . . . .203
12 Creating Tables 205
Creating a Simple Table . . . .207Specifying 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 . . . .232Applying 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 . . . .252Creating a Text Box . . . .253
Special Field Types for E-Mail and Web Addresses . . . .254
Creating a Text Area . . . . 255
Creating a Submit or Clear Button . . . . 255
Adding Default or Placeholder Text . . . .256
Creating Check Boxes and Option Buttons . . . . 259
Creating Lists . . . .262
Additional Input Types in HTML5 . . . .267
Understanding CGI and Other Advanced Tools . . . .268
Key Points . . . .269
15 Incorporating Sound and Video 271
What’s New with Audio and Video in HTML5? . . . .272HTML Multimedia Basics . . . .273
Multimedia Formats and Containers . . . .273
Codecs: Decoding the Video and Audio . . . . 274
Which Format to Choose? . . . .275
File Size and Quality . . . .275
Encoding Video . . . . 276
Embedding Video Clips . . . .277
Introducing the <video> Tag . . . .277
The <embed> Tag: Your Fallback Plan . . . .279
Placing a Video Clip on a Web Page . . . .279
Incorporating Audio on a Web Page . . . .282
Playing Audio with the <audio> Tag . . . .282
Playing Audio in Older Browsers . . . .283
Placing an Audio Clip on a Web Page . . . .283
Key Points . . . .285
16 Including JavaScript and External Content 287
Introducing the Canvas . . . .287JavaScript, Briefly . . . .288
Including JavaScript on Your Web Page . . . .289
Your First JavaScript Web Page . . . .289
JavaScript Events and jQuery . . . .292
Obtaining jQuery . . . .293
Getting Ready for jQuery . . . .294
Selecting Elements with jQuery . . . .295
Calling Functions with JavaScript . . . .297
Responding to Events with jQuery and JavaScript . . . .298
Using the HTML5 <canvas> Tag . . . .303
Including External Content in Web Pages . . . . 310
Key Points . . . . 311
Part 4
Other Ways to Create HTML Code 17 HTML and Microsoft Expression Web 315
Exploring the Expression Web Interface . . . . 316Creating Web Sites and Web Pages . . . .320
Create a Page by Using a CSS Template . . . .325
Insert Graphics . . . .328
Import an Images Folder . . . .328
Place Images on a Page . . . .330
Add a Background Image to a Division . . . . 331
Formatting Text . . . .333
Formatting a Division . . . .338
Contents xi
Inserting Hyperlinks . . . .340
Key Points . . . .343
Part 5
Appendixes A Designing for Usability 347
Understanding Usability . . . .347Planning for Usability . . . .348
Sketching the Site Organization . . . .349
Designing a Consistent Page Template . . . .349
Designing the Content of Individual Pages . . . .350
Performing Usability Testing . . . . 351
B Designing for Accessibility 353
Guideline 1: Provide Equivalent Alternatives to Auditory and Visual Content . . . .354Guideline 2: Don’t Rely on Color Alone . . . .354
Guideline 3: Use Markup and Style Sheets, and Do So Properly . . . . 355
Guideline 4: Clarify Natural Language Usage . . . .356
Guideline 5: Create Tables that Transform Gracefully . . . .356
Guideline 6: Ensure that Pages Featuring New Technologies Transform Gracefully . . 357 Guideline 7: Ensure User Control of Time-Sensitive Content Changes . . . .357
Guideline 8: Ensure Direct Accessibility of Embedded User Interfaces . . . .358
Guideline 9: Design for Device Independence . . . .358
Guideline 10: Use Interim Solutions . . . . 359
Guideline 11: Use W3C Technologies and Guidelines . . . . 359
Guideline 12: Provide Context and Orientation Information . . . .360
Guideline 13: Provide Clear Navigation Mechanisms . . . .360
Guideline 14: Ensure that Documents are Clear and Simple . . . .361
C Tags Added and Removed in HTML5 363
Tags Added in HTML5 . . . .363Tags Removed in HTML5 . . . .364
Glossary . . . 365
Index. . . 369
About the Author. . . 385
xiii
Acknowledgments
Thank you to the wonderful editorial staff at O’Reilly Media for guiding this book smoothly through the editorial and production process . This is my first book for O’Reilly, and I certainly hope that it won’t be the last . It was a pleasure working with you all .
Introduction
Hypertext Markup Language (HTML) is the underlying markup language of the World Wide Web . It’s the common thread that ties together virtually every Web site, from large- scale corporate sites such as Microsoft’s to single-page classroom projects at the local grade school .
Don’t let the phrase “markup language” intimidate you . A markup language annotates or “marks up” plain text, letting a browser know how to format that text so it looks good on a Web page . It’s easy to get started—in fact, you can create a simple Web page in just a few minutes . While full-featured What You See Is What You Get (WYSIWYG) tools exist that can help speed up the process of writing Web pages, all you really need is an ordi- nary text-editing program such as Microsoft Notepad . You don’t need special software or extensive training .
In this introduction, you’ll learn some basics about HTML . You’ll find out how they turn plain text into attractive formatting, how they incorporate graphics and hyperlinks, and how anyone can create Web content in virtually any program that edits text . This intro- duction explains what cascading style sheets (CSS) are, and how they make formatting consistent across large Web sites . You’ll also discover the differences between HTML4, XHTML, and HTML5, so you can make the important decision about which version of HTML you want your code to conform to . Finally, you’ll learn about the conventions used in this book for pointing out special helps like notes, tips, cautions, and references to the data files .
How to Access Your Online Edition Hosted by Safari
The voucher bound in to the back of this book gives you access to an online edition of the book . (You can also download the online edition of the book to your own computer;
see the next section .)
To access your online edition, do the following:
1. Locate your voucher inside the back cover, and scratch off the metallic foil to reveal your access code .
2. Go to http://microsoftpress.oreilly.com/safarienabled .
3. Enter your 24-character access code in the Coupon Code field under Step 1:
(Please note that the access code in this image is for illustration purposes only .) 4. Click the CONFIRM COUPON button .
A message will appear to let you know that the code was entered correctly . If the code was not entered correctly, you will be prompted to re-enter the code .
5. In this step, you’ll be asked whether you’re a new or existing user of Safari Books Online . Proceed either with Step 5A or Step 5B .
5A. If you already have a Safari account, click the EXISTING USER – SIGN IN but- ton under Step 2 .
Download from Wow! eBook <www.wowebook.com>
Introduction xvii
5B. If you are a new user, click the NEW USER – FREE ACCOUNT button under Step 2 .
●
● You’ll be taken to the “Register a New Account” page .
●
● This will require filling out a registration form and accepting an End User Agreement .
●
● When complete, click the CONTINUE button .
6. On the Coupon Confirmation page, click the My Safari button .
7. On the My Safari page, look at the Bookshelf area and click the title of the book you want to access .
How to Download the Online Edition to Your Computer
In addition to reading the online edition of this book, you can also download it to your computer . First, follow the steps in the preceding section . After Step 7, do the following:
1. On the page that appears after Step 7 in the previous section, click the Extras tab . 2. Find “Download the complete PDF of this book,” and click the book title:
A new browser window or tab will open, followed by the File Download dialog box:
3. Click Save .
4. Choose Desktop and click Save .
5. Locate the .zip file on your desktop . Right-click the file, click Extract All, and then follow the instructions .
Note If you have a problem with your voucher or access code, please contact
[email protected], or call 800-889-8969, where you’ll reach O’Reilly Media, distributor of Microsoft Press books.
xix
What Is HTML?
In simple terms, a Web page (or HTML document) is a plain text file that has been encoded using Hypertext Markup Language (HTML) so that it appears nicely formatted in a Web browser . Here’s what HTML means, word-by-word:
●
● Hypertext Text that you click to jump from document to document . This is a refer- ence to the ability of Web pages to link to one another .
●
● Markup Tags that apply layout and formatting conventions to plain text . Literally, the plain text is “marked up” with the tags .
●
● Language A reference to the fact that HTML is considered a programming language . Tip When people think of computer programming, they usually think of writing a compiled program. A compiled programming language runs the human-readable programming code through a utility that converts it to an executable file (usually with an .exe or .com extension), which is then distributed to users. In contrast, HTML is an interpreted programming language.
That means the program is distributed in human-readable format to users, and the program in which it is opened takes care of running it. The HTML code for Web pages resides in files. Each time your Web browser opens a Web page, it processes the HTML code within the file.
Understanding HTML Tags
The code within an HTML file consists of text surrounded by tags . These tags indicate where the formatting should be applied, how the layout should appear, what pictures should be placed in certain locations, and more .
For example, suppose you wanted a certain word to be italicized, like this:
Everything is on sale .
In HTML, there’s no Italics button to click, like there is in a word-processing program . There- fore, you have to “tag” the word that you want to be italicized . The code to turn on italics is
<i>, and the code to turn italics off is </i> . Your HTML code would look something like this:
<i>Everything</i> is on sale.
That’s an example of a two-sided tag, which encloses text between opening and closing tags, in this case <i> and </i> . Note the forward slash in the closing tag (</i>) . That slash differentiates an opening tag from a closing tag . With a two-sided tag, there is always a corresponding closing tag for every opening tag .
To understand how this system of tagging came about, you need to know that back in the olden days of the Internet, nearly everyone connected to it by using a dial-up modem, at speeds ranging from 2400 bps to 28 .8 Kbps . That’s really slow . Text files transfer much faster than binary files, so for any type of information-sharing system to be popular, it had to be text-based . Otherwise, people would doze off while waiting for a page to load .
People designing Web pages also wanted their pages to be attractive . They couldn’t just format pages in a word processor, though, because every word processor handled formatting differently, and it was impossible to know which one a visitor to a site might be using . Word processing files are also much larger than plain text files .
The Web’s creators developed an elegant solution . Instead of sending the formatted pages over the Internet, they created an application—a Web browser—that could interpret plain-text code (HTML tags) as formatting instructions . The text could be sent quickly and efficiently in plain-text format, and then be processed and displayed attrac- tively and graphically on the local PC .
HTML worked great all by itself for all kinds of text formatting, but some Web designers wanted to include graphics on their pages . To accommodate this, the <img> tag was created, which designers use to refer to a graphic stored on a server . When the Web browser gets to that tag, it requests that the image file be downloaded from the server and displayed on the page . (You’ll learn how to insert images in Chapter 9, “Displaying Graphics .”)
The <img> tag is different in several ways from the <i> tag . It is one-sided, meaning it does not have a closing tag, and it takes attributes . An attribute is text within the tag that contains information about how the tag should behave . For example, for an <img> tag, you have to specify a source, abbreviated src . Here’s an example:
<img src=”tree.gif”>
This <img> tag uses the src= attribute, and specifies that the file tree.gif be displayed . Many tags accept attributes, either optional or required . You’ll see many examples throughout the exercises in this book .
With HTML, you can also create hyperlinks from one page to another . When a visitor to a Web site clicks a hyperlink, the Web browser loads the referenced page or jumps to a marked section (a “bookmark”) within the same page . You will learn to create hyperlinks in Chapter 5, “Creating Hyperlinks and Anchors .”
What Is HTML? xxi
The tag for a hyperlink is <a>, a two-sided tag, but most people wouldn’t recognize it without the attribute that specifies the file or location to which to jump . For example, to create a hyperlink with the words Click Here that jumps to the file index.htm when clicked, the coding would look like this:
<a href=”index.htm”>Click Here</a>
There’s a lot more to HTML, of course, but that’s basically how it works . Plain text is marked up with tags that indicate where elements such as formatting, hyperlinks, and graphics should be applied, and a Web browser interprets those tags and displays the page in its formatted state . The trick, of course, is to know which tags to use, and where they’re appropriate, and what attributes they need . And that’s the subject of this book .
Understanding Cascading Style Sheets
Web designers who worked with early versions of HTML to create large Web sites were often frustrated by the amount of repetition involved in their jobs . Suppose a Web site has 200 pages, all using the same basic layout and design . To make a design change to the entire site, a designer would have had to go in and manually edit each of those 200 pages .
Later versions of HTML have gotten around this by supporting cascading style sheets . Based on the same principle as style templates in a word-processing or page-layout pro- gram, Web designers use cascading style sheets to specify the formatting for a particular tag type—usually in a separate style sheet document—and then apply that style sheet to multiple pages . Need to make a change to the style? Simply make it in the style sheet, and the change is applied automatically to all pages .
Although you can still format documents by using older methods—and you’ll learn how to do a little of that in this book—most Web designers rely almost exclusively on cascad- ing style sheets for formatting these days, and XHTML all but demands that you do so . It might seem intimidating at first, but if you are creating a multi-page site, the extra trouble involved in setting up a cascading style sheet will pay for itself many times over .
Why Learn HTML in Notepad?
This book teaches beginner-level HTML coding, but it teaches it in a rather fundamental- ist way: by creating plain text files in Notepad . There are so many good Web site creation programs on the market nowadays that you may be wondering why this book takes this approach .
Simply put, it’s because doing your own coding is the best way to learn HTML . In this book you’ll build a Web site from the ground up, writing every line of code yourself . It’s slower and not as much fun as a fancy graphical program, but it’s great training .
The last chapter of this book shows how to use Microsoft Expression Web to create Web content, and you may eventually choose to move to a program like that . However, you will be a much better Web designer—and understand what is going on in design pro- grams much better—if you tough it out with Notepad in the beginning .
Choosing an HTML Version
Different versions of HTML use different tags for some types of content, although they more similar than different overall, especially at the beginner level covered in this book . Here’s a quick comparison of the HTML versions you may encounter:
●
● HTML4 A very stable, universally accepted code set, which is also fairly forgiving of small coding errors . Using HTML4 codes is desirable when compatibility with all browsers is important .
●
● XHTML A strict, standards-based implementation of HTML4 created with XML (eXtensible Markup Language) . XHTML coding uses the same codes as HTML4, so it is compatible with the same browsers as HTML4 . (See the sidebar about XML on the next page for more information .)
●
● HTML5 A revised code set that builds upon HTML4 to add new capabilities . HTML5 offers many dramatic improvements in the areas of application handling and multimedia, but a lot of those features are beyond the scope of this book . In terms of basic coding, which is what this book teaches, the biggest difference is that there are new specific codes for different types of content that were previously handled with more general codes . For example, HTML5 has <audio> and <video>
tags for inserting multimedia content, whereas HTML4 inserts all types of multimedia content via a generic <embed> tag .
Since this is a book about HTML5, it might seem like an obvious decision to do your cod- ing using HTML5 tags, but it is not quite as simple as that in real-world situations . A good Web browser should ideally support every tag and every version of HTML it can, because the various HTML version differences should be completely invisible to the Web site visitor . However, HTML5 is so new that not all browsers have caught up to it yet, and people who use older computers may not have the latest version of a browser even if an HTML5 compatible version is available .
Tip Here’s a site that lists what HTML5 features are supported by each version of each of the
What Is HTML? xxiii
The code you will create as you work through the exercises in this book is based on HTML5, but I will also show you some workarounds in situations where HTML5 codes might cause problems in some browsers . You’ll learn both ways of creating a certain effect, so that you can make the call of which codes to use in your real-life work as the situations arise .
What are XML and XHTML?
There is a language related to HTML called Extensible Markup Language (XML) that programmers use to create their own tags . It’s widely used for Web databases, for example, because it can define tags for each data field . Because XML can be so completely customized, programmers can create almost any other markup lan- guage within it, just by re-creating all the officially accepted tags of that language . The W3C did just that: they re-created the entire HTML language in XML, and called it Extensible HTML (XHTML) . Version 1 .0 was released in 2001; the current version is XHTML 2 .0, released in 2004 .
XHTML, then, is HTML written within the larger language of XML . Because it is vir- tually identical to HTML in its functionality, the basic set of tags is the same, and you can learn both HTML and XHTML at the same time . You can also use XHTML to create new tags and extensions, which is a valuable feature for advanced Web developers .
There’s just one thing about XHTML to watch out for: it’s not tolerant of mistakes . For example, in HTML, technically you are supposed to begin each paragraph with
<p> and end each paragraph with </p> . But in HTML you can leave out the closing
</p> tag if you want (or if you forget it) . That won’t work in XHTML . There are lots of little ways that XHTML is picky like that .
At one point, it was thought that XHTML would eventually replace HTML4 as its successor, but due to interoperability problems, that has not happened; instead HTML5 is poised to succeed HTML4 . This book doesn’t explicitly cover XHTML, but most of what you will learn can be applied to XHTML coding .
Why Code in HTML5?
The short answer is: you should code in HTML5 because it’s an investment in the future . Within a few years, it will be the standard on which nearly all Web sites are based .
A slightly longer answer is because it enables cleaner, easier-to-write code . Web page technology has grown by leaps and bounds, mostly due to the increase of the average person’s Internet connection speed, but also because users, designers, and programmers increasingly demand more functionality from their Web pages, such as more precise control of fonts and layout, better rendering on devices that vary wildly in size from mobile phones to huge desktop monitors, better images, more interactivity, video, audio, animations, and better support for various image and file formats . Because most people have fast connections, they don’t have to wait a long time for pages to load that contain large audio and video files, which means more and more sites are including audio and video content .
HTML was not originally designed for the rigors of multimedia content delivery, so more and more high-end professional sites have moved to other languages and technologies that piggyback on HTML to deliver that content, such as JavaScript, Java, and Active Server Pages (ASP) .
HTML5 adds some important new tags to make audio, video, and application integration smoother and more reliable . You’ll learn about many of these new tags in Chapters 15 and 16, including <audio>, <video>, and <canvas> .
HTML5 removes support for some of the older tags . For example, an old way (pre-HTML4) of specifying a font was the <font> tag . Today, most people use cascading style sheets to define fonts, so the <font> tag has not been used by many Web designers in a long time anyway . HTML5 formally removes it from the language .
One of the biggest things that HTML5 removes is the ability to create multi-framed Web sites with the <frame> and <frameset> commands . You can still create Web sites with multiple sections, but they’re handled much more capably using tables or divisions . Chapter 11 covers divisions—the newer way, preferred by most professional Web design- ers . Chapters 12 and 13 cover tables, still an acceptable way, and preferred by many casual Web page designers who are familiar with tables from programs like Word .
Minimum System Requirements
There are no minimum system requirements for developing HTML; you can do it in any text editing program with any type of computer and any operating system . That’s the beauty of HTML! This book uses Notepad as the text editor, but you can use any editor that you like .
For testing your work, you will need an HTML5-compliant Web browser application . The latest versions of Google Chrome and Firefox (both freely available online) will work fine for this, as will Internet Explorer 9 or higher .
xxv
Using the Practice Files
Each exercise in the lessons is preceded by a paragraph or paragraphs that list the files needed for that exercise and explain any file preparation you need to take care of before you start working through the exercise . The practice files are available for download from http://oreilly.com/catalog/0790145302083/ . When you unzip them from the download file, separate folders will be created for each chapter, and separate folders within each of those for each exercise .
The following table lists the practice file folders for each chapter and the subfolders you’ll find within them . The practice file folder for each chapter also includes a Solutions subfolder containing finished versions of the practice files used in that chapter .
Chapter Folder Subfolder
Chapter 1:
HTML and XHTML Basics 01Editing no subfolders
Chapter 2:
Setting Up the Document Structure 02Structure CreatingParagraphs PublishingFiles SpecifyingKeywords SpecifyingTitle Chapter 3:
Formatting Text by Using Tags 03Format ApplyingBold
ApplyingSuperscript ConfiguringSettings CreatingHeadings FormattingQuotes UsingMonospace Chapter 4:
Using Lists and Backgrounds 04Lists ChoosingColors
CreatingGlossary InsertingCharacters InsertingLines NestingLists SpecifyingImages Chapter 5:
Creating Hyperlinks and Anchors 05Links CreatingAnchors CreatingHyperlinks LinkingEmail LinkingOther
Chapter Folder Subfolder Chapter 6:
Introduction to Style Sheets 06Styles ConstructingRules
CreatingClasses CreatingExternal CreatingNested StylingHyperlinks Chapter 7:
Formatting Text by Using Style Sheets 07Text AdjustingSpacing ApplyingBold ApplyingStrike CreatingSpan SelectingFont SelectingSize Chapter 8:
Formatting Paragraphs Using Style Sheets 08Paragraphs AddingBorders AdjustingHeight Indenting SettingAlignment Chapter 9:
Displaying Graphics 09Graphics CaptioningFigures
ClearingImages CreatingHyperlinks InsertingImages SizingImages UsingAlt
UsingThumbnails Chapter 10:
Creating Navigational Aids 10Navigation CreatingGraphicBar CreatingImageMap CreatingTextBar Redirecting Chapter 11:
Creating Division-Based Layouts 11Divisions CreatingDivisions FormattingDivisions PositioningDivisions UsingSemantic Chapter 12:
Creating Tables 12Tables CreatingTable
SettingWidth SpanningCells SpecifyingSize UsingTables
xxvii
Chapter Folder Subfolder
Chapter 13:
Formatting Tables 13FmtTables ApplyingBackground
ApplyingBorders ChangingPadding Chapter 14:
Creating User Forms 14Forms CreatingButtons
CreatingForms CreatingLists Chapter 15:
Incorporating Sound and Video 15AudioVideo Chapter 16:
Including JavaScript and External Content 16Canvas Chapter 17:
HTML and Microsoft Expression Web 17Expression ViewingPage
xxviii
Getting Help
Every effort has been made to ensure the accuracy of this book . If you do run into prob- lems, please contact the sources listed in the following topics .
Getting Help with This Book
If your question or issue concerns the content of this book or its practice files, please first consult the book’s errata page, which can be accessed at:
http://oreilly.com/catalog/0790145302083/
This page provides information about known errors and corrections to the book . If you do not find your answer on the errata page, send your question or comment to O’Reilly Media Customer Service at:
xxix
Conventions and Features in This Book
You can save time when you use this book by understanding how the Step by Step series shows special instructions, keys to press, buttons to click, and so on .
Convention Meaning
SET UP These words are found at the beginning of paragraphs preceding step-by-step exercises . They point out items you should check or actions you should carry out before beginning an exercise . Use
Open
These words are found within the SET UP paragraphs that pre- cede step-by-step exercises . They draw your attention to practice files that you’ll need to use in the exercise .
CLEAN UP These words are found at the beginning of paragraphs following step-by-step exercises . They give instructions for closing open files or programs before moving on to another topic .
1 . Numbered steps guide you through hands-on exercises in each topic .
● A round bullet indicates an exercise that has only one step . Troubleshooting These paragraphs show you how to fix a common problem that
might prevent you from continuing with the exercise .
Tip These paragraphs provide a helpful hint or shortcut that makes working through a task easier .
Important These paragraphs point out information that you need to know to complete a procedure .
Note These paragraphs provide supplementary or related information . Compatibility These paragraphs explain alternate coding you can use for
greater backward compatibility
Ctrl+C A plus sign (+) between two key names means that you must hold down the first key while you press the second key . For exam- ple, “press Ctrl+C” means “hold down the Ctrl key while you press the c key .”
user interface elements In exercises, the names of program elements such as buttons, commands, and dialog boxes .
user input Anything you are supposed to type .
glossary terms Terms explained in the glossary at the end of the book .
What Next?
To get started, turn the page to Chapter 1 and start reading and working through the exercises . The lessons are designed to be tackled in the order they appear in the book, but feel free to skip around if you just need to fill in some holes in your HTML knowledge .
1
Part 1
Getting Started with HTML
1 Editing and Viewing HTML Files . . . .3 2 Setting Up the Document Structure. . . .13 3 Formatting Text by Using Tags . . . .25 4 Using Lists and Backgrounds. . . .47 5 Creating Hyperlinks and Anchors . . . .67
Download from Wow! eBook <www.wowebook.com>
Chapter at a Glance
Open a Web page in Notepad, page 2
Preview a Web page in Internet Explorer, page 7
Make, save, and view changes, page 10
3
1 Editing and Viewing HTML Files
In this chapter, you will learn how to 4 Open a Web page in Notepad .
4 Preview a Web page in Internet Explorer . 4 Make, save, and view changes .
As you work through this book’s exercises, you’ll learn HTML by creating and editing text files in Notepad, and then viewing them in a Web browser to check your work . This chapter teaches the important basic skills you need to work in these programs .
See Also Do you need only a quick refresher on the topics in this chapter? See the Key Points section at the end of this chapter.
Practice Files Before you can use the practice files provided for this chapter, you need to download and install them from the book’s companion content location. See “Using the Practice Files” at the beginning of this book for more information.
Opening a Web Page in Notepad
Notepad is included with all versions of Windows, and you’ll find it in the All Programs (or Programs)/Accessories folder on the Start menu . It’s a simple text editor that saves only in plain text format . That’s ideal for HTML editing because you don’t need to worry about any extra word processing formatting being included in the file .
Note You are welcome to use a different text editor application to complete the exercises in this book. Notepad is just a suggestion.
When saving or opening files in Notepad, the default file extension is .txt . The Save and Open dialog boxes are set by default to filter file listings so only those files with .txt exten- sions appear . That means each time you browse for a file, you need to change the file type to All Files so you can browse for Web pages (which have .htm or .html extensions) .
Note You may run into various extensions on Web page files on the Internet, such as .php, .asp, and .jsp. Those are all special formats designed for use with specific server technologies.
This book only covers developing the basic type of Web page: the type with an .htm or .html extension.
In this exercise, you will open a Web page in Notepad and examine its text and tags . SET UP Use the welcome file in the practice file folder for this topic. This practice file is located in the Documents\Microsoft Press\HTML5 SBS\01Editing folder.
1.
From the Start menu, select All Programs | Accessories | Notepad .2.
In the untitled Notepad window, select File | Open .3.
Navigate to the folder containing the practice files for this chapter .On the Places bar, click Documents (or My Documents if you are using Windows XP) . In the Open dialog box, double-click Microsoft Press, HTML5 SBS, and then
01Editing .
Note You won’t see any files in the list at this point. The only thing that you should see is just a _Solutions folder. (That folder contains the solution files for the lesson, but you don’t need those now.)
4.
Click the Files Of Type down arrow, and then click All Files .Opening a Web Page in Notepad 5
5.
In the Open dialog box, click welcome.htm, and then click Open . The welcome .htm file opens in Notepad .Note The .htm extension might not appear on the welcome file in the Open dialog box.
By default, file extensions for known file types are turned off in Windows. To turn them on, open Computer (or My Computer), and on the Tools menu (press the Alt key for the menu bar if you don’t see it), click Folder Options. On the View tab of the Folder Options dialog box, clear the Hide Extensions For Known File Types check box, and then click OK.
6.
Locate the <html> and </html> tags .These tags signify the beginning and end of the HTML code .
7.
Locate the <body> and </body> tags .These tags signify the beginning and end of the visible portion of the Web page when viewed in a browser .
8.
Locate the <p> and </p> tags .These tags signify the beginning and end of a paragraph . CLEAN UP Leave the page open in Notepad for later use.
Adding the Data File Location to the Favorites List
In the course of working through this book, you will open many files in Notepad . To save yourself the trouble of navigating to the data file folder each time (HTML5 SBS), you might want to add that folder to your Favorites bar in the Open dialog box for easy access to the data files .
In this exercise, you will add to the Favorites bar a shortcut that brings you directly to the HTML5 SBS folder .
SET UP Open Notepad.
1.
Select File | Open .2.
Navigate to the folder containing the practice files for this chapter .On the Places bar, click Documents (or My Documents if you are using Windows XP) . In the Open dialog box, double-click Microsoft Press . The HTML5 SBS folder appears as an icon .
3.
Drag the HTML5 SBS icon to the Favorites list on the left side of the window . A shortcut for it appears on the Favorites list .Drag the folder here to create a shortcut
CLEAN UP Close the Windows Explorer window
Now, the next time you want to open a file in the Open dialog box, you can double-click that shortcut, and then double-click the folder for the chapter you are working on, which is much more convenient!
Previewing a Web Page in a Web Browser 7
Opening a File from Windows Explorer
A quick way to open most file types in their default applications is to double-click them from any Windows Explorer window . However, the problem with doing that for HTML files is that the default application is your Web browser, not Notepad; thus, instead of the file opening in Notepad, it opens in your Web browser . One way to get around this is to right-click a file in Windows Explorer, choose Open With from the contextual menu, and then click Notepad . This opens Notepad and loads the file .
Previewing a Web Page in a Web Browser
Because Notepad is not a WYSIWYG (“What You See Is What You Get”) program, you won’t be able to immediately see how the tags you type will affect the finished product . To work around this, most Web page designers keep a browser window open next to Notepad .
You can preview your work in any browser; you do not need to use Internet Explorer 9 (although that’s what I use in this book’s examples) . In fact, as you progress with your Web development skills, you will probably want to acquire several different browsers to test your pages because each browser might display page elements a little differently .
For beginners, though, Internet Explorer is a good choice because it’s the most popular browser—the one your target audience is most likely to be using . Other popular brows- ers include Google Chrome, Firefox, Safari, and Opera .
Caution Versions of Internet Explorer prior to version 9 do not support some of the HTML5 features. You will probably want to test your Web pages in an earlier version to make sure that people who use them will be able to view your page. But don’t use an earlier version of Internet Explorer as you work through this book’s examples; you won’t get the full effect of the new HTML5 features.
Tip If the video card in your computer has two monitor connectors on it, or if you have an additional video card that you could install in your system, you might want to set up two monitors side-by-side. That way you could work on your HTML code in Notepad on one monitor and display the page full-screen in Internet Explorer in the other. All recent versions of Windows support at least two monitors, and some versions support even more.
In this exercise, you will display an HTML file in Internet Explorer . To see the displayed file and the underlying code at the same time, open the practice files from this exercise and the previous exercise in separate windows and arrange them so both are visible .
SET UP Use the welcome file from the previous exercise, or use the one in the practice file folder for this topic. The practice file is located in the Documents\
Microsoft Press\HTML5 SBS\01Editing folder.
1.
Select Start | Internet Explorer .Note Depending on your system and your default browser, Internet Explorer might not be pinned to the top of your Start menu. If it is not, click Start | All Programs | Internet Explorer.
2.
Select File | Open .The Open dialog box appears .
Note If the menu bar does not appear in Internet Explorer, press the Alt key to display it.
Previewing a Web Page in a Web Browser 9
3.
Click the Browse button, and then browse to Documents\Microsoft Press\HTML5 SBS\01Editing .Tip If you created the shortcut in the Favorites bar earlier in the chapter, you can use it to save a few clicks when browsing for the location.
4.
Click welcome.htm, and then click Open .The path to the file appears in the Open dialog box .
5.
Click OK .The file opens in Internet Explorer.
CLEAN UP Leave Internet Explorer open for the next exercise.
The method you just learned works especially well when you already have your Web browser open, in which case you can skip step 1. An alternate method is to browse to the storage location by using Windows Explorer, and double-click the file. Remember that you can’t just double-click a Web page to edit it; you must right-click it. By default, the double-click operation is reserved for opening the page in your Web browser. What was a hardship only a few pages ago is now a convenience!
Tip Not all Web browser software displays pages exactly the same way. For example, one browser’s idea of what text should look like might be different from another. It’s a good idea to check your pages in multiple Web browsers, such as Firefox, Chrome, and Opera. These are available as free downloads from www.firefox.com, www.google.com/chrome, and www.opera.
com, respectively.
Making, Saving, and Viewing Changes
After you’ve made a change to a Web page, you will probably want to preview the result of that change. If you set up your Internet Explorer and Notepad windows side by side in the preceding two exercises, it’s easy to view those changes. Simply save your work in Notepad, and then refresh the display in Internet Explorer.
In this exercise, you will change “Iowa” to “Indiana” in the welcome.htm file, and then preview that change in Internet Explorer. This exercise builds on the previous two, so make sure you have completed them. You can use this procedure throughout the rest of the book to preview your work from each exercise.
SET UP Be sure to have the welcome file open in Notepad and in Internet Explorer before beginning this exercise. Use the welcome file from the previous exercise, or use the one in the practice file folder for this topic. The practice file is located in the Documents\Microsoft Press\HTML5 SBS\01Editing folder.
Key Points 11
1.
In Notepad, locate the word Iowa, and change it to Indiana, as shown in bold text in the following code:<p>Welcome to the Garden Company, located in the heart of Central Indiana.
</p>
2.
Save your work (File | Save) .3.
On the Internet Explorer toolbar, click the Refresh button .Notice that the Web page shown in Internet Explorer now reads “Indiana,” too .
CLEAN UP Close the welcome file, and then exit Notepad and Internet Explorer.
Key Points
●
● Any plain text editor, including Notepad, can be an HTML editor .
●
● Most Web pages have an .htm or .html extension . You can open them in Notepad, but first you need to change the Files Of Type setting in the Open dialog box to All Files . You must change this setting each time you use the Open dialog box .
●
● An alternative way to open a Web page in Notepad is to right-click it in Windows Explorer, select Open With from the contextual menu, and then click Notepad .
●
● To preview a page in a Web browser, select File | Open from the browser’s menu .
●
● You can double-click an .htm or .html file in Windows Explorer to open it automati- cally in your default Web browser .
●
● To see changes you make in Notepad reflected in your Web browser, save your work in Notepad, and then click Refresh in the browser window .
Chapter at a Glance
Create the HTML, head, and body sections, page 15
Create paragraph and line breaks, page 17
Specify a page title, page 19
13
2 Setting Up the
Document Structure
In this chapter, you will learn how to 4 Specify the document type .
4 Create the HTML, Head, and Body sections . 4 Create paragraphs and line breaks .
4 Specify a page title and keywords . 4 Publish a file to a server .
Every society needs an infrastructure with certain rules that everyone agrees to for the general public good . For example, we have all agreed that a red light means “stop” and a green light means “go .” Everyone who wants to participate in the transportation system must play by those rules, or chaos ensues .
HTML is the same way . You can get creative with your Web content, but there must be an underlying structure in place for Web browsers to read and render your Web pages properly . That means the document must contain certain tags that delineate its major sections and indicate to the browser what type of coding the document uses .
In this chapter, you’ll learn how to structure a document with the correct underlying tags . You’ll learn how to specify the type of HTML you are writing and how to create Head and Body sections . You’ll also learn how to create paragraph and line breaks, specify a page title, enter hidden keywords by which your page can be found in search engines, and publish a test page to a Web server .
See Also Do you need only a quick refresher on the topics in this chapter? See the Key Points section at the end of this chapter.
Practice Files Before you can use the practice files provided for this chapter, you need to download and install them from the book’s companion content location. See “Using the Practice Files” at the beginning of this book for more information.
Download from Wow! eBook <www.wowebook.com>
Specifying the Document Type
When creating an HTML5 document, the first line of the document should be this tag:
<!DOCTYPE html>
The DOCTYPE tag always begins with an exclamation point and is always placed at the beginning of the document, before any other tag . Most HTML tags are not case-sensitive, but the word DOCTYPE should always be uppercase .
Using the DOCTYPE tag is like signing a contract . It is an optional tag, but when you use it, you are promising that your coding will conform to certain standards . When a Web browser encounters a DOCTYPE tag, it processes the page in standards mode . When it doesn’t encounter the DOCTYPE tag, it assumes that there is something quirky about the page, and processes the page in quirks mode . When the browser sees the tag <!DOC- TYPE html>, it assumes you are using HTML5 .
The distinction between standards mode and quirks mode came about in earlier days, when there were problems with standardization between Web browsers . In some brows- ers, to display pages properly, you needed to get a little creative with the HTML code . Modern HTML coding does not allow that, but some older pages still include these obsolete workarounds . By using the DOCTYPE tag, you are making a promise to the Web browser that there is nothing but pure HTML code in the page .
Earlier versions of HTML used more complex DOCTYPE tags . If you’re using HTML Ver- sion 4 .01, the syntax for the tag is:
<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
If you’re using XHTML, the syntax for the tag is:
<!DOCTYPE HTML PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Note If you are writing XHTML code, the DOCTYPE tag is required.
Creating the HTML, Head, and Body Sections 15
Creating the HTML, Head, and Body Sections
All of your HTML coding—except the DOCTYPE tag—should be placed within the two- sided <html> tag . Recall from the Introduction that when a tag is two-sided, it requires a corresponding closing tag that is identical to the opening tag but contains a slash:
</html> . The tags <html> and </html> serve as a “wrapper” around all the other tags in the document .
In addition, your document should have two sections: a Head and a Body . The Head sec- tion is defined by the two-sided tag <head> . The Head section contains the page title, which is the text that will appear in the title bar of the Web browser and on the Microsoft Windows taskbar button . It also includes information about the document that is not dis- played, such as its <meta> tags (which you’ll learn about on page 19) . You can also include lines of code that run scripts, like Javascript .
The Body section is defined by the two-sided tag <body>, and it contains all the infor- mation that appears in the Web browser when you view the page .
Note The <html>, <head>, and <body> tags are all optional in HTML—but you should still use them because it’s a good design practice. They are required in XHTML. In addition, in XHTML you must add an argument to the <html> tag that declares its XML namespace, a reference to the fact that XHTML is created within XML (as you learned in Chapter 1, “Editing and Viewing HTML Files”). Here’s how the opening <html> tag should look in an XHTML document: <html xlmns=”http://www.w3.org/1999/xhtml”>.
In this exercise, you will create an HTML5 template file that you can reuse later for your own work .
SET UP Start Microsoft Notepad before beginning this exercise.
1.
In Notepad, open the Format menu . Word Wrap should have a check mark next to it . If it does not, click it to enable the Word Wrap feature .Tip Using Word Wrap makes it easier to see long lines of HTML coding without scrolling.
2.
In the Notepad window, type the following:<!DOCTYPE html>
3.
Press Enter, and then type:<html>
<head>
4.
Press Enter two or three times to add some blank lines, and then type:</head>
<body>
5.
Press Enter two or three times to add some blank lines, and then type:</body>
</html>
6.
Save the file as HTML5.htm on your Windows desktop (or to any other location that is convenient for you) .Note Most of the files you work with in this book will be stored elsewhere, but you might find it helpful to keep the templates created in this exercise handy for reuse. The desktop is a convenient place to store them, or you can store them anywhere you like.
CLEAN UP Close the Notepad window.
You now have a template for creating any HTML documents you like . You can reopen this template file and save it under different names, which will save time re-creating these basic tags .
Tip If you want to avoid accidentally editing the template in the future, make it read-only. To do so, in Windows Explorer, right-click the file, and then select Properties from the contextual menu. In the Properties dialog box, select the Read-Only check box. When you try to save changes to a read-only file, an error message appears and a Save As dialog box prompts you to save a copy of it with a new name.
Creating Paragraphs and Line Breaks 17
Creating Paragraphs and Line Breaks
Within the <body> section of the document, you type the text that will appear on the Web page . Each paragraph of text should be enclosed in a two-sided tag that indicates its type .
The most basic paragraph type is the body paragraph, indicated by the <p> tag . It is a two-sided tag, so the paragraph text is placed between a <p> and a </p> .
Note In HTML, the code will still work even if the </p> is omitted; in XHTML, it won’t. However, even if you never plan on coding in XHTML, it is a good practice to include the </p> tag. This way, you won’t fall into any sloppy habits.
When a browser displays a Web page, it inserts vertical white space between paragraphs:
That spacing is usually convenient, but it can be a problem when the extra space between lines is unwanted, such as with an address .
To create a line break without officially starting a new paragraph (and thereby adding that extra white space), use the <br> tag . This is a one-sided tag placed within a para- graph, at the end of each line, like this:
<p>David Jaffe<br>
317-555-8882</p>
Note In XHTML, the line break tag is <br />. The end slash (and the space) is necessary to indicate that it’s a self-closing tag. Notice that the slash is placed after the letters, not before, as with the closing end of a two-sided tag. In XHTML, one-sided tags must end with a slash to indicate that they are self-closing. The space between the text and the final slash is also required so the tag will be recognized in HTML.
In this exercise, you will add text to an HTML file template, and then preview it in Micro- soft Internet Explorer .
SET UP Use the HTML5.htm file from the previous exercise or in the practice file folder for this topic. This practice file is located in the Documents\Microsoft Press\
HTML5 SBS\02Structure\CreatingParagraphs folder. Open the HTML5 file in Notepad.
1.
Save the HTML5 file in the Documents\Microsoft Press\HTML5 SBS folder as index.htm .Note It is customary to name the opening page of a Web site index.htm, index.html, default.htm, or default.html. When users type a URL in their Web browsers but omit the file name (for example, typing www.microsoft.com rather than www.microsoft.com/
filename.htm), most servers will automatically respond with the index or default page if one exists.
2.
Open the index file in Internet Explorer and arrange the Notepad and Internet Explorer windows so that both are visible .The index file displayed in Internet Explorer is currently blank .
3.
In the Notepad window, type the following between the <body> and </body> tags:<p>Welcome to The Garden Company! We hope you will find our site a useful resource for becoming a better gardener.<p>
<p>Our main store is located at:<br>
108 Ponting Street<br>
Macon, IN 46062</p>
<p>Please stop by and browse our extensive inventory from Monday through Saturday, 7:00 a.m. to 8:00 p.m.</p>
4.
Save your work, and then press F5 or click the Refresh button at the right side of the Address bar to refresh the display in Internet Explorer to see the result of the changes . Leave both windows open for the next exercise .Specifying a Page Title and Metatags 19
Note Your screen might look slightly different, depending on the settings you have configured in your browser.
CLEAN UP Close the Notepad and Internet Explorer windows.
Specifying a Page Title and Metatags
Perhaps you noticed in the preceding exercise that the complete path to the file appeared in the title bar of Internet Explorer . Usually when you view a Web page, a friendly, descriptive title appears in that spot instead . That text is specified by a <title>
tag that is placed in the <head> section (also called the header) . Here’s an example:
<head>
<title>The Garden Company</title>
</head>
Troubleshooting Make sure you place the <title> tag in the <head> section, and not in the
<body> section.
Another element you can place in the header is the <meta> tag . The <meta> tag has several purposes . One of these is to identify keywords related to your page . Placing appropriate keywords on your page can make it easier for people to find your page when they are searching the Web using a search engine such as MSN . When some search engines index your page, they rely not only on the full text of the page, but also on any keywords they find in the <meta> tag area .