CSS3 (The Missing Manual 3rd Edition) David Sawyer O'Reilly

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 650

DownloadCSS3 (The Missing Manual - 3rd Edition) David Sawyer O'Reilly
Open PDF In BrowserView PDF
CSS3
Third Edition

The book that should have been in the box®

David Sawyer McFarland

Beijing | Cambridge | Farnham | Köln | Sebastopol | Tokyo

CSS3: The Missing Manual, Third Edition
by David Sawyer McFarland
Copyright © 2013 Sawyer McFarland Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc.,
1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://my.safaribooksonline.com).
For more information, contact our corporate/institutional sales department: (800)
998-9938 or corporate@oreilly.com.
August 2006:
August 2009:
December 2012:

First Edition.
Second Edition.
Third Edition.

Revision History for the Third Edition:
2012-12-07

First release

See http://oreilly.com/catalog/errata.csp?isbn=0636920024996 for release details.

The Missing Manual is a registered trademark of O’Reilly Media, Inc. The Missing
Manual logo, and “The book that should have been in the box” are trademarks of
O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to
distinguish their products are claimed as trademarks. Where those designations
appear in this book, and O’Reilly Media is aware of a trademark claim, the
designations are capitalized.
While every precaution has been taken in the preparation of this book, the publisher
assumes no responsibility for errors or omissions, or for damages resulting from the
use of the information contained in it.

ISBN-13: 978-1-449-32594-7
[V]

Contents
The Missing Credits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Part One:
Chapter 1:

CSS Basics
HTML for CSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
HTML: Past and Present . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Writing HTML for CSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
The Importance of the Doctype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Making Sure Internet Explorer Is Up-to-Date. . . . . . . . . . . . . . . . . . . . . . . . . .  32

Chapter 2:

Creating Styles and Style Sheets. . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Anatomy of a Style. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  35
Understanding Style Sheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  38
Internal Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
External Style Sheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Tutorial: Creating Your First Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Chapter 3:

Selectors: Identifying What to Style. . . . . . . . . . . . . . . . . . . . . . . 55
Tag Selectors: Page-Wide Styling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Class Selectors: Pinpoint Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  57
ID Selectors: Specific Page Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Styling Groups of Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  62
Styling Tags Within Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Pseudo-Classes and Pseudo-Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Attribute Selectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  72
Child Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  74
Siblings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  79
The :not( ) Selector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Tutorial: Selector Sampler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Chapter 4:

Saving Time with Style Inheritance. . . . . . . . . . . . . . . . . . . . . . . . 93
What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  93
How Inheritance Streamlines Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The Limits of Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Tutorial: Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  97
iii

Chapter 5:

Managing Multiple Styles: The Cascade. . . . . . . . . . . . . . . . . . . 103
How Styles Cascade. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  104
Specificity: Which Style Wins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  108
Controlling the Cascade. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  111
Tutorial: The Cascade in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Part Two:

Applied CSS

Chapter 6:

Formatting Text.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Using Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Using Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Discovering Google Web Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  146
Adding Color to Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  154
Changing Font Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Formatting Words and Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Adding Text Shadow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  166
Formatting Entire Paragraphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Styling Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Tutorial: Text Formatting in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

Chapter 7:

Margins, Padding, and Borders. . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Understanding the Box Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Controling Space with Margins and Padding . . . . . . . . . . . . . . . . . . . . . . . . .  195
Adding Borders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Coloring the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Creating Rounded Corners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Adding Drop Shadows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  210
Determining Height and Width. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Wrapping Content with Floating Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Tutorial: Margins, Backgrounds, and Borders. . . . . . . . . . . . . . . . . . . . . . . . . 225

Chapter 8:

Adding Graphics to Web Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Discovering CSS and the  Tag. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Adding Background Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Controlling Repetition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Positioning a Background Image. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Using Background Property Shorthand. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Using Multiple Background Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
Utilizing Gradient Backgrounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Applying Easy Gradients with Colorzilla. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Tutorial: Enhancing Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  272
Tutorial: Creating a Photo Gallery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Tutorial: Using Background Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

iv

Contents

Chapter 9:

Sprucing Up Your Site’s Navigation. . . . . . . . . . . . . . . . . . . . . . . 293
Selecting Which Links to Style. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Styling Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Building Navigation Bars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
CSS-Style Preloading Rollovers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Styling Particular Types of Links. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Tutorial: Styling Links. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  318
Tutorial: Creating a Navigation Bar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

Chapter 10:

CSS Transforms, Transitions, and Animations. . . . . . . . . . . . . 335
Transforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Transitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

Chapter 11:

Formatting Tables and Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Using Tables the Right Way. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Styling Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Styling Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Tutorial: Styling a Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Tutorial: Styling a Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

Part Three:
Chapter 12:

CSS Page Layout
Introducing CSS Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Types of Web Page Layouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
How CSS Layout Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Layout Strategies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

Chapter 13:

Building Float-Based Layouts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Applying Floats to Your Layouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Overcoming Float Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Tutorial: Multiple Column Layouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440

Chapter 14:

Responsive Web Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Responsive Web Design Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Setting Up a Web Page for RWD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Media Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Flexible Grids. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Fluid Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Responsive Web Design Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

Contents

v

Chapter 15:

Positioning Elements on a Web Page. . . . . . . . . . . . . . . . . . . . . 487
How Positioning Properties Work. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Powerful Positioning Strategies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Tutorial: Positioning Page Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

Part Four:

Advanced CSS

Chapter 16:

CSS for the Printed Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
How Media Style Sheets Work. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  519
How to Add Media Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Creating Print Style Sheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Tutorial: Building a Print Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

Chapter 17:

Improving Your CSS Habits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Adding Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Organizing Styles and Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  541
Eliminating Browser Style Interference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Using Descendent Selectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Trying Different CSS for Internet Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . 558

Part Five:
Appendix A:

Appendixes
CSS Property Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
CSS Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Text Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
List Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Padding, Borders, and Margins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Backgrounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Page Layout Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Animation, Transform, and Transition Properties. . . . . . . . . . . . . . . . . . . . . . 588
Table Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Miscellaneous Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

Appendix B:

CSS Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
CSS Help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
CSS Tips, Tricks, and Advice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
CSS Navigation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  601
CSS Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Showcase Sites. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
CSS Books. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
CSS Software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
vi

Contents

The Missing Credits
About the Author
David Sawyer McFarland is president of Sawyer McFarland Media,
Inc., a web development and training company in Portland, Oregon.
He’s been building websites since 1995, when he designed his first
website: an online magazine for communication professionals. He’s
served as the webmaster at the University of California at Berkeley
and the Berkeley Multimedia Research Center, and he has helped
build, design, and program numerous websites for clients including
Macworld.com.
In addition to building websites, David is also a writer, trainer, and instructor. He’s
taught web design at the UC Berkeley Graduate School of Journalism, the Center
for Electronic Art, the Academy of Art College, Ex’Pressions Center for New Media,
and the Art Institute of Portland. He currently teaches in the Multimedia Program at
Portland State University. He’s written articles about web design for Practical Web
Design, Macworld magazine, and CreativePro.com.
David is also the author of Dreamweaver: The Missing Manual and JavaScript: The
Missing Manual.
He welcomes feedback about this book by email: missing@sawmac.com. (If you’re
seeking technical help, however, please refer to the sources listed in Appendix B.)

About the Creative Team
Nan Barber (editor) has worked with the Missing Manual series since the previous
century. She lives in Massachusetts with her husband and iMac. Email: nanbarber@
oreilly.com.
Holly Bauer (production editor) lives in Ye Olde Cambridge, Mass., where she is an
avid home cook, prolific DIYer, and mid-century modern design enthusiast. Email:
holly@oreilly.com.
Nancy Reinhardt (proofreader) lives in the Midwest, where she enjoys summer
weekends at the lake, boating, swimming, and reading voraciously. Nan is not only
a freelance copyeditor and proofreader, but she’s also a published romance novelist.
Check out her work at www.nanreinhardt.com. Email: nanleigh1@gmail.com.
Nancy A. Guenther (indexer) indexed this book on behalf of Potomac Indexing, LLC,
an international indexing partnership at www.potomacindexing.com. She has been a
full-time freelance indexer since 1983, specializing in computer software, American
studies, and business. Her website is www.guenther​.bizland.com.

The Missing Credits

vii

Daniel J. Quinn (technical reviewer) is a freelance web developer at DQuinn.net.
For the past five years, he has worked as a senior UI engineer at award-winning
digital agency Genuine Interactive, specializing in WordPress and content strategy
for brands like Sam Adams, MassMutual, and Children’s Hospital Boston. Today,
Daniel serves as web developer for Harvard University’s Digital Communications
department. He regularly works with a team of local designers and can be reached
at daniel@dquinn.net.
Jason Arnold (technical reviewer) lives in Santa Rosa, California, with his wife and
three daughters. He works at Healdsburg District Hospital as a Telemetry Technician
and teaches Kenpo Karate to children. He is currently working toward his nursing
degree at Santa Rosa Junior College. In his free time, he does Kenpo Karate with
his daughters and is always on the lookout for an extra tech project to stay busy.

Acknowledgements
Many thanks to all those who helped with this book, including my students, who
always help me see complex concepts through beginners’ eyes. Thanks to my technical editors, Daniel Quinn and Jason Arnold, who saved me from any embarrassing
mistakes, and Zoe Gillenwater whose valuable advice for the first edition of this book
lives on. Also, we all owe a big debt of gratitude to the many web designers who
have broken new ground by using CSS in creative ways and shared their discoveries
with the web design community.
Finally, thanks to David Pogue, whose unflagging enthusiasm and endurance is inspiring; Nan Barber for refining my writing, fixing my mistakes, and keeping me on
track; my wife, Scholle, for her love and support; my son, Graham, who suggested
that I’d get this book done a lot faster if I just typed “Blah, blah, blah, blah, BOO!”
for each chapter; my wonderful daughter, Kate, whose smile is always a great pickme-up; and to my family: Mom, Doug, Mary, David, Marisa, Tessa, Phyllis, Les, Del,
Patricia, and Mike.

—David Sawyer McFarland

The Missing Manual Series
Missing Manuals are witty, superbly written guides to computer products that don’t
come with printed manuals (which is just about all of them). Each book features a
handcrafted index and cross-references to specific pages (not just chapters). Recent
and upcoming titles include:

Access 2010: The Missing Manual by Matthew MacDonald
Access 2013: The Missing Manual by Matthew MacDonald
Adobe Edge Animate: The Missing Manual by Chris Grover
Buying a Home: The Missing Manual by Nancy Conner
Creating a Website: The Missing Manual, Third Edition, by Matthew MacDonald

viii

The Missing Credits

David Pogue’s Digital Photography: The Missing Manual by David Pogue
Dreamweaver CS6: The Missing Manual by David Sawyer McFarland
Droid 2: The Missing Manual by Preston Gralla
Droid X2: The Missing Manual by Preston Gralla
Excel 2010: The Missing Manual by Matthew MacDonald
FileMaker Pro 12: The Missing Manual by Susan Prosser and Stuart Gripman
Flash CS6: The Missing Manual by Chris Grover
Galaxy S II: The Missing Manual by Preston Gralla
Galaxy Tab: The Missing Manual by Preston Gralla
Google+: The Missing Manual by Kevin Purdy
Google SketchUp: The Missing Manual by Chris Grover
HTML5: The Missing Manual by Matthew MacDonald
iMovie ’11 & iDVD: The Missing Manual by David Pogue and Aaron Miller
iPad: The Missing Manual, Fourth Edition by J.D. Biersdorfer
iPhone: The Missing Manual, Fifth Edition by David Pogue
iPhone App Development: The Missing Manual by Craig Hockenberry
iPhoto ’11: The Missing Manual by David Pogue and Lesa Snider
iPod: The Missing Manual, Tenth Edition by J.D. Biersdorfer and David Pogue
JavaScript & jQuery: The Missing Manual, Second Edition by David Sawyer McFarland
Kindle Fire: The Missing Manual by Peter Meyers
Living Green: The Missing Manual by Nancy Conner
Mac OS X Mountain Lion: The Missing Manual by David Pogue
Microsoft Project 2010: The Missing Manual by Bonnie Biafore
Motorola Xoom: The Missing Manual by Preston Gralla
Netbooks: The Missing Manual by J.D. Biersdorfer
NOOK HD: The Missing Manual by Preston Gralla
Office 2010: The Missing Manual by Nancy Conner, Chris Grover, and Matthew
MacDonald
Office 2011 for Macintosh: The Missing Manual by Chris Grover
Office 2013: The Missing Manual by Nancy Conner and Matthew MacDonald
Personal Investing: The Missing Manual by Bonnie Biafore

