ALL-IN-ONE
Making Everything Easier!
™
$39.99 US / $47.99 CN / £27.99 UK
ISBN 978-0-470-53755-8
Programming Languages/HTML
Go to Dummies.com
®
for videos, step-by-step examples,
how-to articles, or to shop!
Harris
spine=2.16”
HTML, XHTML,
& CSS
ALL-IN-ONE
HTML, XHTML,
& CSS
• Creating the HTML/XHTML Foundation
• Styling with CSS
• Using Positional CSS
• Client-Side Programming with JavaScript®
• Server-Side Programming with PHP
• Managing Data with MySQL®
• Into the Future with AJAX
• Moving from Pages to Sites
Andy Harris
Open the book and find:
• The basics of building XHTML
documents
• What to do with selectors,
classes, and styles
nimation, and cool ways to use the UI library
• Create super sites — u
nderstand clients and servers, work with
content management systems, and more
Andy Harris taught himself programming because it was fun. Today he
teaches computer science, game development, and Web programming at
the university level; is a technology consultant for the state of Indiana; and
has helped people with disabilities to form their own Web development
companies.
8
IN
1
BOOKS
BOOKS
Valuable bonus programs on CD-ROM
Bonus CD Includes
Firefox browser plus valuable extensions and plugins
Aptana programmer’s editor that simplifies the process
XAMPP, an easy-to-install server package
Visit the companion Web site at www.dummies.com/
go/htmlxhtmlandcssaiofd2e for code and other
supporting materials
Valuable bonus
tools on CD-ROM!
Covers HTML5 and prior versions of HTML!
2nd Edition
2nd Edition
www.it-ebooks.info
Mobile Apps
• Health & Wellness
• Computing, iPods & Cell Phones
• eBay
• Internet
• Food, Home & Garden
Find out “HOW” at Dummies.com
*Sweepstakes not currently available in all countries; visit Dummies.com for official rules.
Get More and Do More at Dummies.com
®
To access the Cheat Sheet created specifically for this book, go to
www.dummies.com/cheatsheet/htmlxhtmlandcss
spine=2.16”
www.it-ebooks.info
HTML, XHTML,
& CSS
ALL-IN-ONE
FOR
DUMmIES
‰
2ND EDITION
01_9780470537558-ffirs.indd i01_9780470537558-ffirs.indd i 10/7/10 8:24 PM10/7/10 8:24 PM
www.it-ebooks.info
01_9780470537558-ffirs.indd ii01_9780470537558-ffirs.indd ii 10/7/10 8:24 PM10/7/10 8:24 PM
www.it-ebooks.info
by Andy Harris
HTML, XHTML,
& CSS
ALL-IN-ONE
FOR
DUMmIES
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF
THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITH-
OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE
CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES
CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF
A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZA-
TION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE
OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES
THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT
MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS
WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND
WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2010937814
ISBN: 978-0-470-53755-8
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
01_9780470537558-ffirs.indd iv01_9780470537558-ffirs.indd iv 10/7/10 8:24 PM10/7/10 8:24 PM
Disclaimer: This eBook does not include ancillary media that was packaged with the
printed version of the book.
www.it-ebooks.info
About the Author
real partnership, like the rest of our life. Thanks for being my best friend and
companion. Thanks also for doing all the work it takes for us to sustain a
family when I’m in writing mode.
Thank you to Mark Enochs. It’s great to have an editor who gets me, and
who’s willing to get excited about a project. I really enjoy working with you.
Thanks very much to Katie Feltman. It’s fun to see how far a few wacky ideas
have gone. Thanks for continuing to believe in me, and for helping me to
always nd an interesting new project.
Thank you to the copy editors: rst and foremost, I thank Brian Walls for his
all his hard work in making this edition presentable. Thanks also go to Teresa
Artman, John Edwards, and Melba Hopper for their help. I appreciate your
efforts to make my geeky mush turn into something readable. Thanks for
improving my writing.
A special thanks to Jeff Noble for his technical editing. I appreciate your vigi-
lance. You have helped to make this book as technically accurate as possible.
Thank you to the many people at Wiley who contribute to a project like
this. The author only gets to meet a few people, but so many more are
involved in the process. Thank you very much for all you’ve done to help
make this project a reality.
Thanks to Chris McCulloh for all you did on the rst edition, and I thank you
for your continued friendship.
A big thank you to the open source community which has created so many
incredible tools and made them available to all. I’d especially like to thank the
creators of Firefox, Firebug, Aptana, HTML Validator, the Web Developer tool-
bar, Ubuntu and the Linux community, Notepad++, PHP, Apache, jQuery, and
the various jQuery plugins. This is an amazing and generous community effort.
I’d nally like to thank the IUPUI computer science family for years of support
on various projects. Thank you especially to all my students, current and
past. I’ve learned far more from you than the small amount I’ve taught. Thank
you for letting me be a part of your education.
Proofreaders: Lauren Mandelbaum,
Christine Sabooni
Indexer: BIM Indexing & Proofreading Services
Special Help: Tonya Cupp,
Colleen Totz Diamond
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Composition Services
Debbie Stailey, Director of Composition Services
01_9780470537558-ffirs.indd viii01_9780470537558-ffirs.indd viii 10/7/10 8:24 PM10/7/10 8:24 PM
www.it-ebooks.info
Contents at a Glance
Introduction 1
Book I: Creating the HTML/XHTML Foundation 7
Chapter 1: Sound HTML Foundations 9
Chapter 2: It’s All about Validation 19
Chapter 3: Choosing Your Tools 41
Chapter 4: Managing Information with Lists and Tables 65
Chapter 5: Making Connections with Links 83
Chapter 6: Adding Images 93
Chapter 7: Creating Forms 121
Chapter 8: The Future of HTML: HTML 5 141
Book II: Styling with CSS 157
Chapter 1: Coloring Your World 159
Chapter 2: Styling Text 177
Chapter 4: Putting Data Together with Joins 705
Book VII: Into the Future with AJAX 729
Chapter 1: AJAX Essentials 731
Chapter 2: Improving JavaScript and AJAX with jQuery 747
Chapter 3: Animating jQuery 771
Chapter 4: Using the jQuery User Interface Toolkit 797
Chapter 5: Improving Usability with jQuery 823
Chapter 6: Working with AJAX Data 843
Book VIII: Moving from Pages to Sites 867
Chapter 1: Managing Your Servers 869
Chapter 2: Planning Your Sites 895
Chapter 3: Introducing Content Management Systems 915
Chapter 4: Editing Graphics 941
Chapter 5: Taking Control of Content 961
Appendix A: What’s on the CD 979
Index 985
02_9780470537558-ftoc.indd x02_9780470537558-ftoc.indd x 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info
Table of Contents
Introduction 1
No Experience Necessary 2
Great for Advanced Folks, Too! 2
Use Any Computer 3
Don’t Buy Any Software 3
How This Book Is Organized 4
New for the Second Edition 5
Icons Used in This Book 6
What’s Next? 6
Book I: Creating the HTML/XHTML Foundation 7
Chapter 1: Sound HTML Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Tools to avoid unless you have nothing else 44
A noteworthy editor: Notepad++ 45
The old standards: VI and Emacs 46
Other text editors 49
The Web Developer’s Browser 49
A little ancient history 49
Overview of the prominent browsers 50
Other notable browsers 52
The bottom line in browsers 53
Tricking Out Firefox 53
Validating your pages with HTML Validator 54
Using the Web Developer toolbar 55
Using Firebug 57
Using a Full-Blown IDE 58
Introducing Aptana 58
Customizing Aptana 60
Introducing Komodo Edit 62
Chapter 4: Managing Information with Lists and Tables . . . . . . . . . . .65
Making a List and Checking It Twice 65
Creating an unordered list 65
Creating ordered lists 67
Making nested lists 69
Building the de nition list 72
Building Tables 74
De ning the table 75
Spanning rows and columns 77
Avoiding the table-based layout trap 80
Chapter 5: Making Connections with Links . . . . . . . . . . . . . . . . . . . . . .83
Making Your Text Hyper 83
Introducing the anchor tag 84
Batch processing 115
Using Images as Links 117
Creating thumbnail images 118
Creating a thumbnail-based image directory 120
Chapter 7: Creating Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
You Have Great Form 121
Forms must have some form 123
Organizing a form with eldsets and labels 123
Building Text-Style Inputs 126
Making a standard text eld 126
Building a password eld 127
Making multi-line text input 128
Creating Multiple Selection Elements 130
Making selections 130
Building check boxes 132
Creating radio buttons 134
Pressing Your Buttons 136
Making input-style buttons 137
Building a Submit button 138
It’s a do-over: The Reset button 138
Introducing the <button> tag 139
Chapter 8: The Future of HTML: HTML 5. . . . . . . . . . . . . . . . . . . . . . . . .141
Can’t We Just Stick with XHTML? 141
Using the HTML 5 doctype 142
Browser support for HTML 5 142
Validating HTML 5 142
02_9780470537558-ftoc.indd xiii02_9780470537558-ftoc.indd xiii 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info
HTML, XHTML, & CSS All-in-One For Dummies, 2nd Edition
xiv
Applying the font-family style attribute 179
Using generic fonts 180
Making a list of fonts 181
The Curse of Web-Based Fonts 183
Understanding the problem 183
Examining possible solutions 184
Using images for headlines 185
Specifying the Font Size 188
Size is only a suggestion! 188
Using the font-size style attribute 188
Absolute measurement units 189
Relative measurement units 190
02_9780470537558-ftoc.indd xiv02_9780470537558-ftoc.indd xiv 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info
Table of Contents
xv
Determining Other Font Characteristics 191
Using font-style for italics 192
Using font-weight for bold 193
Using text-decoration 194
Using text-align for basic alignment 196
Other text attributes 197
Using the font shortcut 197
Working with subscripts and superscripts 199
Chapter 3: Selectors, Class, and Style . . . . . . . . . . . . . . . . . . . . . . . . . .201
Selecting Particular Segments 201
De ning more than one kind of paragraph 201
Styling identi ed paragraphs 203
Using Emphasis and Strong Emphasis 203
Adding emphasis to the page 204
www.it-ebooks.info
HTML, XHTML, & CSS All-in-One For Dummies, 2nd Edition
xvi
Chapter 5: Levels of CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239
Managing Levels of Style 239
Using local styles 239
Using an external style sheet 242
Understanding the Cascading Part of Cascading Style Sheets 246
Inheriting styles 247
Hierarchy of styles 248
Overriding styles 249
Precedence of style de nitions 250
Using Conditional Comments 251
Coping with incompatibility 251
Making Internet Explorer–speci c code 252
Using a conditional comment with CSS 253
Checking the Internet Explorer version 256
Book III: Using Positional CSS 257
Chapter 1: Fun with the Fabulous Float . . . . . . . . . . . . . . . . . . . . . . . . .259
Avoiding Old-School Layout Pitfalls 259
Problems with frames 259
Problems with tables 260
Problems with huge images 261
Problems with Flash 261
Introducing the Floating Layout Mechanism 262
Using oat with images 263
Adding the oat property 264
Using Float with Block-Level Elements 265
Floating a paragraph 265
Adjusting the width 267
Revisiting List Styles 299
De ning navigation as a list of links 300
Turning links into buttons 300
Building horizontal lists 302
Creating Dynamic Lists 304
Building a nested list 304
Hiding the inner lists 306
Getting the inner lists to appear on cue 307
Building a Basic Menu System 310
Building a vertical menu with CSS 312
Building a horizontal menu 314
Chapter 4: Using Alternative Positioning. . . . . . . . . . . . . . . . . . . . . . . .317
Working with Absolute Positioning 317
Setting up the HTML 318
Adding position guidelines 318
Making absolute positioning work 319
Managing z-index 320
Handling depth 320
Working with z-index 322
Building a Page Layout with Absolute Positioning 322
Overview of absolute layout 322
Writing the XHTML 324
Adding the CSS 324
Creating a More Flexible Layout 326
Designing with percentages 326
Building the layout 328
Exploring Other Types of Positioning 329
Creating a xed menu system 330
Setting up the XHTML 331
Setting the CSS values 332
The pesky plus sign 355
Changing Variables to the Desired Type 356
Using variable conversion tools 356
Fixing the addInput code 357
Chapter 2: Making Decisions with Conditions. . . . . . . . . . . . . . . . . . .359
Working with Random Numbers 359
Creating an integer within a range 359
Building a program that rolls dice 360
Using if to Control Flow 361
The basic if statement 362
All about conditions 363
Comparison operators 363
Using the else Clause 364
Using if-else for more complex interaction 365
Solving the mystery of the unnecessary else 367
Using switch for More Complex Branches 367
Creating an expression 368
Switching with style 369
02_9780470537558-ftoc.indd xviii02_9780470537558-ftoc.indd xviii 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info
Table of Contents
xix
Nesting if Statements 370
Building the nested conditions 371
Making sense of nested ifs 372
Chapter 3: Loops and Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373
Building Counting Loops with for 373
Building a standard for loop 374
Counting backward 375
Counting by 5 375
Examining variable scope 402
Building a Basic Array 405
Accessing array data 405
Using arrays with for loops 406
Revisiting the ants song 407
02_9780470537558-ftoc.indd xix02_9780470537558-ftoc.indd xix 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info
HTML, XHTML, & CSS All-in-One For Dummies, 2nd Edition
xx
Working with Two-Dimension Arrays 409
Setting up the arrays 410
Getting a city 411
Creating a main( ) function 411
Creating Your Own Objects 413
Building a basic object 413
Adding methods to an object 414
Building a reusable object 415
Using your shiny new objects 417
Introducing JSON 417
Storing data in JSON format 418
Building a more complex JSON structure 419
Chapter 5: Talking to the Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423
Understanding the Document Object Model 423
Navigating the DOM 423
Changing DOM properties with Firebug 425
Examining the document object 425
Harnessing the DOM through JavaScript 427
Getting the blues, JavaScript-style 427
Writing JavaScript code to change colors 428
Managing Button Events 428
Interpreting radio buttons 456
Working with Regular Expressions 457
Introducing regular expressions 460
Using characters in regular expressions 462
Marking the beginning and end of the line 463
Working with special characters 463
Conducting repetition operations 464
Working with pattern memory 465
Chapter 7: Animating Your Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467
Making Things Move 467
Looking over the HTML 468
Getting an overview of the JavaScript 470
Creating global variables 471
Initializing 472
Moving the sprite 472
Checking the boundaries 474
Reading Input from the Keyboard 475
Building the keyboard page 476
Overwriting the init( ) function 477
Setting up an event handler 478
Responding to keystrokes 479
Deciphering the mystery of key codes 480
Following the Mouse 481
Looking over the HTML 481
Initializing the code 482
Building the mouse listener 483
Creating Automatic Motion 483
Creating a setInterval( ) call 485
Building Image-Swapping Animation 486
Preparing the images 487
Using double quote interpolation 515
Generating output with heredocs 515
Switching from PHP to XHTML 517
Chapter 2: PHP and XHTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519
Exploring the Relationship between PHP and XHTML 519
Embedding PHP inside XHTML 520
Viewing the results 521
Sending Data to a PHP Program 522
Creating a form for PHP processing 523
Receiving data in PHP 525
Choosing the Method of Your Madness 527
Using get to send data 527
Using the post method to transmit form data 529
Getting data from the form 530
Retrieving Data from Other Form Elements 532
Building a form with complex elements 532
Responding to a complex form 535
Chapter 3: Control Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539
Introducing Conditions (Again) 539
Building the Classic if Statement 540
Rolling dice the PHP way 541
Checking your six 541
Understanding comparison operators 545
Taking the middle road 545
Building a program that makes its own form 547
Making a switch 549
Looping with for 552
Looping with while 555
02_9780470537558-ftoc.indd xxii02_9780470537558-ftoc.indd xxii 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info
Writing text to les 592
Writing a basic text le 594
Reading from the le 599
Using Delimited Data 601
Storing data in a CSV le 601
Viewing CSV data directly 603
Reading the CSV data in PHP 604
Working with File and Directory Functions 608
opendir( ) 608
readdir( ) 608
chdir( ) 609
Generating the list of le links 609
Chapter 7: Connecting to a MySQL Database . . . . . . . . . . . . . . . . . . . .613
Retrieving Data from a Database 613
Understanding data connections 616
Building a connection 617
02_9780470537558-ftoc.indd xxiii02_9780470537558-ftoc.indd xxiii 10/7/10 8:26 PM10/7/10 8:26 PM
www.it-ebooks.info