100% found this document useful (1 vote)
34 views

Download full HTML CSS and JavaScript All in One Covering HTML5 CSS3 and ES6 Sams Teach Yourself 3rd Edition Meloni ebook all chapters

The document promotes the ebook 'HTML, CSS, and JavaScript All in One, Third Edition' by Meloni, available for download on ebookmeta.com. It includes links to other recommended digital products and outlines the contents of the ebook, covering web design fundamentals, advanced techniques, and dynamic site functionality. The ebook aims to provide a comprehensive learning experience for readers interested in web development.

Uploaded by

tortugavitjf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views

Download full HTML CSS and JavaScript All in One Covering HTML5 CSS3 and ES6 Sams Teach Yourself 3rd Edition Meloni ebook all chapters

The document promotes the ebook 'HTML, CSS, and JavaScript All in One, Third Edition' by Meloni, available for download on ebookmeta.com. It includes links to other recommended digital products and outlines the contents of the ebook, covering web design fundamentals, advanced techniques, and dynamic site functionality. The ebook aims to provide a comprehensive learning experience for readers interested in web development.

Uploaded by

tortugavitjf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get the full ebook with Bonus Features for a Better Reading Experience on ebookmeta.

com

HTML CSS and JavaScript All in One Covering HTML5


CSS3 and ES6 Sams Teach Yourself 3rd Edition
Meloni

https://ebookmeta.com/product/html-css-and-javascript-all-
in-one-covering-html5-css3-and-es6-sams-teach-yourself-3rd-
edition-meloni/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Sams Teach Yourself C in One Hour a Day 9th Edition


Siddhartha Rao

https://ebookmeta.com/product/sams-teach-yourself-c-in-one-hour-a-
day-9th-edition-siddhartha-rao/

ebookmeta.com

Teach Yourself VISUALLY HTML and CSS The Fast and Easy Way
to Learn 2nd Edition Guy Hart-Davis

https://ebookmeta.com/product/teach-yourself-visually-html-and-css-
the-fast-and-easy-way-to-learn-2nd-edition-guy-hart-davis/

ebookmeta.com

Sams Teach Yourself SAP in 24 Hours Fourth Edition George


W Anderson

https://ebookmeta.com/product/sams-teach-yourself-sap-in-24-hours-
fourth-edition-george-w-anderson/

ebookmeta.com

Snowflakes and Holidates 02 Riley Ash My Hero Holidate 1st


Edition Riley Ash

https://ebookmeta.com/product/snowflakes-and-holidates-02-riley-ash-
my-hero-holidate-1st-edition-riley-ash/

ebookmeta.com
Wardlaw's Contemporary Nutrition: A Functional Approach
6th Edition Anne M. Smith

https://ebookmeta.com/product/wardlaws-contemporary-nutrition-a-
functional-approach-6th-edition-anne-m-smith/

ebookmeta.com

Black Mental Health Patients Providers and Systems 1st


Edition Ezra E. H. Griffith

https://ebookmeta.com/product/black-mental-health-patients-providers-
and-systems-1st-edition-ezra-e-h-griffith/

ebookmeta.com

Eye Yield Ophthalmology Basics for Board and FRCS Part 1


Exams Saif Aldeen Saleh Alryalat

https://ebookmeta.com/product/eye-yield-ophthalmology-basics-for-
board-and-frcs-part-1-exams-saif-aldeen-saleh-alryalat/

ebookmeta.com

Art Subjects Making Artists in the American University


Howard Singerman

https://ebookmeta.com/product/art-subjects-making-artists-in-the-
american-university-howard-singerman/

ebookmeta.com

Self-Care All-in-One For Dummies Shamash Alidina

https://ebookmeta.com/product/self-care-all-in-one-for-dummies-
shamash-alidina/

ebookmeta.com
Fokker Aircraft of WWI Volume 5 1918 Design Part 1
Prototypes D VI 1st Edition Jack Herris