The Missing Credits

ix

Photoshop CS6: The Missing Manual by Lesa Snider
Photoshop Elements 10: The Missing Manual by Barbara Brundage
PHP & MySQL: The Missing Manual, Second Edition by Brett McLaughlin
QuickBooks 2012: The Missing Manual by Bonnie Biafore
Switching to the Mac: The Missing Manual, Lion Edition by David Pogue
Windows 7: The Missing Manual by David Pogue
Windows 8: The Missing Manual by David Pogue
Your Body: The Missing Manual by Matthew MacDonald
Your Brain: The Missing Manual by Matthew MacDonald
Your Money: The Missing Manual by J.D. Roth
For a full list of all Missing Manuals in print, go to www.missingmanuals.com/library
.html.

x

The Missing Credits

Introduction

C

ascading Style Sheets—CSS for short—give you creative control over the
layout and design of your web pages. Using CSS, you can dress up your site’s
text with eye-catching headlines, drop caps, and borders, just like the ones in
glossy magazines. You can also arrange images with precision, create columns and
banners, and highlight your links with dynamic rollover effects. You can even make
elements fade in or out of view, move objects around the page, or make a button
slowly change colors when a visitor mouses over it.
Anything that can do all that must be pretty complicated, right? Au contraire! The
whole idea behind CSS is to streamline the process of styling web pages. In the next
few pages, you’ll learn about the basics of CSS. In Chapter 1, you’ll get right to work
creating a CSS-powered web page.

How CSS Works
CSS works with HTML, but it’s not HTML. It’s a different language altogether. While
HTML provides structure to a document by organizing information into headers,
paragraphs, bulleted lists, and so on, CSS works hand-in-hand with the web browser
to make HTML look good.
For example, you might use HTML to turn a phrase into a top-level heading, indicating that it introduces the content on the rest of the page. However, you’d use CSS to
format that heading with, say, big and bold red type and position it 50 pixels from
the left edge of the window. In CSS, that text formatting comprises a style—a rule
describing the appearance of a particular portion of a web page. A style sheet is a
set of these styles.

1

The Benefits
of CSS

You can also create styles specifically for working with images. For instance, a style
can align an image along the right edge of a web page, surround the image with a
colorful border, and place a 50-pixel margin between the image and the surrounding text.
Once you’ve created a style, you can apply it to text, images, headings, or other
elements on a page. For example, you can select a paragraph of text and apply a
style to instantly change the text’s size, color, and font. You can also create styles
for specific HTML tags, so, for example, all first-level headings (

tags) in your site are displayed in the same style, no matter where they appear. The Benefits of CSS Before CSS, web designers were limited to the layout and styling options of HTML. And if you surfed the Web in 1995, then you understand the emphasis on limited. HTML still forms the foundation of all pages on the World Wide Web, but it’s simply not a design tool. Sure, HTML provides basic formatting options for text, images, tables, and other web page elements, and patient, meticulous webmasters can make pages look pretty good using only HTML. But the result is often sluggish web pages laden with clunky code. CSS, in contrast, offers the following advantages: • Style sheets offer far more formatting choices than HTML. With CSS, you can format paragraphs as they appear in a magazine or newspaper (the first line indented and no space between each paragraph, for example) and control the leading (the space between lines of type in a paragraph). • When you use CSS to add a background image to a page, you get to decide whether and how it tiles (repeats). HTML can’t even begin to do that. • Even better, CSS styles take up much less space than HTML’s formatting options, such as the much-hated tag. You can usually trim a lot of kilobytes from text-heavy web pages by using CSS. As a result, your pages look great and load faster. • Style sheets also make updating your site easier. You can collect all your styles into a single external style sheet that’s linked to every page in your site. Then, when you edit a style, that change immediately ripples through your site wherever that style appears. You can completely change the appearance of a site just by editing a single style sheet. What You Need to Know This book assumes you’ve already got some knowledge of HTML (and maybe some CSS experience as well). Perhaps you’ve built a site or two (or at least a page or two) and have some familiarity with the sea of tags—,

,

, , 2 CSS3: The Missing Manual and so on—that make up the Hypertext Markup Language. CSS can’t do anything without HTML, so to move forward you need to know how to create a web page by using basic HTML. Document Types If you’ve used HTML in the past to create web pages, but feel like your knowledge is a bit rusty, the next section provides a basic refresher. Tip If you’re just getting your feet wet learning HTML, then check out these free online tutorials: HTML Dog (www.htmldog.com/guides/htmlbeginner) and W3Schools (www.w3schools.com/html). If you’re a printed page fan, then you may want to pick up a copy of Creating a Website: The Missing Manual, Third Edition or Head First HTML and CSS, Second Edition (both O’Reilly). HTML: The Barebones Structure HTML (Hypertext Markup Language) uses simple commands called tags to define the various parts of a web page. For example, this HTML code creates a simple web page: Hey, I am the title of this web page

Hey, I am a paragraph on this web page.

It may not be exciting, but this example has all the basic elements a web page needs. You’ll notice something called a doctype declaration at the very beginning of the code, followed by (with the brackets), a head, a body, and some stuff—the actual page contents—inside the body, ending in a final . Document Types All web pages begin with a doctype—a line of code that identifies what flavor of HTML you used to write the page. Two doctypes have been used for years—HTML 4.01 and XHTML 1.0—and each of those doctypes has two styles: strict and transitional. For example, the HTML 4.01 transitional doctype looks like the following (the other doctypes for HTML 4.01 and XHTML 1.0 look similar): Note For examples of all various doctypes, visit www.webstandards.org/learn/reference/templates. Introduction 3 How HTML Tags Work If you look at the code for the sample HTML page in this section, you’ll see that it uses a much more succinct doctype: That’s HTML5’s new—and simpler—doctype. HTML5 is intended to be easier to use and more streamlined than its predecessors. This book uses the HTML5 doctype, which is supported by every popular browser (even the old Internet Explorer 6). And because HTML5 is the future of the Web, there’s no reason to use the older doctypes any longer. Note Just because the HTML doctype works in older browsers doesn’t mean that those browsers understand all HTML5 tags or features. Internet Explorer 8 and earlier, for example, don’t recognize the new HTML5 tags. To style tags with CSS for those versions of IE, you have to employ a little JavaScript. You’ll learn how to get older browsers up to speed on page 30. But no matter which doctype you prefer, the important thing is to always use one. Without it, your pages will look different depending on your visitor’s browser, since browsers display CSS differently if they don’t have a doctype for guidance. Each doctype requires you to write your HTML in a certain way. For example, the tag for a line break looks like this in HTML 4.01:
But in XHTML, it looks like this:
And there’s another advantage of HTML5: it accepts either one. How HTML Tags Work In the simple HTML example on page 3, as in the HTML code of any web page, most commands appear in pairs that surround a block of text or other commands. Sandwiched between brackets, these tags are instructions that tell a web browser how to display the web page. Tags are the “markup” part of the Hypertext Markup Language. The starting (opening) tag of each pair tells the browser where the instruction begins, and the ending tag tells it where the instruction ends. Ending or closing tags always include a forward slash (/) after the first bracket symbol (<). On any web page, you’ll usually find at least these four elements. • The first line of a web page is the DOCTYPE declaration, discussed in the previous section. • The tag appears once at the beginning of a web page and again (with an added forward slash) at the end: . This tag tells a web browser that 4 CSS3: The Missing Manual the information contained in this document is written in HTML, as opposed to some other language. All the contents of a page, including other tags, appear between the opening and closing tags. XHTML, Too If you were to think of a web page as a tree, the tag would be its roots. Springing from the trunk are two branches that represent the two main parts of any web page: the head and the body. • The head of a web page contains the title of the page (“Izzie’s Mail-Order Pencils”). It may also include other, invisible information, like a page description, that browsers and search engines use. You surround the head section with opening and closing tags. In addition, the head section can include information that browsers use to format the page’s HTML and to add interactivity. As you’ll see, the section can contain CSS code (like the kind you’ll learn to write in this book) or a link to another file containing CSS information. • The body, as set apart by its surrounding tags, contains all the content that appears inside a browser window—headlines, text, pictures, and so on. Within the tag, you commonly find tags like these: • You tell a web browser where a paragraph of text begins with a

(opening paragraph tag), and where it ends with a

(closing paragraph tag). • The tag emphasizes text. When you surround some text with it and its partner tag, , you get boldface type. The HTML snippet Warning! tells a web browser to strongly emphasize the word “Warning!” • The tag, or anchor tag, creates a hyperlink in a web page. When clicked, a hyperlink—or link—can lead anywhere on the Web. You tell the browser where the link points by putting a web address inside the tags. For instance, you can type Click here!. The browser knows that when your visitor clicks the words “Click here!” it should go to the Missing Manual website. The href part of the tag is called an attribute, and the URL (the Uniform Resource Locator or web address) is the value. In this example, http://www.missingmanuals.com is the value of the href attribute. XHTML, Too Like any technology, HTML has evolved over time. Although standard HTML has served its purpose well, it’s always been a somewhat sloppy language. Among other things, it lets you use uppercase, lowercase, and mixed-case letters in tags ( and are both correct, for example) and permits unclosed tags (so you can use a

tag without a closing

tag to create a paragraph). While this flexibility may make page-writing easier, it also makes life more difficult for web browsers, smart phones, and other technologies that interact with data on the Web. Introduction 5 HTML5: The Wheel Turns Again Additionally, HTML doesn’t work with one of the hottest Internet languages: XML, or Extensible Markup Language. To keep pace with the times, an improved version of HTML, called XHTML, was introduced back in 2000, and you’ll find it used frequently on many sites (in fact, XHTML is just an “XML-ified” version of HTML). XHTML was seen as the future (back in 2000), but has since been supplanted by HTML5. While web browsers still understand XHTML (and probably will for a long time), this book doesn’t include it. HTML5: The Wheel Turns Again When the W3C—the group responsible for many Internet technologies—introduced XHTML as an intermediate step in the transition to XML as the prime language of the Web, the web development community heralded it as the next big thing. History has shown that prediction to be a bit grandiose. As it turns out, the complexity of moving to XML kept browser manufacturers from following the XML path the World Wide Web Consortium laid down. Instead, the W3C closed down the XHTML working group in favor of HTML5—a new version of HTML that a moves away from XML and back to HTML, albeit an enhanced version. With the support of all browser manufacturers, HTML5 is the new next big thing. Some browsers, including Google Chrome and Firefox, already recognize parts of HTML5, and even mainstream news outlets like Time, Newsweek, and CNN drop “HTML5” into their technology reporting. HTML5 isn’t some radically new technology. In fact, unlike XHTML, which was intended to foster a new way to build web pages, HTML5 is about making sure the Web continues to work the way it always has. Most of the basics of HTML are still in place. Also, HTML5 adds a few new elements, meant to support the way web designers currently build websites. For example, in HTML5, the
tag can contain the content you’d usually find at the top of a page, such as a logo and sitewide navigation links; the new
tag to create columns of text and accurately place pictures and text on a page. Unfortunately, since that tag was intended to display spreadsheet-like data— research results, train schedules, and so on—designers had to get creative by using the
tag in unusual ways, sometimes nesting a table within a table within a table to make their pages look good. Meanwhile, browser makers introduced new tags and attributes for the specific purpose of making a page look better. The tag, for example, lets you specify a font color, typeface, and one of seven different sizes. (If you’re keeping score at home, that’s about 100 fewer sizes than you can get with, say, Microsoft Word.) Finally, when designers couldn’t get exactly what they wanted, they often resorted to using graphics. For example, they’d create a large graphic to capture the exact font and layout for web page elements and then slice the Photoshop files into smaller files and piece them back together inside tables to recreate the original design. While all of the preceding techniques—using tags in creative ways, taking advantage of design-specific tag attributes, and making extensive use of graphics—provide design control over your pages, they also add a lot of additional HTML code (and more wrinkles to your forehead). HTML Present: Scaffolding for CSS No matter what content your web page holds—the fishing season calendar, driving directions to the nearest IKEA, or pictures from your kid’s birthday party—it’s the page’s design that makes it look like either a professional enterprise or a part-timer’s hobby. Good design enhances the message of your site, helps visitors find what they’re looking for, and determines how the rest of the world sees your website. That’s why web designers went through the contortions described in the previous section to force HTML to look good. By taking on those design duties, CSS lets HTML go back to doing what it does best—structure content. Using HTML to control the look of text and other web page elements is obsolete. Don’t worry if HTML’s