https://ebookmeta.com/product/fokker-aircraft-of-wwi-
volume-5-1918-design-part-1-prototypes-d-vi-1st-edition-jack-herris/

ebookmeta.com
Jennifer Kyrnin
Julie Meloni

SamsTeach Yourself

HTML, CSS,
and JavaScript
Third Edition

All
One
in
Sams Teach Yourself HTML, CSS, and JavaScript All in One, Third Edition Editor-in-Chief
Copyright © 2019 by Pearson Education, Inc. Mark Taub
All rights reserved. This publication is protected by copyright, and permission must be obtained
Editor
from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmis-
sion in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. Mark Taber
For information regarding permissions, request forms, and the appropriate contacts within the Managing Editor
Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/
permissions/. No patent liability is assumed with respect to the use of the information contained Sandra Schroeder
herein. Although every precaution has been taken in the preparation of this book, the publisher Senior Project
and authors assume no responsibility for errors or omissions. Nor is any liability assumed for Editor
damages resulting from the use of the information contained herein.
Lori Lyons
ISBN-13: 978-0-672-33808-3
ISBN-10: 0-672-33808-4 Copy Editor
Library of Congress Control Number: 2018953965 Kitty Wilson
01 18 Project Manager
Trademarks Suganya Karuppasamy
All terms mentioned in this book that are known to be trademarks or service marks have Indexer
been appropriately capitalized. Pearson cannot attest to the accuracy of this information.
Use of a term in this book should not be regarded as affecting the validity of any trademark Ken Johnson
or service mark. Proofreader
Warning and Disclaimer Abigail Manheim
Every effort has been made to make this book as complete and as accurate as possible, but no
Technical Editor
warranty or fitness is implied. The information provided is on an “as is” basis. The author and the
publisher shall have neither liability nor responsibility to any person or entity with respect to any Julie Meloni
loss or damages arising from the information contained in this book or from the use of the CD or Editorial Assistant
programs accompanying it.
Cindy Teeters
Special Sales
For information about buying this title in bulk quantities, or for special sales opportunities (which Cover Designer
may include electronic versions; custom cover designs; and content particular to your business, Chuti Prasertsith
training goals, marketing focus, or branding interests), please contact our corporate sales
Compositor
department at corpsales@pearsoned.com or (800) 382-3419.
codemantra
For government sales inquiries, please contact governmentsales@pearsoned.com.
For questions about sales outside the U.S., please contact intlcs@pearson.com.
Contents at a Glance

Part I: Getting Started on the Web


LESSON 1 Understanding How the Web Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Structuring an HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 Understanding Cascading Style Sheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4 Understanding JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5 Validating and Debugging Your Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Part II: Building Blocks of Practical Web Design


LESSON 6 Working with Fonts, Text Blocks, Lists, and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7 Using External and Internal Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
8 Working with Colors, Images, and Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Part III: Advanced Web Page Design with CSS


LESSON 9 Working with Margins, Padding, Alignment, and Floating . . . . . . . . . . . . . . . 249
10 Understanding the CSS Box Model and Positioning . . . . . . . . . . . . . . . . . . . . . . . . . 271
11 Using CSS to Do More with Lists, Text, and Navigation . . . . . . . . . . . . . . . . . . . . 289
12 Creating Layouts Using Modern CSS Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
13 Taking Control of Backgrounds and Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
14 Using CSS Transformations and Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
15 Animating with CSS and the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Part IV: Responsive Web Design


LESSON 16 Understanding the Importance of Responsive Web Design . . . . . . . . . . . . . . . 427
17 Designing for Mobile Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
18 Using Media Queries and Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
iv Sams Teach Yourself HTML, CSS, and JavaScript All in One

Part V: Getting Started with Dynamic Sites