tag is too big for your taste or bulleted lists aren’t spaced just right. You can take care of that later using CSS. Instead, think of HTML as a method of adding structure to the content you want up on the Web. Use HTML to organize your content and CSS to make that content look great. Chapter 1: HTML for CSS 19 Writing HTML for CSS Writing HTML for CSS If you’re new to web design, you may need some helpful hints to guide your forays into HTML (and to steer clear of well-intentioned, but out-of-date HTML techniques). Or if you’ve been building web pages for a while, you may have picked up a few bad HTML-writing habits that you’re better off forgetting. The rest of this chapter introduces you to some HTML writing habits that will make your mother proud—and help you get the most out of CSS. Think Structure HTML adds meaning to text by logically dividing it and identifying the role that text plays on the page: For example, the

tag is the most important introduction to a page’s content. Other headers let you divide the content into other, less important, but related sections. Just like the book you’re holding, for example, a web page should have a logical structure. Each chapter in this book has a title (think

) and several sections (think

), which in turn contain smaller subsections. Imagine how much harder it would be to read these pages if every word just ran together as one long paragraph. Note For a tutorial on HTML, visit www.w3schools.com/html/html_intro.asp . For a quick list of all available HTML tags, visit Sitepoint.com’s HTML reference at http://reference.sitepoint.com/html. HTML provides many other tags besides headers for marking up content to identify its role on the page. (After all, the M in HTML stands for markup.) Among the most popular are the

tag for paragraphs of text and the

tag for page layout. Use it only to display tabular information like spreadsheets, schedules, and charts. As you’ll see in Part Three of this book, you can do all your layout with CSS for much less time and code than the table-tag tango. • Avoid the awkward tag attributes that enhance only the presentation of the content: background, bgcolor, text, link, alink, and vlink set colors and images for the page, text, and links. CSS gets the job done better (see Chapter 7 and Chapter 8 for CSS equivalents of these attributes). • Don’t abuse the
tag. If you grew up using the
tag (
in XHTML) to insert a line break without creating a new paragraph, then you’re in for a treat. (Browsers automatically—and sometimes infuriatingly—insert a bit of space between paragraphs, including between headers and

tags. In the past, designers used elaborate workarounds to avoid paragraph spacing they didn’t want, like replacing a single

tag with a bunch of line breaks and using a tag to make the first line of the paragraph look like a headline.) Using CSS’s margin controls, you can easily set the amount of space you want to see between paragraphs, headers, and other block-level elements. 26 CSS3: The Missing Manual Note In the next chapter, you’ll learn about a technique called a “CSS Reset,” which eliminates the gaps browsers normally insert between paragraphs and other tags (see page 115). Writing HTML for CSS As a general rule, adding attributes to tags that set colors, borders, background images, or alignment—including attributes that let you format a table’s colors, backgrounds, and borders—is pure old-school HTML. So is using alignment properties to position images and center text in paragraphs and table cells. Instead, look to CSS to control text placement (page 169), borders (page 202), backgrounds (page 206), and image alignment (page 239). UP TO SPEED Validate Your Web Pages HTML follows certain rules: For example, the tag wraps around the other tags on a page, and the tag needs to appear within the <head> tag. XHTML provides an even more strict set of rules to follow, while HTML5 is a bit more lax. It’s easy to forget these rules or simply make a typo. Incorrect (or invalid, as the geeks would say) HTML causes problems like making your page look different in different web browsers. More importantly, even valid CSS may not work as expected with invalid HTML. Fortunately, there are tools for checking whether the HTML in your web pages is correctly written. The easiest way to check—that is, validate—your pages is on the W3C’s website at http://validator.w3.org (see Figure 1-4). Get the Web Developer extension for Firefox ( http://chrispederick​ .com/work/web-developer); it provides a quick way to test a page in the W3C validator. The W3C, or World Wide Web Consortium, is the organization responsible for determining the standards for many of the technologies and languages of the Web, including HTML and CSS. If the W3C validator finds any errors in your page, it tells you what those errors are. If you use Firefox, you can download an extension that lets you validate a web page directly in that browser, without having to visit the W3C site. It can even attempt to fix any problems it encounters. You can get the extension here: http://users.skynet.be/mgueury/mozilla/. Chrome users can download the HTML Tidy extension for Chrome from http://bit.ly/SCONRY. A similar tool is available for the Safari browser as well: www.zappatic.net/safaritidy. Chapter 1: HTML for CSS 27 Writing HTML for CSS Figure 1-4 The W3C HTML validator located at http://validator​ .w3.org lets you quickly make sure the HTML in a page is sound. You can point the validator to an already existing page on the Web, upload an HTML file from your computer, or just paste the HTML of a web page into a form box and then click the Check button. Tips to Guide Your Way It’s always good to have a map for getting the lay of the land. If you’re still not sure how to use HTML to create well-structured web pages, then here are a few tips to get you started: • Use headings to indicate the relative importance of text. Again, think outline. When two headings have equal importance in the topic of your page, use the same level header on both. If one is less important or a subtopic of the other, then use the next level header. For example, follow an <h2> with an <h3> tag (see Figure 1-5). In general, it’s good to use headings in order and try not to skip heading numbers. For example, don’t follow an <h2> tag with an <h5> tag. 28 CSS3: The Missing Manual Writing HTML for CSS Figure 1-5 Use the headline tags (<h1>, <h2>, and so on) as you would if you were outlining a school report: Put them in order of importance, beginning with an <h1> tag, which should shout “Listen up! This is what this whole page is about.” • Use the <p> tag for paragraphs of text. • Use unordered lists (<ul>) when you’ve got a list of several related items, such as navigation links, headlines, or a set of tips like these. • Use numbered lists (<ol>) to indicate steps in a process or define the order of a set of items. The tutorials in this book are a good example, as is a list of rankings like “Top 10 websites popular with monks.” • To create a glossary of terms and their definitions or descriptions, use the <dl> (definition list) tag in conjunction with the <dt> (definition term) and <dd> (definition description) tags. (For an example of how to use this combo, visit www​ .w3schools.com/tags/tryit.asp?filename=tryhtml_list_definition.) • If you want to include a quotation like a snippet of text from another website, a movie review, or just some wise saying of your grandfather’s, try the <blockquote> tag for long passages or the <q> tag to place a short quote within a longer paragraph, like this: <p>Mark Twain is said to have written <q>The The coldest winter I ever spent was a summer in San Francisco</q>. Unfortunately, he never actually wrote that famous quote.</p> • Take advantage of obscure tags like the <cite> tag for referencing a book title, newspaper article, or website, and the <address> tag to identify and supply contact information for the author of a page (great for a copyright notice). Chapter 1: HTML for CSS 29 The Importance of the Doctype • As explained in full on page 26, steer clear of any tag or attribute aimed just at changing the appearance of a text or image. CSS, as you’ll see, can do it all. • When there just isn’t an HTML tag that fits the bill, but you want to identify an element on a page or a bunch of elements on a page so you can apply a distinctive look, use the <div> and <span> tags (see page 21). You’ll get more advice on how to use these in later chapters. • Don’t overuse <div> tags. Some web designers think all they need are <div> tags, ignoring tags that might be more appropriate. For example, to create a navigation bar, you could add a <div> tag to a page and fill it with a bunch of links. A better approach would be to use a bulleted list (<ul> tag), After all, a navigation bar is really just a list of links. As discussed on page 23, HTML5 provides several new tags that can take the place of the <div> tag, like the <article>, <section>, and <footer> tags. For a navigation bar, you could use the HTML5 <nav> tag. • Remember to close tags. The opening <p> tag needs its partner in crime (the closing </p> tag), as do all other tags, except the few self-closers like <br> and <img> (<br /> and <img /> in XHTML). • Validate your pages with the W3C validator (see Figure 1-4 and the box on page 27). Poorly written or typo-ridden HTML causes many weird browser errors. The Importance of the Doctype HTML follows certain rules—these rules are contained in a Document Type Definition file, otherwise known as a DTD. A DTD is a text file that explains what tags, attributes, and values are valid for a particular type of HTML. And for each version of HTML, there’s a corresponding DTD. By now you may be asking, “But what’s all this got to do with CSS?” Everything—if you want your web pages to appear correctly and consistently in web browsers. You tell a web browser which version of HTML or XHTML you’re using by including what’s called a doctype declaration at the beginning of a web page. This doctype declaration is the first line in the HTML file, and defines what version of HTML you’re using (such as HTML5 or HTML 4.01 Transitional). When you mistype the doctype declaration or leave it out, you can throw most browsers into an altered state called quirks mode. Quirks mode is browser manufacturers’ attempt to make their software behave like browsers did circa 1999 (in the Netscape 4 and Internet Explorer 5 days). If a modern browser encounters a page that’s missing the correct doctype, then it thinks, “Gee, this page must have been written a long time ago, in an HTML editor far, far away. I’ll pretend I’m a really old browser and display the page just as one of those 30 CSS3: The Missing Manual buggy old browsers would display it.” That’s why, without a correct doctype, your lovingly CSS-styled web pages may not look as they should, according to current standards. If you unwittingly view your web page in quirks mode when checking it in a browser, you may end up trying to fix display problems that are related to an incorrect doctype and not the incorrect use of HTML or CSS. The Importance of the Doctype Note For more (read: technical) information on quirks mode, visit www.quirksmode.org/css/quirksmode​ .html and https://developer.mozilla.org/en/Mozilla%2527s_Quirks_Mode. Fortunately, it’s easy to get the doctype correct. All you need to know is what version of HTML you’re using. If you’re using HTML5, things are easy. The doctype is simply: <!doctype html> Put this at the top of your HTML file and you’re good to go. If you’re still using older versions of HTML or XHTML such as HTML 4.01 Transitional and XHTML 1.0 Transitional, then the doctype is a lot more convoluted. If you’re using HTML 4.01 Transitional, for example, type the following doctype declaration at the very beginning of every page you create: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www. w3.org/TR/html4/loose.dtd"> The doctype declaration for XHTML 1.0 Transitional is similar. It’s also necessary to add a little code to the opening <html> tag that’s used to identify the file’s XML type—in this case, it’s XHTML—like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> If this entire discussion is making your head ache and your eyes slowly shut, keep your life simple by using the HTML5 doctype. It’s short, easy to remember, and works in all browsers. You can use this doctype even if you don’t touch any of the new HTML5 tags. Note Most visual web page tools like Dreamweaver and Expression Web automatically add a doctype declaration whenever you create a new web page, and many HTML-savvy text editors have shortcuts for adding doctypes. Chapter 1: HTML for CSS 31 Making Sure Internet Explorer Is Up-to-Date Frequently Asked Question Should I Care About IE 6, 7, or 8? I keep hearing that Internet Explorer 6 is dead and we don’t need to worry about it anymore. Is this true? What about other versions of IE? If you’re a web designer, you’ve probably got the latest version of Internet Explorer, Firefox, Safari, Chrome, or Opera on your computer. Previous versions of this book talked quite a bit about Internet Explorer 6 and even provided a section on how to deal with IE 6 bugs. That’s all been removed, since IE 6 is rapidly disappearing. If you’re building sites with a US audience in mind, as of August 2012, less than .6 percent of US web surfers use that outdated browser; in the UK, it’s 1.1%. The exact figure varies depending on whom you ask (for example, http://gs.statcounter.com has worldwide IE 6 usage at .57 percent and www.ie6countdown​ .com says it’s at 6 percent as of August 2012). In China, IE 6 is still widely used. However, even statistics that include the geographic region of your site’s audience don’t truly reflect what visitors to your site use. If you build a site aimed at tech-savvy web designers, odds are that IE 6 hasn’t been to your site in a long time. However, if your site’s aimed at people in China, you may need to deal with IE 6. The best way to find out how much of your traffic comes via IE 6 is to look at your web server’s log files or sign up for Google Analytics (www.google.com/analytics) so you can track your visitors’ browsers (among many other things). For the great majority of web design projects, IE 6 is effectively gone. In addition, IE 7 is also rapidly disappearing, with just 1.16 percent worldwide market share as of August 2012 ( http://gs.statcounter.com/#browser_version-wwmonthly-201108-201208). IE 8, however, you still need to contend with. Windows XP is still a popular operating system, and Microsoft chose not to bring Internet Explorer 9 or later to that operating system. So as long as XP is around, Internet Explorer 8 will be around, too. (To keep up with advances in web development, XP users can switch to Chrome or Firefox.) According to StatCounter, in August 2012, IE 8 accounted for 13.65 percent of all web browsers ( http://gs.statcounter.com/#browser_versionww-monthly-201108-201208 ). According to NetMarketShare, IE 8 accounts for a whopping 26 percent of web browsers ( http://marketshare.hitslink.com/browser-market-share​ .aspx?qprid=2&qpcustomd=0). Since IE 8 doesn’t understand HTML5 tags, you can’t directly format them with CSS either—you have to turn to a JavaScript workaround as described in the box on page 24. Making Sure Internet Explorer Is Up-to-Date Thanks to Microsoft’s auto-update feature, Windows PCs now update Internet Explorer to the latest version. Windows 7 and 8 users have the latest version of Internet Explorer installed—IE 10 at the time of this writing. IE 10 supports many of the exciting and powerful new HTML5 and CSS3 properties. As you’ll learn in this book, CSS3 provides many exciting design possibilities like drop shadows (page 210), gradient backgrounds (page 259), and rounded corners (page 207), to name a few. 32 CSS3: The Missing Manual Unfortunately, not all Windows users will be able to take advantage of these advancements in web design. As explained in the box on page 32, the widely used Windows XP operating system can run only Internet Explorer 8 or earlier. In fact, IE 8 is still the most common version of Internet Explorer used on the Web. This book will point out CSS properties that won’t work in IE 8, as well as possible workarounds. Making Sure Internet Explorer Is Up-to-Date Because IE 8 is still very popular, you need to keep a couple of things in mind. IE 8 is sort of like a chameleon: It can take on the appearance of a different version. If you’re not careful, it may not display your web pages the way you want it to. For example, and most importantly, you must include a proper doctype. As mentioned in the previous section, without a doctype, browsers switch into quirks mode. Well, when IE 8 goes into quirks mode, it tries to replicate the look of IE 5 (!?). But wait—there’s more! IE 8 can also pretend to be IE 7. When someone viewing your site in IE 8 clicks a “compatibility view” button, IE 8 goes into IE 7 mode, displaying pages without IE 8’s full CSS 2.1 goodness. The same thing happens if Microsoft puts your website onto its Compatibility View List—a list of sites that Microsoft has determined look better in IE 7 than in IE 8. If you’re designing a site using the guidelines in this book, you won’t want IE 8 to act like IE 7…ever. Fortunately, there’s a way to tell IE 8 to stop all this nonsense and just be IE 8. Adding a single META tag to a web page instructs IE 8 to ignore Compatibility View and the Compatibility View List and always display the page by using its most standardscompliant mode: <meta http-equiv="X-UA-Compatible" content="IE=edge" /> Put this instruction in the page’s <head> section (below the <title> tag is a good place). This tag works for current versions of IE, too: The "IE=edge" part of the tag will instruct versions of Internet Explorer later than IE 8 to also display web pages in their standard mode. Unfortunately, you must do this on every page of your site. Now that your HTML ship is steering in the right direction, it’s time to jump into the fun stuff (and the reason you bought this book): Cascading Style Sheets. Chapter 1: HTML for CSS 33 Making Sure Internet Explorer Is Up-to-Date UP TO SPEED Cross-Browser Testing There are a lot of web browsers out there. If you use Windows, you automatically get Internet Explorer and can install additional browsers like Firefox, Safari, Opera, or Google’s Chrome. On a Mac, you can stick with the already-installed Safari browser, or run Firefox or Chrome. While the very latest browsers are mostly comparable when displaying CSS-driven web pages, you can’t say the same for IE 8, which is still in widespread use. both operating systems. It’s the most efficient testing technique for Mac users. Both Boot Camp and virtualization software require a copy of the Windows OS. To really make sure your sites work for the largest audience, you need a way to test your designs in as wide a range of browsers as possible. Here are a few techniques: • CrossBrowserTesting.com ( http://CrossBrowserTesting​ .com) is $30 a month (ouch) for 150 minutes of use and offers an added benefit—interactive testing. You get to see your page running remotely on a PC under your control—you can test features that a screenshot can’t capture, like Flash movie playback, animation, and JavaScript interactions. Windows. Normally, Windows computers can only run one version of IE—you can’t test in IE 6, IE 7, and IE 8 on the same computer. Well, normally you can’t, but you can if you run virtual machines on your Windows computer to create completely separate versions of Internet Explorer that you can then test in. It’s a fair amount of work, but it gives you the most accurate way of testing a page in multiple versions of IE. Visit www.howtogeek.com/102261/how-to-run-internet-explorer7-8-and-9-at-the-same-time-using-virtual-machines to learn more about how to do this. You should also install the other major browsers on your computer: Firefox, Opera, and Chrome. Apple has discontinued Safari for Windows, so you can simply rely on Chrome (which has a similar underlying rendering engine) or use one of the services listed later in this box (like Adobe Browserlab) to test your designs in Safari. Mac. Testing is a bit trickier for Mac people. You have to test in Internet Explorer—it’s still one of the most used browsers in the world, and the lack of most CSS3 properties in IE 8 means that your painstakingly crafted design might look great for you, but fall apart in IE 8. You have a few options: First, you can buy (or borrow) a Windows machine; second, if you have an Intel Mac, you can install Windows using Apple’s Boot Camp software (www.apple.com/macosx/features/bootcamp.html ); a third option is to install virtualization software like VMware Fusion or Parallels Desktop on your Mac. This software lets you run a virtual Windows machine side-by-side with the Mac OS. You can jump between Mac and Windows to test in various browsers in 34 CSS3: The Missing Manual Everyone. Another option that works for both Windows and Macs and doesn’t require installing extra software is one of the many cross-browser testing services that let you see what your pages look like in many different operating systems and browsers. • BrowserStack (www.browserstack.com) is another service that lets you test your pages interactively via your web browser. For $19 a month, it’s like renting multiple Windows and Mac machines with multiple versions of IE, Chrome, Firefox, and Safari installed. • Browsershots ( www.browsershots.org ) is a free alternative that provides screenshots for a wide range of browsers on Windows and Linux. • You can also see what a page looks like in IE 9, 8 7, 6, and even 5.5 for free with NetRenderer ( http://ipinfo​ .info/netrenderer), though. You need to have the pages you want to test up on a publicly accessible website. Visit http://ipinfo.info/netrenderer, type the URL of the page, and in a few moments you’ll see a screen shot. Unfortunately, it doesn’t take a picture of a complete web page—just the top part that you’d normally see on a screen. On the other hand, it’s free. • Finally, you can use Adobe’s BrowserLab tool ( https:// browserlab.adobe.com) to get snapshots of a web page in all major browsers. If you use Dreamweaver to build sites, for example, you can launch BrowserLab from within the program and preview the page you’re currently working on. chapter 2 Creating Styles and Style Sheets Even the most complex and beautiful websites, like the one in Figure 2-1, start with a single CSS style. As you add multiple styles and style sheets, you can develop fully formed websites that inspire designers and amaze visitors. Whether you’re a CSS novice or a Style Sheet Samurai, you need to obey a few basic rules about how to create styles and style sheets. In this chapter, you’ll start at square one, learning the basics of creating and using styles and style sheets. Tip Some people learn better by doing rather than reading. If you’d like to try your hand at creating styles and style sheets first and then come back here to read up on what you just did, turn to page 43 for a hands-on tutorial. Anatomy of a Style A single style defining the look of one element on a page is a pretty basic beast. It’s essentially just a rule that tells a web browser how to format something on a web page—turn a headline blue, draw a red border around a photo, or create a 150-pixel-wide sidebar box to hold a list of links. If a style could talk, it would say something like, “Hey Browser, make this look like that.” A style is, in fact, made up of two elements: the web page element that the browser formats (the selector) and the actual formatting instructions (the declaration block). For example, a selector can be a headline, a paragraph of text, a photo, and so on. Declaration blocks can turn that text blue, add a red border around a paragraph, position the photo in the center of the page—the possibilities are endless. 35 Anatomy of a Style Figure 2-1 Every CSS-driven web page starts with a single CSS style. Here, a basic style (bottom) sets the groundwork for the body of the entire page (top). Note Technical types often follow the lead of the W3C and call CSS styles rules. This book uses the terms “style” and “rule” interchangeably. 36 CSS3: The Missing Manual Of course, CSS styles can’t communicate in nice clear English like the previous paragraph. They have their own language. For example, to set a standard font color and font size for all paragraphs on a web page, you’d write the following: Anatomy of a Style p { color: red; font-size: 1.5em; } This style simply says, “Make the text in all paragraphs—marked with <p> tags—red and 1.5 ems tall.” (An em is a unit of measurement that’s based on a browser’s normal text size. More on that in Chapter 6.) As Figure 2-2 illustrates, even a simple style like this example contains several elements: • Selector. As described earlier, the selector tells a web browser which element or elements on a page to style—like a headline, paragraph, image, or link. In Figure 2-2, the p selector refers to the <p> tag. This selector makes web browsers format all <p> tags by using the formatting directions in this style. With the wide range of selectors that CSS offers and a little creativity, you’ll master your pages’ formatting. (The next chapter covers selectors in depth.) • Declaration Block. The code following the selector includes all the formatting options you want to apply to the selector. The block begins with an opening brace ({) and ends with a closing brace (}). • Declaration. Between the opening and closing braces of a declaration block, you add one or more declarations, or formatting instructions. Every declaration has two parts, a property and a value. A colon separates the property name and its value, and the whole declaration ends with a semicolon. • Property. CSS offers a wide range of formatting options, called properties. A property is a word—or a few hyphenated words—indicating a certain style effect. Most properties have straightforward names like font-size, margin-top, and text-align. For example, the background-color property sets—you guessed it—a background color. You’ll learn about oodles of CSS properties throughout this book. You must add a colon after the property name to separate it from the value. Tip Appendix A has a handy glossary of CSS properties. • Value. Finally, you get to express your creative genius by assigning a value to a CSS property—by making a background blue, red, purple, or chartreuse, for example. As upcoming chapters explain, different CSS properties require specific types of values—a color (like red, or #FF0000), a length (like 18px, 200%, or 5em), a URL (like images/background.gif ), or a specific keyword (like top, center, or bottom). Chapter 2: Creating Styles and Style Sheets 37 Understanding Style Sheets Figure 2-2 A style (or rule) is made of two main parts: a selector, which tells web browsers what to format, and a declaration block, which lists the formatting instructions that the browsers use to style the selector. You don’t need to write a style on a single line, as pictured in Figure 2-2. Many styles have multiple formatting properties, so you can make them easier to read by breaking them up into multiple lines. For example, you may want to put the selector and opening brace on the first line, each declaration on its own line, and the closing brace by itself on the last line, like so: p { color: red; font-size: 1.5em; } Web browsers ignore spaces and tabs, so feel free to add them to make your CSS more readable. For example, it’s helpful to indent properties, with either a tab or a couple of spaces, to visibly separate the selector from the declarations, making it easy to tell which is which. In addition, putting one space between the colon and the property value is optional but adds to the readability of the style. In fact, you can put as much white space between the two as you want. For example, color:red, color: red, and color : red all work. Note Don’t forget to end each property/value pair with a semicolon: color: red; Leaving off that semicolon can trip up browsers, breaking your style sheet and ruining the look of your web page. Don’t worry, this mistake is very common—just make sure you use a CSS validator, as described in the box on page 41. Understanding Style Sheets Of course, a single style won’t transform a web page into a work of art. It may make your paragraphs red, but to infuse your websites with great design, you need many different styles. A collection of CSS styles comprises a style sheet. A style sheet can be one of two types—internal or external, depending on whether the style information is located in the web page itself or in a separate file linked to the web page. 38 CSS3: The Missing Manual Internal or External—How to Choose Most of the time, external style sheets are the way to go, since they make building web pages easier and updating websites faster. An external style sheet collects all your style information in a single file that you then link to a web page with just a single line of code. You can attach the same external style sheet to every page in your website, providing a unified design. It also makes a complete site makeover as easy as editing a single text file. Understanding Style Sheets On the receiving end, external style sheets help web pages load faster. When you use an external style sheet, your web pages can contain only basic HTML—no byte-hogging HTML tables or <font> tags and no internal CSS style code. Furthermore, when a web browser downloads an external style sheet, it stores the file on your visitor’s computer (in a behind-the-scenes folder called a cache) for quick access. When your visitor hops to other pages on the site that use the same external style sheet, there’s no need for the browser to download the style sheet again. The browser simply downloads the requested HTML file and pulls the external style sheet from its cache—a significant savings in download time. Note When you’re working on your website and previewing it in a browser, the cache can work against you. See the box below for a workaround. WORKAROUND WORKSHOP Don’t Get Caught in the Cache A browser’s cache is a great speed-boost for Web surfers. Whenever the cache downloads and stores a frequently used file—like an external CSS file or an image—it saves precious moments traveling the highways of the Internet. Instead of re-downloading the next time it needs the same file, the browser can go straight to the new stuff—like a yet-to-beviewed page or graphic. But what’s good for your visitors isn’t always good for you. Because the web browser caches and recalls downloaded external CSS files, you can often get tripped up as you work on a site design. Say you’re working on a page that uses an external style sheet, and you preview the page in a browser. Something doesn’t look quite right, so you return to your web editor and change the external CSS file. When you return to the web browser and reload the page, the change you just made doesn’t appear! You’ve just been caught by the cache. When you reload a web page, browsers don’t always reload the external style sheet, so you may not be seeing the latest and greatest version of your styles. To get around this snafu, you can force reload a page (which also reloads all linked files) by pressing the Ctrl (c) key and clicking the browser’s Reload button; Ctrl+F5 also works for Chrome and Internet Explorer; Ctrl+Shift+R (c -Shift-R) is Firefox’s keyboard shortcut; and Ctrl+R (c-R) works for both Safari and Chrome. Chapter 2: Creating Styles and Style Sheets 39 Internal Style Sheets Internal Style Sheets An internal style sheet is a collection of styles that’s part of the web page’s code. It always appears between opening and closing HTML <style> tags in the page’s <head> portion. Here’s an example: <style> h1 { color: #FF7643; font-family: Arial; } p { color: red; font-size: 1.5em; } </style> </head> <body> <!-- The rest of your page follows... --> Note You can place the <style> tag and its styles after the <title> tag in the head of the page, but web designers usually place them right before the closing </head> tag as shown here. If you also use JavaScript in your pages, add the JavaScript code after the style sheet. Many JavaScript programs rely on CSS, so by adding your CSS first, you can make sure the JavaScript program has all the information it needs to get its job done. The <style> tag is HTML, not CSS. But its job is to tell the web browser that the information contained within the tags is CSS code and not HTML. Creating an internal style sheet is as simple as typing one or more styles between the <style> tags. Note HTML5 simply requires <style> for the opening tag of an internal style sheet. Earlier versions of HTML and XHTML require an additional attribute on the opening <style> tag: <style type="text/css"> If you leave that attribute off a non-HTML5 page, you’ll get an error if you try to validate the web page. Yet another reason to use HTML5. Internal style sheets are easy to add to a web page and provide an immediate visual boost to your HTML. But they aren’t the most efficient method for designing an entire website composed of many web pages. For one thing, you need to copy and paste the internal style sheet into each page of your site—a time-consuming chore that adds bandwidth-hogging code to each page. But internal style sheets are even more of a hassle when you want to update the look of a site. For example, say you want to change the <h1> tag, which you originally decided should appear as large, green, bold type. But now you want small, 40 CSS3: The Missing Manual blue type in the Courier typeface. Using internal style sheets, you’d need to edit every page. Who has that kind of time? Fortunately, there’s a simple solution to this dilemma—external style sheets. External Style Sheets Note It’s also possible (though rarely advisable) to add styling information to an individual HTML tag without using a style sheet. The tutorial on page 44 shows you how to perform that maneuver by using an inline style. External Style Sheets An external style sheet is nothing more than a text file containing all your CSS rules. It never contains any HTML code—so don’t include the <style> tag. In addition, always end the file name with the extension .css. You can name the file whatever you like, but it pays to be descriptive. Use global.css, site.css, or, simply, styles.css, for example, to indicate a style sheet used by every page on the site, or use form.css to name a file containing styles used to make a web form look good. UP TO SPEED Validate Your CSS Just as you should make sure you’ve correctly written the HTML in your web pages using the W3C HTML validator (see the box on page 27), you should also check your CSS code to make sure it’s kosher. The W3C provides an online tool for CSS checking as well: http://jigsaw.w3.org/css-validator. It operates just like the HTML validator: You can type the URL of a web page (or even just the address to an external CSS file), upload a CSS file, or copy and paste CSS code into a web form and submit it for validation. It’s easy to make a typo when writing CSS, and one small mistake can throw all of your carefully planned designs out of whack. When your CSS-infused web page doesn’t look as you expect, a simple CSS error may be the cause. The W3C CSS validator is a good first stop when troubleshooting your designs. Tip If you have a page with an internal style sheet but want to use an external style sheet, then just cut all of the code between the <style> tags (without the tags themselves). Then create a new text file and paste the CSS into the file. Save the file with a .css extension—styles.css, for example—and link it to your page, using one of the techniques described next. Once you create an external style sheet, you must connect it to the web page you wish to format. You can attach a style sheet to a web page using HTML’s <link> tag or CSS’s own @import directive—a command that basically does the same thing as the link tag. All current web browsers treat these two techniques the same, and both let you attach style sheets to a web page, so choosing one is mostly a matter of preference. Chapter 2: Creating Styles and Style Sheets 41 External Style Sheets Note The @import directive can do one thing the <link> tag can’t: attach external style sheets to an external style sheet. This advanced technique is discussed at the bottom of this page. Linking a Style Sheet by Using HTML The most common method of adding an external style sheet to a web page is to use the HTML <link> tag. You write the tag slightly differently depending on whether you’re using HTML5, HTML 4.01, or XHTML. For example, here’s HTML5: <link rel="stylesheet" href="css/styles.css"> HTML 4.01 is similar, but requires an additional type attribute: <link rel="stylesheet" type="text/css" href="css/styles.css"> Finally, XHTML is just like HTML 4.01, but it requires a closing slash at the end of the tag: <link rel="stylesheet" type="text/css" href="css/styles.css" /> Because HTML5 requires less code, we’d recommend that you use HTML5. All doctypes require two attributes: • rel="stylesheet" indicates the type of link—in this case, a link to a style sheet. • href points to the location of the external CSS file on the site. The value of this property is a URL and will vary depending on where you keep your CSS file. It works the same as the src attribute you use when adding an image to a page or the href attribute of a link pointing to another page. Tip You can attach multiple style sheets to a web page by adding multiple <link> tags, each pointing to a different style sheet file. This technique is a great way to organize your CSS styles, as you can see in Chapter 15. Linking a Style Sheet by Using CSS CSS includes a built-in way to add external style sheets—the @import directive. You add the directive inside of an HTML <style> tag, like so: <style> @import url(css/styles.css); </style> Note If you’re using an HTML 4.01 or XHTML doctype, then you need to add type="text/css" to the opening <style> tag. See the note on page 40. Unlike HTML’s <link> tag, @import is part of the CSS language and has some definite un-HTML-like qualities: 42 CSS3: The Missing Manual • To make the connection to the external CSS file, you use url instead of href and enclose the path in parentheses. So in this example, css/styles.css is the path to the external CSS file. Quotes around the URL are optional, so url(css/ styles.css) and url("css/styles.css") both work. Tutorial: Creating Your First Styles • As with the <link> tag, you can include multiple external style sheets by using more than one @import: <style type="text/css"> @import url(css/styles.css); @import url(css/form.css); </style> • You can add regular CSS styles after the @import directives if you want to create a rule that applies just to that one page, but still use the site’s global design rules to format the rest of the page. Note You’ll learn how rules interact and how you can create a rule that overrides other rules on page 113. You can even create an external CSS file that contains only @import directives linking to other external style sheets, a technique often used to help organize your styles (see page 547). Here’s an example: <style> @import url(css/styles.css); @import url(css/form.css); p { color:red; } </style> You must place all the @import lines before any CSS rules, as shown here. Web browsers ignore any style sheets you import after a CSS rule, so if you reversed the order of the code above and had the p style appear first, then the browser ignores any styles in style.css or form.css style sheets. Which to use? Although both methods work, the <link> tag is more common. In a few cases, using the @import technique can slow your style sheets’ download speed (visit www.stevesouders.com/blog/2009/04/09/dont-use-import to find out when this can happen and why). So, if you don’t have a strong preference for either, just use the <link> method. Tutorial: Creating Your First Styles The rest of this chapter takes you through the basic steps for adding inline styles, writing CSS rules, and creating internal and external style sheets. As you work through this book, you’ll work through various CSS designs, from simple design elements to complete CSS-enabled web page layouts. To get started, download the tutorial files Chapter 2: Creating Styles and Style Sheets 43 Tutorial: Creating Your First Styles located on the book’s companion website at www.sawmac.com/css3. Click the tutorial link and download the zip archive containing the files (detailed instructions for unzipping the files are on the website as well). Each chapter’s files are in a separate folder, named 02 (for Chapter 2), 03 (for Chapter 3), and so on. Next, launch your favorite web page–editing software, whether it’s a simple text editor, like Notepad or TextEdit, or a visually oriented program, like Dreamweaver or Microsoft Expression Web (for information on selecting an editor, see page 7). Creating an Inline Style When you type a CSS rule directly into a page’s HTML, you’re creating an inline style. Inline styles offer none of the time- and bandwidth-saving benefits of style sheets, so the pros hardly ever use them. Still, in a pinch, if you absolutely must change the styling on a single element on a single page, then you may want to resort to an inline style. (For example, when creating HTML-formatted email messages, it’s best to use inline styles. That’s the only way to get CSS to work in Gmail, for instance.) And if you do, you at least want the style to work properly. The important thing is to carefully place the style within the tag you want to format. Here’s an example that shows you exactly how to do it: 1. In your web page–editing program, open the file 02→basic.html. This simple-but-elegant HTML5 file contains a couple of different headings, a few paragraphs, and a copyright notice inside an <address> tag. You’ll start by creating an inline style for the <h1> tag. 2. Click inside the opening <h1> tag and type style="color: #666666;". The tag should look like this: <h1 style="color: #666666;"> The style attribute is HTML, not CSS, so you use the equal sign after it and enclose all of the CSS code inside quotes. Only the stuff inside the quotes is CSS. In this case, you’ve added a property named color—which affects the color of text—and you’ve set that property to #666666 (a hexadecimal code for defining a color that’s gray). You’ll learn more about coloring text on page 154. The colon separates the property name from the property value that you want. Next you’ll check the results in a web browser. 3. Open the basic.html page in a web browser. For example, start up your favorite web browser and choose File→Open File (or press Ctrl+O [c-O] and select the basic.html file in the 02 tutorial folder from your computer; or just drag the file from the desktop—or wherever you’ve 44 CSS3: The Missing Manual saved the tutorial files—into an open browser window). Many HTML editors also include a “Preview in Browser” function, which, with a simple keyboard shortcut or menu option, opens the page in a web browser. It’s worth checking your program’s manual to see if it includes this timesaving feature. Tutorial: Creating Your First Styles When you view the page in a browser, the headline is now gray. Inline styles can include more than one CSS property. You’ll add another property next. 4. Return to your HTML editor, click after the semicolon following #666666, and type font-size: 3em;. The semicolon separates two different property settings. The <h1> tag should look like this: <h1 style="color: #666666; font-size: 3em;"> 5. Preview the page in a web browser. For example, click your browser window’s Reload button (but make sure you’ve saved the HTML file first). The headline is now massive in size. And you’ve had a taste of how labor-intensive inline styles are. Making all the <h1> headings on a page look like this one could take days of typing and add acres of HTML code. 6. Return to your page editor and delete the entire style property, which returns the heading tag back to its normal <h1>. Next, you’ll create a style sheet within a web page. (You’ll find a finished version of this part of the tutorial in the 02_finished folder in a file named inline.html.) Creating an Internal Style Sheet A better approach than inline styles is using a style sheet that contains multiple CSS rules to control multiple elements of a page. In this section, you’ll create a style that affects all top-level headings in one fell swoop. This single rule automatically formats every <h1> tag on the page. 1. With the file basic.html open in your text editor, click directly after the closing tag. Then hit Enter (Return) and type . Now, you’ll add a CSS selector that marks the beginning of your first style. Chapter 2: Creating Styles and Style Sheets 45 Tutorial: Creating Your First Styles 3. Click between the opening and closing 7. Save the page and preview it in a web browser. You can preview the page by opening it in a web browser as described in Step 3 on page 44, or, if the page is still open in a browser window, then just click the Reload button. Next you’ll add another style. Note Always remember to add the closing tag at the end of an internal style sheet. When you don’t, a web browser displays the CSS style code followed by a completely unformatted web page—or no web page at all. 8. Back in your text-editing program, click after the closing brace of the h1 style you just created, press Enter, and then add the following rule: p { color: #616161; line-height: 150%; margin-top: 10px; margin-left: 80px; } This rule formats every paragraph on the page. Don’t worry too much right now about what each of these CSS properties is doing. Later chapters cover these properties in depth. For now, just practice typing the code correctly and get a feel for how to add CSS to a page. 9. Preview the page in a browser. The page is starting to shape up and should look like Figure 2-3. You can see what stylistic direction the page is headed in. You can see a completed version of this tutorial by opening the internal.html file in the 02_finished folder. The process you’ve just worked through is CSS in a nutshell: Start with an HTML page, add a style sheet, and create CSS rules to make the page look great. In the next part of this tutorial, you’ll see how to work more efficiently using external style sheets. Chapter 2: Creating Styles and Style Sheets 47 Tutorial: Creating Your First Styles Figure 2-3 CSS easily formats text in creative ways, letting you change fonts, text colors, font sizes, and a lot more, as you’ll see in Chapter 6. Creating an External Style Sheet Since it groups all of your styles at the top of the page, an internal style sheet is a lot easier to create and maintain than the inline style you created a few pages ago. Also, an internal style sheet lets you format any number of instances of a tag on a page, like every