LESSON 19 Understanding Dynamic Websites and HTML5 Applications. . . . . . . . . . . . . 487
20 Getting Started with JavaScript Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
21 Working with the Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . . . . . . 523
22 Using JavaScript Variables, Strings, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
23 Controlling Flow with Conditions and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
24 Responding to Events and Using Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
25 JavaScript Best Practices .............................................................. 655
26 Using Third-Party JavaScript Libraries and Frameworks . . . . . . . . . . . . . . . . . . . 681

Part VI: Advanced Website Functionality and Management


LESSON 27 Working with Web-Based Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
28 Organizing and Managing a Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Table of Contents

Part I: Getting Started on the Web

LESSON 1: Understanding How the Web Works 1


A Brief History of HTML and the World Wide Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Creating Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Understanding Web Content Delivery ......................................................... 3
Selecting a Web Hosting Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Testing with Multiple Web Browsers and Devices .......................................... 8
Creating a Sample File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Using FTP to Transfer Files ....................................................................... 10
Understanding Where to Place Files on the Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Distributing Content Without a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Tips for Testing Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Q&A ..................................................................................................... 20
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

LESSON 2: Structuring an HTML Document 23


Getting Prepared . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Getting Started with a Simple Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
HTML Tags Every Web Page Must Have. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Organizing a Page with Paragraphs and Line Breaks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Organizing Your Content with Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Understanding Semantic Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Using <header> in Multiple Ways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Understanding the <section> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Using <article> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
vi Sams Teach Yourself HTML, CSS, and JavaScript All in One

Implementing the <nav> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45


When to Use <aside> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Using <footer> Effectively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Q&A ..................................................................................................... 50
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

LESSON 3: Understanding Cascading Style Sheets 55


How CSS Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
A Basic Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
A CSS Style Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Using Style Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Using Style IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Internal Style Sheets and Inline Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Q&A ..................................................................................................... 75
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

LESSON 4: Understanding JavaScript 79


Learning Web Scripting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
How JavaScript Fits into a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Exploring JavaScript’s Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Displaying Time with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Testing the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Q&A ..................................................................................................... 93
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

LESSON 5: Validating and Debugging Your Code 97


Validating Your Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Debugging HTML and CSS Using Developer Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Debugging JavaScript Using Developer Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Table of Contents vii

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Q&A ................................................................................................... 118
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

Part II: Building Blocks of Practical Web Design

LESSON 6: Working with Fonts, Text Blocks, Lists, and Tables 121
Working with Special Characters ............................................................ 122
Boldface, Italic, and Special Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Tweaking the Font. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Using Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Aligning Text on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
The Three Types of HTML Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Placing Lists Within Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Creating a Simple Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Controlling Table Sizes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Alignment and Spanning Within Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Page Layout with Tables ........................................................................ 157
Using CSS Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Q&A ................................................................................................... 164
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

LESSON 7: Using External and Internal Links 167


Using Web Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Linking Within a Page Using Anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Linking Between Your Own Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Linking to Non-HTML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Linking to External Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Linking to an Email Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Opening a Link in a New Browser Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Giving Titles to Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
viii Sams Teach Yourself HTML, CSS, and JavaScript All in One

Using CSS to Style Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182


Using Links Effectively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Q&A ................................................................................................... 188
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

LESSON 8: Working with Colors, Images, and Multimedia 191


Best Practices for Choosing Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Understanding Web Colors .................................................................... 194
Using Hexadecimal Values for Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Using RGB and RGBa Values for Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Using CSS to Set Background, Text, and Border Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Choosing Graphics Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
The Least You Need to Know About Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Preparing Photographic Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Creating Banners and Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Optimizing Images by Reducing or Removing Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Creating Tiled Background Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Placing Images on a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Describing Images with Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Specifying Image Height and Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Aligning Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Turning Images into Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Using Background Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Using Image Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Linking to Multimedia Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Embedding Multimedia Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Additional Tips for Using Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Q&A ................................................................................................... 245
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Table of Contents ix

Part III: Advanced Web Page Design with CSS

LESSON 9: Working with Margins, Padding, Alignment, and Floating 249


Using Margins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Padding Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Keeping Everything Aligned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Centering Blocks of Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Understanding the float Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Q&A ................................................................................................... 267
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

LESSON 10: Understanding the CSS Box Model and Positioning 271
The CSS Box Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Changing the Box Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Whole Scoop on Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Controlling the Way Things Stack Up ...................................................... 281
Managing the Flow of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Q&A ................................................................................................... 285
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

LESSON 11: Using CSS to Do More with Lists, Text, and Navigation 289
HTML List Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
How the CSS Box Model Affects Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Placing List Item Indicators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Creating Image Maps with List Items and CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
How Navigation Lists Differ from Regular Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Creating Vertical Navigation with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Creating Horizontal Navigation with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Q&A ................................................................................................... 314
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
x Sams Teach Yourself HTML, CSS, and JavaScript All in One

LESSON 12: Creating Layouts Using Modern CSS Techniques 317


Getting Ready to Do Page Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The Importance of Putting Mobile Devices First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Understanding Fixed Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Understanding Liquid Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Creating a Fixed/Liquid Hybrid Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Using Modern CSS Layout Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Q&A ................................................................................................... 350
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

LESSON 13: Taking Control of Backgrounds and Borders 353


Reviewing Background and Border Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Using Multiple Borders and Backgrounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Using Forgotten Background Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Using Gradients as Backgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Rounding the Corners of HTML Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Using Images as Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Understanding CSS Outlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Q&A ................................................................................................... 379
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

LESSON 14: Using CSS Transformations and Transitions 383


Understanding CSS 2D Transformations .................................................. 383
Transforming Elements in Three Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Working with CSS Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Using JavaScript to Trigger Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Q&A ................................................................................................... 399
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Table of Contents xi

LESSON 15: Animating with CSS and the Canvas 401


Understanding CSS Animation ............................................................... 401
Using the CSS Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Choosing Between CSS Animation and Canvas Animation . . . . . . . . . . . . . . . . . . . . . . . . . 423
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Q&A ................................................................................................... 424
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

Part IV: Responsive Web Design

LESSON 16: Understanding the Importance of Responsive Web Design 427


What Is Responsive Web Design? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
What Is Progressive Enhancement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Writing HTML for Responsive Web Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Validating HTML, CSS, and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Q&A ................................................................................................... 440
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442

LESSON 17: Designing for Mobile Devices 443


Designing for Mobile Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Understanding Mobile First Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Using Responsive Tables and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Creating Responsive Layouts Without Media Queries ................................ 464
Alternatives for Mobile Design Besides RWD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Q&A ................................................................................................... 469
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

LESSON 18: Using Media Queries and Breakpoints 471


What Is a Media Query? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Using Media Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
What Is a Breakpoint? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
xii Sams Teach Yourself HTML, CSS, and JavaScript All in One

How to Define Breakpoints in Your CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477


Optimal Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Q&A ................................................................................................... 484
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

Part V: Getting Started with Dynamic Sites

LESSON 19: Understanding Dynamic Websites and HTML5 Applications 487


Understanding the Different Types of Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Including JavaScript in HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Displaying Random Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Understanding the Document Object Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Changing Images Based on User Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Thinking Ahead to Developing HTML5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Q&A ................................................................................................... 502
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506

LESSON 20: Getting Started with JavaScript Programming 507


Basic Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
JavaScript Syntax Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Using Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Best Practices for JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Understanding JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Q&A ................................................................................................... 518
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522

LESSON 21: Working with the Document Object Model (DOM) 523
Understanding the Document Object Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Using window Objects ........................................................................... 524
Working with the document Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Table of Contents xiii

Accessing Browser History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528


Working with the location Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
More About the DOM Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Working with DOM Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Creating Positionable Elements (Layers) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Hiding and Showing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Modifying Text in a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Adding Text to a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Q&A ................................................................................................... 547
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

LESSON 22: Using JavaScript Variables, Strings, and Arrays 551