tag, by typing one simple rule. But an external style sheet gets even better—it can store all of the styles for an entire website. Editing one style in the external style sheet updates the whole site. In this section, you’ll take the styles you created in the previous section and put them in an external style sheet. 1. In your text-editing program, create a new file and save it as styles.css in the same folder as the web page you’ve been working on. External style sheet files end with the extension .css. The file name styles.css indicates that the styles contained in the file apply throughout the site. (But you can use any file name you like, as long as it ends with the .css extension.) Start by adding a new style to the style sheet. 48 CSS3: The Missing Manual 2. Type the following rule into the styles.css file: Tutorial: Creating Your First Styles html { padding-top: 25px; background-image: url(images/bg_page.png); } This rule applies to the HTML tag—the tag that surrounds all other HTML tags on a page. The padding-top property adds space between the top of the tag and the content that goes inside it. In other words, what you just typed will add 25 pixels of space between the top of the browser window and the content inside the page. The background-image adds a background image to the page, The CSS background-image property can display the graphic in many different ways—in this case, the graphic will tile seamlessly from left to right and top to bottom, covering the entire browser window. You can read more about background image properties on page 240. 3. Add a second rule following after the rule you just typed to the styles.css file: body { width: 80%; padding: 20px; margin: 0 auto; box-shadow: 10px 10px 10px rgba(0,0,0,.5); background-color: #E1EDEB; } This rule applies to the body tag—the tag that holds all the content visible in a web browser window. There are a lot of different things going on in this style, and each of these properties is covered in-depth later in the book. But in a nutshell, this style creates a box for the page’s content that’s 80 percent the width of the browser window, has a little bit of space inside that moves text from the edge of the box (that’s the padding property), and centers the box on the page (that’s the margin property, and the particular trick of centering a page’s content is discussed in the tutorial starting on page 89). Finally, the box gets a light blue background color and a transparent black drop shadow. Instead of recreating the work you did earlier, just copy the styles you created in the previous section and paste them into this style sheet. 4. Open the basic.html page that you’ve been working on and copy all of the text inside the . These are the opening and closing style tags—it’s a good idea to type both tags at the same time, so you don’t accidentally forget to add the closing tag. Together, these two tags tell a web browser that the information between them is Cascading Style Sheet instructions. The HTML should now look like this (the stuff you added is in bold): Selector Basics Type selectors—such as the tag selector you’re about to create—are the most basic type of selector. (If you completed the tutorial in the last chapter, you’ve already created a few.) 82 CSS3: The Missing Manual Tutorial: Selector Sampler Figure 3-10 Plain HTML looks cold and monotonous in a web browser. But with a little CSS, you can turn drab (shown here) into fab (Figure 3-11) in 31 easy steps. Note If you’re using HTML 4.01 or XHTML doctypes (instead of HTML5), you’ll need to add the type="text/ css" attribute to the opening style tag like this: —to indicate the end of the style sheet. These tags mark the area where CSS instructions go. Now, you’ll create a style that applies to all

tags. Chapter 4: Saving Time with Style Inheritance 97 Tutorial: Inheritance Note If you’re using HTML 4.01 or XHTML doctypes (instead of HTML5 as in this tutorial) you’ll need to add the type="text/css" attribute to the opening style tag like this: Now that the basic style tags are in place, you’ll add the CSS reset discussed on page 115. Instead of typing it all out, you’ll just copy and paste the CSS from an external style sheet. Chapter 6: Formatting Text 179 Tutorial: Text Formatting in Action 4. Open the file reset.css . Copy all the code from that file and paste it between the opening and closing These two styles add a border to each image in the gallery, and set the font, alignment, and margins of the captions. The first is a descendent selector to target just the images inside the figures. Next, place the photos side by side. 278 CSS3: The Missing Manual Note When you insert the internal style sheet, make sure to place it in the page’s head section, after the link tag and before the IE conditional comment, which attaches the HTML5shiv JavaScript file (page 24). Tutorial: Creating a Photo Gallery 4. Add the following style to the internal style sheet you just created: figure { float: left; width: 210px; margin: 0 10px 10px 10px; } This style floats each photo/caption pair to the left. In effect, it places the photos side-by-side until there’s no more room in the row. The browser then drops the next photos down a row, until all of the photos are displayed one row on top of the next. The width is the total width of the photo plus padding and borders. In this example, it’s 200 pixels for the photo, 8 pixels for left and right padding, and 2 pixels for left and right borders. Note In this fictitious photo gallery, each picture is the same width. In the real world, you may have pictures of varying sizes. See the box below for a trick that lets you arrange rows of pictures of different widths. Using different height images won’t work (as you’ll see in Step 5). When you’ve got images with differing heights, stick with HTML tables. POWER USERS’ CLINIC When One Width Doesn’t Fit All It’s a breeze to set up a photo gallery—like the one in this tutorial—when the photos are conveniently all the same width. But what if you have photos of differing widths? One solution is to create a style for each different width and apply a class to the

tag for the particular image. (That’s tons of work, so it would pay to do some photo editing work to standardize your photos to just a handful of different widths first.) For example:
. Then create a class style, for example .w300, and set the width to the width of the image (in this case, 300) plus 10 to cover the padding and borders: .w300 { width: 310 }. 5. Save the file and preview the gallery.html page in a web browser. It should look like the left image in Figure 8-21. Adjust the width of your browser window to make it thinner and wider and watch how the images reflow into the space. Aha—something’s not quite right. The second row of images has two empty spaces where photos should be. This problem occurs because the caption for the second image on the first line is taller than the other captions on the line. Images that jump down to another row bump into that caption and can’t get by it. (You can read more about this float property snafu on page 425.) Fortunately, there’s a simple fix to this dilemma. Chapter 8: Adding Graphics to Web Pages 279 Tutorial: Creating a Photo Gallery 6. Return to your text editor and the gallery.html file. Locate the figure style. Remove the float:left and add display: inline-block; vertical-align: top. The finished style should look like this: figure { display: inline-block; vertical-align: top; width: 210px; margin: 0 10px 10px 10px; } The display: inline-block property (page 308) treats each image/caption pair as a block (a box with height and width) but also as an inline element (so the blocks can sit side-by-side). In addition, the vertical-align property set to top makes sure that each
tag aligns to the top of all the other
tags in a row. 7. Save the file and preview the page in a web browser. See the right side of Figure 8-21. If you resize the browser window, the gallery reformats itself. With a wider window you can fit four or even five images on a row, but if you make it smaller you’ll see only one or two images per row. Figure 8-21 Floating elements next to each other is one way to simulate the column and row appearance of a table. But it doesn’t work well if the elements are of varying heights (left). Using display: inline-block is another way to force elements to sit side-byside (right) without the nasty dropping behavior pictured in the left image. 280 CSS3: The Missing Manual Adding Drop Shadows Your gallery looks good, but you can make it even more impressive. Adding drop shadows under each photo lends the page an illusion of depth and a realistic 3-D quality. But before you fire up Photoshop, you’ll be glad to know there’s no need to add individual drop shadows. Instead, you can make CSS automatically add a shadow to any image you want. Before CSS3 (and in previous editions of this book), you had to go through an elaborate process to add drop shadows to the images in this gallery, adding extra divs around the images and using two background graphics, and it still didn’t look like a perfect drop shadow. Fortunately CSS3 makes it so easy, you’ll wrap this tutorial up in two steps. Tutorial: Creating a Photo Gallery 1. In a text editor, open the styles.css file you completed in the previous tutorial. You’ll update the img style you created before. 2. Add box-shadow: 2px 2px 4px rgba(0,0,0,.5); at the end of the figure img style so it looks like this (changes in bold): figure img { border: 1px solid #666; background-color: #FFF; padding: 4px; box-shadow: 2px 2px 4px rgba(0,0,0,.5); } You learned about the box-shadow property back on page 210. Here you’ve added a shadow that extends 2 pixels to the right of the image, 2 pixels below the image, and which spreads out 4 pixels. Using an RGBA color (page 155), you can set the shadow to 50 percent transparent black color. 3. Save the file and preview the page. It should look like Figure 8-22. Each image has its own drop shadow, and you didn’t even have to open Photoshop! You can find a completed version of this tutorial in the 08_finished→gallery_ex folder. Chapter 8: Adding Graphics to Web Pages 281 Tutorial: Using Background Images Figure 8-22 Adding drop shadows to photos gives a page a 3-D look and increases the visual appeal of any photo gallery. Fortunately, using CSS you can easily add a drop shadow to any picture without even touching Photoshop. Tutorial: Using Background Images The CSS background-image property is the secret weapon of modern web design. It can turn a ho-hum, text-heavy web page into a dazzling swirl of imagery (see Figure 8-23). Since you can use it to add an image to the background of any HTML tag, the designs you can create are limited only by your imagination. The drop shadow example in the previous tutorial is just one example of creative background image use. Other common background image frills include applying a page background and adding custom bullets to unordered lists. You’ll explore some of these common tasks in this tutorial. 282 CSS3: The Missing Manual Adding an Image to the Page Background Whether it’s an intricate pattern, a logo, or a full-screen photograph, images appear in the background of many web pages. In fact, adding an image to the background of a page is probably the most common application of the background-image property. Tutorial: Using Background Images 1. In your text editor, open the file 08→bg_ex→bg_images.html. This page is a basic two-column layout: a very simple page, with some text formatted on a white background (Figure 8-23, top). To start, you’ll add a background image to the page. The page has an external style sheet with the basic formatting, but to avoid having to wade through all the styles in that file, you’ll add an internal style sheet for the steps in this tutorial. 2. Click between the opening and closing This style is about as generic as it gets. It will apply to all tag on the page. It’s a good place to start, since it sets up the overall look of links for the page. You’ll add more styles that will let you pinpoint links in specific areas of the page. Now, time to remove that boring old underline beneath the link. 4. Add text-decoration: none; to the style you just created. This removes the underline, but also makes the link less visible on the page. Remember: you should always do something to make links stand out and seem clickable to your site’s visitors. 318 CSS3: The Missing Manual Tutorial: Styling Links Figure 9-9 Here’s a basic web page with links in their standard browser configuration—underlined and blue (or purple, if they’re links to previously visited pages). In this case, some links point to other pages on the site, some point to other sites, and one is an email address. In this tutorial, you’ll style each of these links differently. 5. Add font-weight: bold; to the a style. Now links appear in bold (other text may appear bold, too). Next you’ll replace the underline, but you’ll do it a bit more creatively, using a border instead of the text-decoration property. 6. Add a border declaration to the style, so it looks like this: a { color: #207EBF; text-decoration: none; font-weight: bold; border-bottom: 2px solid #F60; } Chapter 9: Sprucing Up Your Site’s Navigation 319 Tutorial: Styling Links The links really stand out, and using a border instead of the normal underline applied to links lets you change the line’s color, size, and style (Figure 9-11, left). Now you’ll change the look of visited links. 7. Add a :visited pseudo-class style for visited links: a:visited { color: #6E97BF; } This style changes the look of visited links to a lighter, grayer shade of the main link color—a subtle way to draw attention away from an already visited page. If you preview the page, click one of the links (try one in the middle part of the page) and then return to the links.html page. You should see the link get lighter in color. You’ll also notice that it stays bold and continues to have the orange underline you assigned to the a style in Step 6. That’s the cascade in action (Chapter 5)—the a:visited style is more specific than a plain a selector, so its color property overrides the color assigned by the a style. Time to take it a step further by adding a rollover effect, so the link’s background changes color when the mouse moves over it. 8. Add a :hover pseudo-class style to the style sheet: a:hover { color: #FFF; background-color: #6E97BF; border-bottom-color: #6E97BF; } This pseudo-class applies only when the mouse is over the link. The interactive quality of rollovers lets visitors know the link does something (Figure 9-10). Figure 9-10 With a couple of styles, you can change the look of any link. With the :hover pseudo-class, you can even switch to a different style when the mouse moves over the link. 320 CSS3: The Missing Manual Adding a Background Image to a Link The email link at the bottom of the page looks no different than the other links on the page (Figure 9-11, top). You have other plans for that mailto link, however. Since it points to an email address, clicking it doesn’t take a visitor to another page, but instead launches an email program. To provide a visual cue emphasizing this point, you’ll add a cute little email icon. Tutorial: Styling Links 1. Add a descendent selector to the internal style sheet of the links.html file: a[href^="mailto:"] { color: #666666; border: none; background: url(images/email.gif) no-repeat left center; } This is an advanced attribute selector, which selects any links that begin with mailto,: (in other words, it selects email links). The border: none setting removes the underline defined by the a style you created in Step 6—you’re going for a subtle look here. The background property adds an image on the left edge of the link. Finally, the no-repeat value forces the graphic to appear just a single time. Trouble is, the graphic lies directly underneath the link, so it’s hard to read the text (circled in the middle image in Figure 9-11). 2. Add 20 pixels of left padding to the attribute style you just created: padding-left: 20px; Remember that padding adjusts the space between content and its border. So adding some left padding moves the text over 20 pixels but leaves the background in place. One last touch: move the entire link a little away from the copyright notice. 3. Add 10 pixels of left margin to the style, so it finally ends up like this: a[href^="mailto:"] { color: #666666; border: none; background: url(images/email.gif) no-repeat left center; padding-left: 20px; margin-left: 10px; } This small visual adjustment makes it clear that the icon is related to the link and not part of the copyright notice (Figure 9-11, bottom). Chapter 9: Sprucing Up Your Site’s Navigation 321 Tutorial: Styling Links Figure 9-11 Just a few subtle touches can help make a link’s purpose obvious. In this case, a plain link (top) becomes clearly identifiable as an email link (bottom). Highlighting Different Links At times you may want to indicate that a link points to another website. In this way, you can give your visitors a visual clue that there’s additional information elsewhere on the Internet or warn them that they’ll exit your site if they click the link. Also, you may want to identify links that point to downloadable files or other non-web-page documents. On the web page you’re working on, the right-hand “Resources” sidebar contains different types of links that you’ll highlight with icons—a different icon for each type of link. First, you’ll set up a basic style that applies to all of those links. 1. Add this style to the links.html internal style sheet: .resources a { border-bottom: none; } Since all of the links you want to format are inside a
with the class resources, the descendent selector .resources a targets just those links. This style gets rid of the underline that the generic link style added. 322 CSS3: The Missing Manual Next, you’ll add an icon next to external links. 2. Add another style at the end of the links.html internal style sheet: Tutorial: Styling Links .resources a[href^='http://'] { background: url(images/globe.png) no-repeat right top; } This is a descendent selector style that uses the advanced attribute selector discussed on page 317. Basically, it targets any link that begins with http:// (but only those that are also inside the element with the class resources). As with the email link style you created earlier, this style adds a background image. It places the image at the right side of the link. However, this style has a similar problem as the email link style—the image sits underneath the link’s text. Fortunately, the solution is the same—just add some padding to move the image out of the way of the text. In this case, though, instead of adding left padding, you’ll add right padding (since the icon appears on the right side of the link). In addition, since every link in the resources box will have a similarly sized icon, you can save some code by adding the padding to the .resources a style you created in Step 1. 3. Edit the .resources a style so that it looks like this: .resources a { border-bottom: none; padding-right: 22px; } If you save the page and preview it in a web browser, you’ll see small globe icons to the right of the bottom two links in the sidebar. Time to format the other links. 4. Add three more styles to the internal style sheet: .resources a[href$='.pdf'] { background: url(images/acrobat.png) no-repeat right top; } .resources a[href$='.zip'] { background: url(images/zip.png) no-repeat right top; } .resources a[href$='.doc'] { background: url(images/word.png) no-repeat right top; } These three styles look at how the href attribute ends; identifies links to either Adobe Acrobat files (.pdf), zip archives (.zip), or Word documents (.doc); and assigns a different icon in each case. Chapter 9: Sprucing Up Your Site’s Navigation 323 Tutorial: Creating a Navigation Bar 5. Finally, add a hover state for the resources links: .resources a:hover { color: #000; background-color: rgba(255,255,255,.8); } This style both changes the color of the text and adds a background color (see Figure 9-12). You can find a finished version of this tutorial in the 09_finished/links/links​ .html file. Figure 9-12 Using advanced attribute selectors, you can easily identify and style different types of links—external links, links to PDF files, Word docs, and zip files. Tutorial: Creating a Navigation Bar In this exercise, you’ll turn a plain old list of links into a spectacular navigation bar, complete with rollover effects and a “You are here” button effect. 1. In a text editor, open 09→nav_bar→nav_bar.html. As you can see, there’s not much to this file yet. There’s an internal style sheet with the basic reset styles discussed on page 115, and one rule setting up some basic properties for the tag. The HTML consists of an unordered list with six links. It looks like example #1 in Figure 9-13. Your first step is to add some HTML so you can target your CSS to format the links in this list. 2. Locate the opening
    tag and add class="mainNav" to it, so it looks like this:
tag to 0, browsers still display double borders. That is, the bottom border of one cell will appear above the top border of the cell below, causing a doubling of border lines. Setting the border-collapse property to collapse eliminates both the space between cells and this doubling up of borderlines (page 381). This property works only when applied to a
tag. • Values: collapse, separate • Example: border-collapse: collapse; border-spacing Sets the amount of space between cells in a table. It replaces the
tag’s cellspacing HTML attribute. However, Internet Explorer 7 and earlier doesn’t understand the border-spacing property, so it’s best to continue to use the cellspacing attribute in your
tags to guarantee space between cells in all browsers. Note If you want to eliminate the space browsers normally insert between cells, just set the bordercollapse property to collapse. • Values: Two CSS length values. The first sets the horizontal separation (the space on either side of each cell), and the second sets the vertical separation (the space separating the bottom of one cell from the top of the one below it). • Example: border-spacing: 0 10px; caption-side When applied to a table caption, this property determines whether the caption appears at the top or bottom of the table. (Since, according to HTML rules, the
tag must immediately follow the opening tag, a caption would normally appear at the top of the table.) • Values: top, bottom • Example: caption-side: bottom; Note Unfortunately, this property has no effect in Internet Explorer 6 or 7 (it works in IE 8), so it’s safest to stick with the HTML equivalent: . The hide value prevents any part of the cell from being displayed. Instead, only an empty placeholder appears, so borders, background colors, and background images don’t show up in an emptied cell. Apply this property to a style formatting the
or . empty-cells Determines how a browser should display a table cell that’s completely empty, which in HTML would look like this:
tag. 594 CSS3: The Missing Manual Miscellaneous Properties • Values: show, hide • Example: empty-cells: show; Note The empty-cells property has no effect in Internet Explorer 7 and earlier. table-layout Controls how a web browser draws a table and can slightly affect the speed at which the browser displays it. The fixed setting forces the browser to render all columns the same width as the columns in the first row, which (for complicated technical reasons) draws tables faster. The auto value is the normal “browser just do your thing” value, so if you’re happy with how quickly your tables appear on a page, don’t bother with this property. If you use it, apply table-layout to a style formatting the
tag. • Values: auto, fixed • Example: table-layout: fixed; Miscellaneous Properties CSS 2.1 offers a few additional—and sometimes interesting—properties. They let you enhance your web pages with special content and cursors, offer more control over how a page prints, and so on. (Unfortunately, browser understanding of these properties is spotty at best.) content Specifies text that appears either before or after an element. Use this property with the :after or :before pseudo-elements. You can add an opening quotation mark in front of quoted material and a closing quotation after the quote. Internet Explorer 6 and 7 don’t understand this property, so its use is limited. • Values: Text inside of quotes "like this", the keywords normal, open-quote, close-quote, no-open-quote, no-close-quote. You can also use the value of an HTML attribute. • Examples: p.advert:before { content: "And now a word from our sponsor…"; } a:after { content: " (" attr(href) ") "; } Note Adding text in this way (like the opening and closing quote example) is called generated content. Read a simple explanation of the generated content phenomenon at www.westciv.com/style_master/academy/ css_tutorial/advanced/generated_content.html. For a deeper explanation, visit www.w3.org/TR/CSS21/generate. html. Appendix A: CSS Property Reference 595 Miscellaneous Properties cursor Lets you change the look of the mouse pointer when it moves over a particular element. You can make a question mark appear next to the cursor when someone mouses over a link that provides more information on a subject (like a word definition). • Values: auto, default, crosshair, pointer, move, e-resize, ne-resize, nwresize, n-resize, se-resize, sw-resize, s-resize, w-resize, text, wait, help, progress. You can also use a URL value to use your own graphic as a cursor (see the Note below). The look of a cursor when mousing over a link is pointer, so if you want to make some element on the page display the “click me” icon, you can add the declaration cursor: pointer to the style. • Example: cursor: help; cursor: url(images/cursor.cur); Note Not all browsers recognize URL cursor values. For more information, visit www.quirksmode.org/css/ cursor.html. opacity Lets you control the transparency of any element and all of its descendents. In this way, you can have underlying colors, images, and content show through an element. Note that if you apply opacity to a div, all of the items inside that div—headlines, images, paragraphs, and other divs—will share the same opacity level. In other words, if you set a
tag to .5 opacity (50% transparent), an image inside that div will also be 50 percent transparent—even explicitly setting the opacity of that image to 1 won’t override the 50 percent transparency. • Values: A decimal value from 0 to 1. 0 is invisible, 1 is completely opaque. • Example: opacity: .5; orphans Specifies the minimum number of lines of text that can be left at the bottom of a printed page. Suppose you’re printing your web page on a laser printer, and a fiveline paragraph is split between two pages with just one line at the bottom of page one, and the four remaining lines at the top of page two. Because a single line all by itself looks odd (sort of like a lost orphan—get it?), you can tell the browser to break a paragraph only if at least, say, three lines are left on the bottom of the page. (At this writing, only the Opera browser understands this property.) • Values: A number like 1, 2, 3, or 5. • Example: orphans: 3; 596 CSS3: The Missing Manual page-break-after Determines whether a page break (in printing) occurs after a particular element. With it, you can make sure that a particular paragraph is always the last item to appear on a printed page. Miscellaneous Properties • Values: auto, always, avoid, left, right. Auto represents the normal value and lets the browser determine when and how to break content across printed pages. Always forces the element that follows to appear at the top of a separate printed page, and it’s the only value that works consistently across browsers. Avoid prevents a page break after an element; it’s a great way to keep a headline with the paragraph that follows it, but unfortunately, most browsers don’t understand it. Left and right determine whether the element following appears on a left- or right-handed page, which may force the browser to print an extra empty page. But since no browsers understand these values, don’t worry about wasting paper. Browsers treat left and right the same as always. • Example: page-break-after: always; page-break-before Works like page-break-after, except the page break appears before the styled element, placing it at the top of the next printed page. You can use this property to make sure headlines for different sections of a long web page each appear at the top of a page. • Values: Same as page-break-after. • Example: page-break-before: always; page-break-inside Prevents an element from being split across two printed pages. If you want to keep a photo and its caption together on a single page, wrap the photo and caption text in a
tag, and then apply a style with page-break-inside to that
. (At this writing, only Opera understands this property.) • Values: avoid • Example: page-break-inside: avoid; widows The opposite of orphans, it specifies the minimum number of lines that must appear at the top of a printed page. Say the printer can manage to fit four out of five lines of a paragraph at the bottom of a page and has to move the last line to the top of the next page. Since that line might look weird all by itself, use widows to make the browser move at least two or three (or whatever number of) lines together to the top of a printed page. (Only Opera understands this property, so it’s of limited use.) • Values: A number like 1, 2, 3 or 5. • Example: widows: 3; Appendix A: CSS Property Reference 597 Appendix B CSS Resources N o one book—not even this one—can answer all of your CSS questions. Luckily, CSS resources abound for both beginning and expert web designers. In this appendix, you’ll find resources to help you with general CSS concepts as well as specific CSS tasks, like building a navigation bar or laying out a web page. References References that cover CSS properties range from the official to the obscure. There are websites and online tutorials, of course, but you don’t have to be on the Web to learn about CSS. Some of these guides come on good old-fashioned paper. World Wide Web Consortium (W3C) • CSS Current Work (www.w3.org/Style/CSS/current-work). Here you’ll find all of the CSS specifications, including the newest additions. The different specifications are organized by status, like Completed, Revising, or Abandoned. You can click any specification to dig into the nitty gritty details, but just because a specification is listed as “completed” doesn’t necessarily mean it’s been fully implemented in all web browsers. However, this site does provide the final (sometimes overly complex and hard-to-understand) word on CSS. Books • Cascading Style Sheets: The Definitive Guide by Eric Meyer (O’Reilly). For comprehensive technical (yet readable) coverage of CSS, check out this guide. 599 CSS Help Other Online References • CSS Reference at the Mozilla Developer Network (https//developer.mozilla. org/en-US/docs/CSS/CSS_Reference). The Mozilla Developer Network (MDN) provides one of the most comprehensive references to CSS (as well as HTML5, JavaScript, and other web technologies). • The Sitepoint CSS Reference (http://reference.sitepoint.com/css) provides a clear, designer-oriented reference to CSS properties and concepts. It’s a good place to turn if the W3C or MDN docs cause your eyes to glaze over. • Can I use... (http://caniuse.com). This frequently updated site provides detailed information on CSS and browser compatibility. Here, you’ll be able to determine whether a particular CSS property works in Internet Explorer 9, for example. • CSS3 Files (http://www.css3files.com/) provides very good instruction and even better demonstrations of popular CSS3 properties like animation, shadows, gradients, and more. CSS Help Even with the best references (like this book), sometimes you need to ask an expert. You can join a discussion list, where CSS-heads answer questions by email, or peruse a wealth of information in an online forum. Discussion Boards • CSSCreator Forum (www.csscreator.com/css-forum). A very active online forum offering help and advice for everything from basic CSS to advanced layout. • SitePoint.com’s CSS Forum (http://www.sitepoint.com/forums/forumdisplay. php?53-CSS-amp-Page-Layout). Another helpful group of CSS addicts. • CSS-Tricks.com Forum (http://css-tricks.com/forums). A relative newcomer, this small forum holds some good information. (If you like PHP and JavaScript, there’s some good discussion on those topics here as well.) CSS Tips, Tricks, and Advice The Web makes it easy for anyone to become a publisher. That’s the good news. The bad news is, when everyone’s a publisher, it’s harder to sort through all the chaff to find the golden wheat of clear, concise, and accurate information. There’s plenty of good CSS information on the Web—and a lot that’s not good. Here are a few of the best destinations for CSS information: • CSS-Tricks.com (http://css-tricks.com). This one-man blog is full of great CSS tips. You’ll find frequently updated tips and tricks as well as comprehensive video tutorials. 600 CSS3: The Missing Manual • Sitepoint (www.sitepoint.com/tags/css). Lots of articles and tutorials on CSS techniques. You’ll often find the latest news and information about CSS here, too. CSS Navigation • Smashing Magazine (www.smashingmagazine.com/tag/css). Smashing Magazine gathers some of the best resources on the Web, and in the CSS category, you’ll find a nearly endless number of links highlighting some of the most creative thinking on CSS and web design. • NetMagazine (http://www.netmagazine.com/tag/css?ct=tutorial). NetMagazine often has interesting and useful CSS tutorials. CSS Navigation Chapter 9 shows you how to create navigation buttons for your website from scratch. But online tutorials are a great way to solidify your knowledge. Also, once you understand the process in detail, you don’t have to do it yourself every single time. On the Web, you can find examples of navigation features for inspiration. Tutorials • Listutorial (http://css.maxdesign.com.au/listutorial). Step-by-step tutorials on building navigation systems from unordered lists. • 40 Premium CSS Menu and Navigation Solutions (www.tripwiremagazine. com/2012/06/css-menu-and-navigation.html). More tutorials than you can shake a stick at. • The Apple.com navigation menu created using only CSS3 (www.marcofolio. net/css/the_apple.com_navigation_menu_created_using_only_css3.html). If you like the simple, clean appearance of Apple.com, you might be interested in how to create their menu with CSS. Online Examples • CSS Navigation Bar Code Generator (http://lab.mattvarone.com/navbar). Feeling lazy? Let this online tool create all the code you need to use the CSS sprite method described on page 314. • CSS Menu Maker (http://cssmenumaker.com). Choose from a selection of fancy CSS3-rich navigation bars. You can customize them to fit your site, and then download the code. • CSS Showcase (www.alvit.de/css-showcase). A gallery of navigation menus, tabs, and CSS navigation techniques. • Listamatic (http://css.maxdesign.com.au/listamatic). Showcase of CSS-based navigation systems. Also lots of links to related websites. • Listamatic2 (http://css.maxdesign.com.au/listamatic2). More CSS menus, including nested lists with submenus. Appendix B: CSS Resources 601 CSS Layout • CSS Menu Builder (www.cssmenubuilder.com/). Lots of cool menus, many useful techniques. A must see. • Responsive CSS Navigation Menu (http://pixelsdaily.com/resources/html-css/ responsive-css-navigation-menu/). A responsive menu that changes from a horizontal bar, to a two-row bar, to a two-column stack of buttons. CSS Layout CSS layout is so flexible, you could spend a lifetime exploring the possibilities. And some people seem to be doing just that. You can gain from their labors by reading articles, checking out online examples, and experimenting with tools that can do some of the CSS work for you. Box Model Information • Interactive CSS Box Model (www.redmelon.net/tstme/box_model). Fun, interactive tool for visualizing the box model. • Paul Irish’s * { box-sizing: border-box } FTW blog post (http://paulirish. com/2012/box-sizing-border-box-ftw/). An influential blog post that champions abandoning the old CSS box model and using the box-sizing property as a more sane way to keep track of element heights and widths. Layout Examples • PageBlox (www.pageblox.com). Provides an online tool for generating a CSSbased responsive design. It takes care of browser bugs and generates the media queries for large and small screens. • CSS Layout Generator (www.pagecolumn.com). Pick the number of columns, tweak a few knobs, and this website generates all the HTML and CSS required. With a site like this, who needs a book? (Just kidding!) • Even More Layout Generators (www.webdesignbooth.com/15-extremelyuseful-css-grid-layout-generator-for-web-designers). If you can’t get enough of websites that automatically create your CSS and HTML, you’ll find a list of 15 different online tools. • 960 Grid System (http://960.gs). One of the better CSS frameworks that provides a set of basic styles and a technique for using divs and class names to create complex, multicolumn, fixed-width layouts. (You can find a detailed video introduction to this system at http://nettuts.com/videos/screencasts/adetailed-look-at-the-960-css-framework/). There’s even an online tool to help you generate your own grid based on the 960 grid system: http://grids.heroku. com. 602 CSS3: The Missing Manual • YUI Grids CSS (http://yuilibrary.com/yui/docs/cssgrids/). Yahoo’s very own CSS-layout system. It’s a bit techy, but like the 960 Grid System, provides a basic framework for building complicated multicolumn layouts. Showcase Sites • Twitter Bootstrap (http://twitter.github.com/bootstrap/). A complete website toolkit, this site includes HTML, CSS, and JavaScript components that make it easy to build a complete, responsive, grid-based page, with some fancy JavaScript thrown in. • Foundation (http://foundation.zurb.com). Another take on the complete website toolkit. Very much like Twitter bootstrap, Foundation includes HTML, CSS, and JavaScript. It has excellent documentation and is relatively easy to learn. Miscellaneous Layout Resources • Adaptive CSS Layouts (www.smashingmagazine.com/2009/06/09/smartfixes-for-fluid-layouts). Provides many resources for building flexible layouts that adapt to the full width of the browser window. • One clean HTML markup, many layouts ( http://tjkdesign.com/articles/ one_html_markup_many_css_layouts.asp). Great blog post that takes a single HTML page and demonstrates eight different ways to lay it out with just CSS. • Variable fixed width layout (www.clagnut.com/blog/1663). Short blog post about a technique for adjusting the number of columns on a page, based on the width of the browser window. • 3-Column Layout Index ( http://css-discuss.incutio.com/?page=Three ColumnLayouts). A nearly exhaustive (or at least exhausting) list of different three-column layouts. Showcase Sites Knowing the CSS standard inside out is no help when your imagination is running dry. A great source of inspiration is the creative work of others. There are more CSS showcase sites than you can shake a search engine at, so here’s a handful of sites where you can appreciate and study beautiful CSS designs. • CSS ZenGarden (www.csszengarden.com). The mother of all CSS showcase sites: many different designs for the exact same HTML. • CSS Beauty (www.cssbeauty.com). A wonderful gallery of inspirational CSS designs. • CSS Elite (www.csselite.com). “Showcasing the best in CSS web design”…of course they all say that. • CSS Mania (http://cssmania.com). Yet another showcase site, whose ungrammatical claim to fame is “Since March 2004, the most updated CSS showcase all over the globe.” Appendix B: CSS Resources 603 CSS Books • CSS Winner (www.csswinner.com). Highlights a new “winner” everyday. It’s unclear if the judges are really just teenagers surfing the Web from their bedrooms, but the site does highlight some very nicely designed sites. CSS Books Hey, not even this book can tell you everything there is to know about CSS! • Handcrafted CSS: More Bulletproof Web Design by Dan Cederholm (New Riders). A bit dated, but this book has some great design and very good thinking about how to craft HTML for formatting with CSS. • Bulletproof Web Design by Dan Cederholm (New Riders). A classic, updated for HTML5 and CSS3, covers how best to create CSS styles that can withstand the pressure of visitors changing text sizes, resizing their browser windows, and the general instability of the browser environment. Great tips on building layouts, navigation bars, and more. • Head First HTML with CSS & XHTML by Elisabeth Freeman and Eric Freeman (O’Reilly). A lively, highly illustrated introduction to websites integrating HTML and CSS. • Stunning CSS3: A Project-Based Guide to the Latest in CSS by Zoe Mickley Gillenwater (New Riders). Provides many good tips and techniques for using CSS3 to create attractive and flexible web pages. CSS Software There are lots of different ways to create Cascading Style Sheets. Keeping it simple, you can stick with the free text editors that come with Windows and Mac OS, like Notepad or TextEdit. There are also CSS-only editors and full-fledged web page– development programs like Dreamweaver that include CSS creation tools. Windows and Mac • Style Master (www.westciv.com/style_master/product_info). This powerful CSS editor with a long history includes many tools, including simple wizards to get you started, sample templates, tutorials, and a complete CSS guide. • Dreamweaver (www.adobe.com/dreamweaver). Definitely not just for CSS, this premium web-development tool includes everything you need to build complete websites. Visual editing tools make it easier to see the effect of CSS on your web pages as you work. • Sublime Text (http://www.sublimetext.com). This powerful code editor is useful for creating a wide variety of text documents. While not specifically a CSS tool, it’s a popular tool among web developers. 604 CSS3: The Missing Manual Windows Only • Top Style (www.topstyle4.com). The venerable CSS editor that also lets you edit your HTML documents—a one-stop shop for web page building. Includes many tools to increase your productivity. There’s also a free “lite” version. CSS Software • Microsoft Expression Web (www.microsoft.com/expression/products/Web_ Overview.aspx). A complete website-construction tool that works very well with CSS. Mac Only • Espresso (http://www.macrabbit.com/espresso/). This all-in-one web design tool includes a code editor, an FTP program, and CSSEdit 3, a powerful, yet simple CSS editor. • Code (http://panic.com/coda/ ). Another all-in-one web-building tool with built-in CSS-editing tools. Appendix B: CSS Resources 605 Index Symbols 3D transforms, 345 40 Premium CSS Menu and Navigation Solutions, 601 960 Grid System, 602 2011 BeerCamp website, 345 $= (“ends in”) selector, 317 $= (“ends with”) selector, 74 /* and */, for comments, 539 > (angle bracket), for child relationship, 74 – (“begins with”) selector, 73, 316 and :not() selector, 81 { } (braces), 37 for internal style, 46 *= (contains) attribute selector, 81 statement, 559 / (forward slash), for closing tags, 4 # (hash symbol), for hex numbers, 564

Navigation menu