Using Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Understanding Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
Data Types in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Converting Between Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Using String Objects ........................................................................... 558
Working with Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Using Numeric Arrays ........................................................................... 564
Using String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Sorting a Numeric Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Introducing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Using Objects to Simplify Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Extending Built-in Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Using the Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Working with Math Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Working with Dates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Q&A ................................................................................................... 590
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
xiv Sams Teach Yourself HTML, CSS, and JavaScript All in One

LESSON 23: Controlling Flow with Conditions and Loops 595


The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Using Shorthand Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Testing Multiple Conditions with if and else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Using Multiple Conditions with switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Using for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Using while Loops ............................................................................... 606
Using do...while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Working with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Looping Through Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Q&A ................................................................................................... 612
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615

LESSON 24: Responding to Events and Using Windows 617


Understanding Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Using Mouse Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Using Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Using the load and unload Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Using click to Change the Appearance of a <div> ................................. 631
Controlling Windows with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Moving and Resizing Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Using Timeouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Displaying Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Q&A ................................................................................................... 650
Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654

LESSON 25: JavaScript Best Practices 655


Scripting Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Reading Browser Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Cross-Browser Scripting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Discovering Diverse Content Through
Random Scribd Documents
"No," said Rabbit. "Don't. Run. Come on." And they all hurried off,
Rabbit leading the way.
"Now," said Rabbit, after they had gone a little way, "we can talk.
What were you going to say, Pooh?"
"Nothing much. Why are we going along here?"
"Because it's the way home."
"Oh!" said Pooh.
"I think it's more to the right," said Piglet nervously. "What do you
think, Pooh?"
Pooh looked at his two paws. He knew that one of them was the right,
and he knew that when you had decided which one of them was the
right, then the other one was the left, but he never could remember
how to begin.
"Well," he said slowly——
"Come on," said Rabbit. "I know it's this way."
They went on. Ten minutes later they stopped again.
"It's very silly," said Rabbit, "but just for the moment I——Ah, of
course. Come on...."
"Here we are," said Rabbit ten minutes later. "No, we're not...."
"Now," said Rabbit ten minutes later, "I think we ought to be getting—
or are we a little bit more to the right than I thought?..."
"It's a funny thing," said Rabbit ten minutes later, "how everything
looks the same in a mist. Have you noticed it, Pooh?"
Pooh said that he had.
"Lucky we know the Forest so well, or we might get lost," said Rabbit
half an hour later, and he gave the careless laugh which you give
when you know the Forest so well that you can't get lost.
Piglet sidled up to Pooh from behind.
"Pooh!" he whispered.
"Yes, Piglet?"
"Nothing," said Piglet, taking Pooh's paw. "I just wanted to be sure of
you."
When Tigger had finished waiting for the others to catch him up, and
they hadn't, and when he had got tired of having nobody to say, "I
say, come on" to, he thought he would go home. So he trotted back;
and the first thing Kanga said when she saw him was "There's a good
Tigger. You're just in time for your Strengthening Medicine," and she
poured it out for him. Roo said proudly, "I've had mine," and Tigger
swallowed his and said, "So have I," and then he and Roo pushed
each other about in a friendly way, and Tigger accidentally knocked
over one or two chairs by accident, and Roo accidentally knocked
over one on purpose, and Kanga said, "Now then, run along."
"Where shall we run along to?" asked Roo.
"You can go and collect some fir-cones for me," said Kanga, giving
them a basket.

So they went to the Six Pine Trees, and threw fir-cones at each other
until they had forgotten what they came for, and they left the basket
under the trees and went back to dinner. And it was just as they were
finishing dinner that Christopher Robin put his head in at the door.
"Where's Pooh?" he asked.
"Tigger dear, where's Pooh?" said Kanga. Tigger explained what had
happened at the same time that Roo was explaining about his Biscuit
Cough and Kanga was telling them not both to talk at once, so it was
some time before Christopher Robin guessed that Pooh and Piglet
and Rabbit were all lost in the mist on the top of the Forest.
"It's a funny thing about Tiggers," whispered Tigger to Roo, "how
Tiggers never get lost."
"Why don't they, Tigger?"
"They just don't," explained Tigger. "That's how it is."
"Well," said Christopher Robin, "we shall have to go and find them,
that's all. Come on, Tigger."
"I shall have to go and find them," explained Tigger to Roo.
"May I find them too?" asked Roo eagerly.
"I think not today, dear," said Kanga. "Another day."
"Well, if they're lost tomorrow, may I find them?"
"We'll see," said Kanga, and Roo, who knew what that meant, went
into a corner, and practised jumping out at himself, partly because he
wanted to practise this, and partly because he didn't want Christopher
Robin and Tigger to think that he minded when they went off without
him.

"The fact is," said Rabbit, "we've missed our way somehow."
They were having a rest in a small sand-pit on the top of the Forest.
Pooh was getting rather tired of that sand-pit, and suspected it of
following them about, because whichever direction they started in,
they always ended up at it, and each time, as it came through the
mist at them, Rabbit said triumphantly, "Now I know where we are!"
and Pooh said sadly, "So do I," and Piglet said nothing. He had tried
to think of something to say, but the only thing he could think of was,
"Help, help!" and it seemed silly to say that, when he had Pooh and
Rabbit with him.
"Well," said Rabbit, after a long silence in which nobody thanked him
for the nice walk they were having, "we'd better get on, I suppose.
Which way shall we try?"
"How would it be," said Pooh slowly, "if, as soon as we're out of sight
of this Pit, we try to find it again?"
"What's the good of that?" said Rabbit.
"Well," said Pooh, "we keep looking for Home and not finding it, so I
thought that if we looked for this Pit, we'd be sure not to find it, which
would be a Good Thing, because then we might find something that
we weren't looking for, which might be just what we were looking for,
really."
"I don't see much sense in that," said Rabbit.
"No," said Pooh humbly, "there isn't. But there was going to be when I
began it. It's just that something happened to it on the way."
"If I walked away from this Pit, and then walked back to it, of course I
should find it."
"Well, I thought perhaps you wouldn't," said Pooh. "I just thought."
"Try," said Piglet suddenly. "We'll wait here for you."
Rabbit gave a laugh to show how silly Piglet was, and walked into the
mist. After he had gone a hundred yards, he turned and walked back
again ... and after Pooh and Piglet had waited twenty minutes for him,
Pooh got up.
"I just thought," said Pooh. "Now then, Piglet, let's go home."
"But, Pooh," cried Piglet, all excited, "do you know the way?"
"No," said Pooh. "But there are twelve pots of honey in my cupboard,
and they've been calling to me for hours. I couldn't hear them
properly before, because Rabbit would talk, but if nobody says
anything except those twelve pots, I think, Piglet, I shall know where
they're calling from. Come on."
They walked off together; and for a long time Piglet said nothing, so
as not to interrupt the pots; and then suddenly he made a squeaky
noise ... and an oo-noise ... because now he began to know where he
was; but he still didn't dare to say so out loud, in case he wasn't. And
just when he was getting so sure of himself that it didn't matter
whether the pots went on calling or not, there was a shout from in
front of them, and out of the mist came Christopher Robin.
"Oh, there you are," said Christopher Robin carelessly, trying to
pretend that he hadn't been Anxious.
"Here we are," said Pooh.
"Where's Rabbit?"
"I don't know," said Pooh.
"Oh—well, I expect Tigger will find him. He's sort of looking for you
all."
"Well," said Pooh, "I've got to go home for something, and so has
Piglet, because we haven't had it yet, and——"
"I'll come and watch you," said Christopher Robin.
So he went home with Pooh, and watched him for quite a long time ...
and all the time he was watching, Tigger was tearing round the Forest
making loud yapping noises for Rabbit. And at last a very Small and
Sorry Rabbit heard him. And the Small and Sorry Rabbit rushed
through the mist at the noise, and it suddenly turned into Tigger; a
Friendly Tigger, a Grand Tigger, a Large and Helpful Tigger, a Tigger
who bounced, if he bounced at all, in just the beautiful way a Tigger
ought to bounce.
"Oh, Tigger, I am glad to see you," cried Rabbit.
CHAPTER VIII
IN WHICH Piglet Does a Very Grand Thing
Half way between Pooh's house and Piglet's house was a Thoughtful
Spot where they met sometimes when they had decided to go and
see each other, and as it was warm and out of the wind they would sit
down there for a little and wonder what they would do now that they
had seen each other. One day when they had decided not to do
anything, Pooh made up a verse about it, so that everybody should
know what the place was for.

This warm and sunny Spot


Belongs to Pooh.
And here he wonders what
He's going to do.
Oh, bother, I forgot—
It's Piglet's too.
Now one autumn morning when the wind had blown all the leaves off
the trees in the night, and was trying to blow the branches off, Pooh
and Piglet were sitting in the Thoughtful Spot and wondering.
"What I think," said Pooh, "is I think we'll go to Pooh Corner and see
Eeyore, because perhaps his house has been blown down, and
perhaps he'd like us to build it again."
"What I think," said Piglet, "is I think we'll go and see Christopher
Robin, only he won't be there, so we can't."
"Let's go and see everybody," said Pooh. "Because when you've
been walking in the wind for miles, and you suddenly go into
somebody's house, and he says, 'Hallo, Pooh, you're just in time for a
little smackerel of something,' and you are, then it's what I call a
Friendly Day."
Piglet thought that they ought to have a Reason for going to see
everybody, like Looking for Small or Organizing an Expotition, if Pooh
could think of something.
Pooh could.
"We'll go because it's Thursday," he said, "and we'll go to wish
everybody a Very Happy Thursday. Come on, Piglet."
They got up; and when Piglet had sat down again, because he didn't
know the wind was so strong, and had been helped up by Pooh, they
started off. They went to Pooh's house first, and luckily Pooh was at
home just as they got there, so he asked them in, and they had
some, and then they went on to Kanga's house, holding on to each
other, and shouting "Isn't it?" and "What?" and "I can't hear." By the
time they got to Kanga's house they were so buffeted that they stayed
to lunch. Just at first it seemed rather cold outside afterwards, so they
pushed on to Rabbit's as quickly as they could.
"We've come to wish you a Very Happy Thursday," said Pooh, when
he had gone in and out once or twice just to make sure that he could
get out again.
"Why, what's going to happen on Thursday?" asked Rabbit, and when
Pooh had explained, and Rabbit, whose life was made up of
Important Things, said, "Oh, I thought you'd really come about
something," they sat down for a little ... and by-and-by Pooh and
Piglet went on again. The wind was behind them now, so they didn't
have to shout.
"Rabbit's clever," said Pooh thoughtfully.
"Yes," said Piglet, "Rabbit's clever."
"And he has Brain."
"Yes," said Piglet, "Rabbit has Brain."
There was a long silence.
"I suppose," said Pooh, "that that's why he never understands
anything."
Christopher Robin was at home by this time, because it was the
afternoon, and he was so glad to see them that they stayed there
until very nearly tea-time, and then they had a Very Nearly tea, which
is one you forget about afterwards, and hurried on to Pooh Corner, so
as to see Eeyore before it was too late to have a Proper Tea with
Owl.
"Hallo, Eeyore," they called out cheerfully.
"Ah!" said Eeyore. "Lost your way?"
"We just came to see you," said Piglet. "And to see how your house
was. Look, Pooh, it's still standing!"
"I know," said Eeyore. "Very odd. Somebody ought to have come
down and pushed it over."
"We wondered whether the wind would blow it down," said Pooh.
"Ah, that's why nobody's bothered, I suppose. I thought perhaps
they'd forgotten."
"Well, we're very glad to see you, Eeyore, and now we're going on to
see Owl."
"That's right. You'll like Owl. He flew past a day or two ago and
noticed me. He didn't actually say anything, mind you, but he knew it
was me. Very friendly of him, I thought. Encouraging."
Pooh and Piglet shuffled about a little and said, "Well, good-bye,
Eeyore" as lingeringly as they could, but they had a long way to go,
and wanted to be getting on.
"Good-bye," said Eeyore. "Mind you don't get blown away, little Piglet.
You'd be missed. People would say 'Where's little Piglet been blown
to?'—really wanting to know. Well, good-bye. And thank you for
happening to pass me."
"Good-bye," said Pooh and Piglet for the last time, and they pushed
on to Owl's house.
The wind was against them now, and Piglet's ears streamed behind
him like banners as he fought his way along, and it seemed hours
before he got them into the shelter of the Hundred Acre Wood and
they stood up straight again, to listen, a little nervously, to the roaring
of the gale among the tree-tops.
"Supposing a tree fell down, Pooh, when we were underneath it?"
"Supposing it didn't," said Pooh after careful thought.
Piglet was comforted by this, and in a little while they were knocking
and ringing very cheerfully at Owl's door.
"Hallo, Owl," said Pooh. "I hope we're not too late for——I mean, how
are you, Owl? Piglet and I just came to see how you were, because
it's Thursday."
"Sit down, Pooh, sit down, Piglet," said Owl kindly. "Make yourselves
comfortable."
They thanked him, and made themselves as comfortable as they
could.
"Because, you see, Owl," said Pooh, "we've been hurrying, so as to
be in time for—so as to see you before we went away again."
Owl nodded solemnly.
"Correct me if I am wrong," he said, "but am I right in supposing that it
is a very Blusterous day outside?"
"Very," said Piglet, who was quietly thawing his ears, and wishing that
he was safely back in his own house.
"I thought so," said Owl. "It was on just such a blusterous day as this
that my Uncle Robert, a portrait of whom you see upon the wall on
your right, Piglet, while returning in the late forenoon from a——
What's that?"
There was a loud cracking noise.
"Look out!" cried Pooh. "Mind the clock! Out of the way, Piglet! Piglet,
I'm falling on you!"
"Help!" cried Piglet.
Pooh's side of the room was slowly tilting upwards and his chair
began sliding down on Piglet's. The clock slithered gently along the
mantelpiece, collecting vases on the way, until they all crashed
together on to what had once been the floor, but was now trying to
see what it looked like as a wall. Uncle Robert, who was going to be
the new hearth-rug, and was bringing the rest of his wall with him as
carpet, met Piglet's chair just as Piglet was expecting to leave it, and
for a little while it became very difficult to remember which was really
the north. Then there was another loud crack ... Owl's room collected
itself feverishly ... and there was silence.

In a corner of the room, the tablecloth began to wriggle.


Then it wrapped itself into a ball and rolled across the room.
Then it jumped up and down once or twice, and put out two ears. It
rolled across the room again, and unwound itself.
"Pooh," said Piglet nervously.
"Yes?" said one of the chairs.
"Where are we?"
"I'm not quite sure," said the chair.
"Are we—are we in Owl's House?"
"I think so, because we were just going to have tea, and we hadn't
had it."
"Oh!" said Piglet. "Well, did Owl always have a letter-box in his
ceiling?"
"Has he?"
"Yes, look."
"I can't," said Pooh. "I'm face downwards under something, and that,
Piglet, is a very bad position for looking at ceilings."
"Well, he has, Pooh."
"Perhaps he's changed it," said Pooh. "Just for a change."
There was a disturbance behind the table in the other corner of the
room, and Owl was with them again.

"Ah, Piglet," said Owl, looking very much annoyed; "where's Pooh?"

You might also like