You are on page 1of 53

CSS CSS3 20 lessons to successful

web development 1st Edition Nixon


Visit to download the full and correct content document:
https://textbookfull.com/product/css-css3-20-lessons-to-successful-web-development-
1st-edition-nixon/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

HTML5 20 Lessons to Successful Web Development 1st


Edition Robin Nixon

https://textbookfull.com/product/html5-20-lessons-to-successful-
web-development-1st-edition-robin-nixon/

Modern CSS: Master the Key Concepts of CSS for Modern


Web Development Joe Attardi

https://textbookfull.com/product/modern-css-master-the-key-
concepts-of-css-for-modern-web-development-joe-attardi/

Modern CSS: Master the Key Concepts of CSS for Modern


Web Development Joe Attardi

https://textbookfull.com/product/modern-css-master-the-key-
concepts-of-css-for-modern-web-development-joe-attardi-2/

Learning PHP MySQL JavaScript With jQuery CSS HTML5 5th


Edition Robin Nixon

https://textbookfull.com/product/learning-php-mysql-javascript-
with-jquery-css-html5-5th-edition-robin-nixon/
Beginning Unreal Game Development: Foundation for
Simple to Complex Games Using Unreal Engine 4 1st
Edition David Nixon [David Nixon]

https://textbookfull.com/product/beginning-unreal-game-
development-foundation-for-simple-to-complex-games-using-unreal-
engine-4-1st-edition-david-nixon-david-nixon/

Web Development for beginners Learn HTML CSS Javascript


step by step with this Coding Guide Programming Guide
for beginners Website development 1st Edition Mastery

https://textbookfull.com/product/web-development-for-beginners-
learn-html-css-javascript-step-by-step-with-this-coding-guide-
programming-guide-for-beginners-website-development-1st-edition-
mastery/

Practical Web Design learn the fundamentals of web


design with HTML5 CSS3 Bootstrap jQuery and Vue js 1st
Edition Philippe Hong

https://textbookfull.com/product/practical-web-design-learn-the-
fundamentals-of-web-design-with-html5-css3-bootstrap-jquery-and-
vue-js-1st-edition-philippe-hong/

Basics of Web Design: HTML5 & CSS, 6th Edition Terry


Felke-Morris

https://textbookfull.com/product/basics-of-web-design-
html5-css-6th-edition-terry-felke-morris/

Grid Layout in CSS Interface Layout for the Web 1st


Edition Eric A. Meyer

https://textbookfull.com/product/grid-layout-in-css-interface-
layout-for-the-web-1st-edition-eric-a-meyer/
CSS & CSS3
20 Lessons to Successful Web Development
About the Author
Robin Nixon is a prolific author on programming and web development (as well as
psychology and motivation), with his books having been translated into numerous
foreign languages—frequently topping the US and international computer book charts.
He has worked with computers and technology for all his life, and began writing on
the subject about 35 years ago.
He has authored hundreds of articles, and over two dozen books, and is a popular
video and online instructor, with thousands of students taking his courses. Robin
is also an accomplished programmer, developer, and entrepreneur, with several
successful Internet startups to his name, from which he has learned a wealth of
programming hints and tips, which he enjoys passing on in his expanding range of
web development books, including the following titles:

• HTML5: 20 Lessons to Successful Web Development (McGraw-Hill Education, 2015)


• JavaScript: 20 Lessons to Successful Web Development (McGraw-Hill Education, 2015)
• PHP: 20 Lessons to Successful Web Development (McGraw-Hill Education, 2015)
• Learning PHP, MySQL, JavaScript, CSS & HTML5 (O’Reilly, 2014)
• Web Developer's Cookbook (McGraw-Hill Education, 2012)
• HTML5 for iOS and Android (McGraw-Hill Education, 2010)

About the Technical Editor


Albert Wiersch has been writing software since the Commodore VIC-20 and Commodore
64 days in the early 1980s. He holds a Bachelor of Science degree in Computer Science
Engineering and an MBA from the University of Texas at Arlington. Albert currently
develops and sells software that helps web developers, educators, students, businesses,
and government agencies check their HTML & CSS documents and their websites
for quality problems, including many SEO (search engine optimization), mobility,
and accessibility issues, with discounts made available to students. His website is at
HTMLValidator.com.
CSS & CSS3
20 Lessons to Successful Web Development

Robin Nixon

New York Chicago San Francisco


Athens London Madrid Mexico City
Milan New Delhi Singapore Sydney Toronto
Copyright © 2015 by McGraw-Hill Education. All rights reserved. Except as permitted under the United States Copyright Act of 1976,
no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system,
without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a
computer system, but they may not be reproduced for publication.

ISBN: 978-0-07-185021-6

MHID: 0-07-185021-X

The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-184996-8,
MHID: 0-07-184996-3.

eBook conversion by codeMantra


Version 1.0

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked
name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the
trademark. Where such designations appear in this book, they have been printed with initial caps.

McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in corporate
training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com.

Information has been obtained by McGraw-Hill Education from sources believed to be reliable. However, because of the possibility of
human or mechanical error by our sources, McGraw-Hill Education, or others, McGraw-Hill Education does not guarantee the accuracy,
adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of
such information.

TERMS OF USE

This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work is subject
to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may
not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate,
sell, publish or sublicense the work or any part of it without McGraw-Hill Education’s prior consent. You may use the work for your own
noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail
to comply with these terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES OR
WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM
USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK
OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill Education and
its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be
uninterrupted or error free. Neither McGraw-Hill Education nor its licensors shall be liable to you or anyone else for any inaccuracy, error
or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill Education has no responsibility for the
content of any information accessed through the work. Under no circumstances shall McGraw-Hill Education and/or its licensors be liable
for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even
if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever
whether such claim or cause arises in contract, tort or otherwise.
To Julie
This page intentionally left blank
Contents at a Glance

PART I Basic CSS


1 Introduction to CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Learning the CSS Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Applying Declarations to IDs and Classes . . . . . . . . . . . . . . . . . . . . . . . . 19
4 Accessing Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5 Working with the Cascade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6 Selecting Fonts and Typography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7 Manipulating Color and Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
8 Handling Pseudo-Selectors and Using Shorthand Properties . . . . . . . . . 87
9 Understanding the Box Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
PART II CSS3
10 Introduction to CSS3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11 Using Selectors and Attribute Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . 133
12 Setting Backgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
13 Attaching Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
14 Controlling Box Shadows, Overflow, and Columns . . . . . . . . . . . . . . . . . 165
15 Adding Colors and Opacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
16 Creating Text Effects and Changing the Box Model . . . . . . . . . . . . . . . . . 189
17 Linking to Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
18 Making 2D Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
19 Applying Specific Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
20 Directing 3D Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
A Answers to the Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

vii
This page intentionally left blank
Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

PART I Basic CSS

LESSON 1 Introduction to CSS ................................. 3


How the Document Object Model Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Correct HTML Structure and Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
About Cascading Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Importing a Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Importing CSS from Within HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Embedded Style Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Using IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

LESSON 2 Learning the CSS Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11


Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Multiple Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Style Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Default Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
User Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
External Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Internal Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Inline Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
It’s All About Separation of Style and Content . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

ix
x Contents

LESSON 3 Applying Declarations to IDs and Classes . . . . . . . . . . . . . . . . . . 19


Using IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
A Specific Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
IDs Are Single-Use Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Assigning Multiple Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Accessing IDs and Classes with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The O() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The S() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The C() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using Hyphenated Property Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Why Use JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

LESSON 4 Accessing Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31


The Type Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Descendant Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
The Child Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
The ID Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Narrowing ID Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The Class Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Narrowing Class Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
The Attribute Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
The Universal Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Selecting by Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
For JavaScript Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Points of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
LESSON 5 Working with the Cascade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Style Sheet Creators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Style Sheet Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Style Sheet Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Calculating Specificity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
When Rules Are Particularly Important . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
So What Are Divs and Spans Anyway? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Specifying Measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Inches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Centimeters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Millimeters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Contents xi

Picas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Ems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Exs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Percent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

LESSON 6 Selecting Fonts and Typography . . . . . . . . . . . . . . . . . . . . . . . . . 55


Font Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Generic Font Families . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Font Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Font Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Font Weight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Changing Text Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Text Decoration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Line, Word, and Letter Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Horizontal Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Managing Uppercase and Lowercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Text Indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

LESSON 7 Manipulating Color and Position . . . . . . . . . . . . . . . . . . . . . . . . 67


Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Using rgb() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Short Color Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Linear Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Radial Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Controlling Color Stops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Repeating Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
An Example Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Alpha Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
An Example Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
CSS3 Browser Incompatibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Older Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Let George Do It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The prefixfree.js Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Positioning Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Absolute Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Relative Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Fixed Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
An Example Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
xii Contents

LESSON 8 Handling Pseudo-Selectors and Using Shorthand Properties . . . . . 87


Introducing the Pseudo-Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Link-Related Pseudo-Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Accessing Elements Numerically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Selecting Elements by Numeric Location . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Relational Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Miscellaneous Pseudo-Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Pseudo-Elements That Add Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Shorthand Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Border and Outline Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Background Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Font Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Margin and Padding Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
List Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

LESSON 9 Understanding the Box Model . . . . . . . . . . . . . . . . . . . . . . . . . 107


Visualizing the Box Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Setting Margins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Using Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Applying Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Non-image Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The Object Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Changing the Box Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

PART II CSS3
LESSON 10 Introduction to CSS3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Attribute Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Improved Backgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Enhanced Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Box Shadows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Element Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Multicolumn Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Extended Color Handling and Opacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Additional Text Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Box Sizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Resizing and Outlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Transitions and Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Contents xiii

LESSON 11 Using Selectors and Attribute Selectors . . . . . . . . . . . . . . . . . 133


Summary of Selector Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
The Universal Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Type Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Class Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
ID Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Descendant Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Child Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Adjacent Sibling Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
General Sibling Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Attribute Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Pseudo-Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Pseudo-Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Attribute Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
The ^= Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
The $= Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
The *= Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

LESSON 12 Setting Backgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141


The background-clip Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
The background-origin Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Compatibility Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
The background-size Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Using the auto Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Multiple Backgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
LESSON 13 Attaching Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The border-color Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The border-style Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
The border-image Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
The border-radius Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
LESSON 14 Controlling Box Shadows, Overflow, and Columns . . . . . . . . . . . 165
Adding Box Shadows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The box-shadow Supported Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Managing Element Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Flowing Text over Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
xiv Contents

LESSON 15 Adding Colors and Opacity . . . . . . . . . . . . . . . . . . . . . . . . . . . 177


HSL Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
HSLA Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
RGB Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
RGBA Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
The opacity Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

LESSON 16 Creating Text Effects and Changing the Box Model . . . . . . . . . . 189
The text-shadow Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
The text-shadow Supported Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
The text-overflow Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
The word-wrap Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The box-sizing Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Browser Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The resize Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
The outline and outline-offset Properties . . . . . . . . . . . . . . . . . . . . . . . . 200
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

LESSON 17 Linking to Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203


Loading a Web Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
The Main Types of Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Loading a Web Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
And the Winner Is… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Google Web Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Browser Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Other Font Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
LESSON 18 Making 2D Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . 211
2D Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
The none Transformation Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
The matrix(w,s1,s2,h,x,y) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Argument 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Argument 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Argument 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Argument 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Argument 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Argument 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Internet Explorer Prior to IE9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Contents xv

LESSON 19 Applying Specific Transformations . . . . . . . . . . . . . . . . . . . . . 225


The translate(x,y) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The scale(x,y) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Older Versions of IE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
The rotate(n) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Older Versions of IE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The skew(x,y) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Older Versions of IE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

LESSON 20 Directing 3D Transformations . . . . . . . . . . . . . . . . . . . . . . . . . 237


The perspective Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
The transform-origin Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
About the Three Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
The translate3d(x,y,z) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
The scale3d(x,y,z) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
The rotate3d(x,y,z,r) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The backface-visibility Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
The transform-style Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The transition-property Property . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
The transition-duration Property . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
The transition-delay Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
The transition-timing-function Property . . . . . . . . . . . . . . . . . . . . 253
The cubic-bezier() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Shorthand transition Property Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Self-Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
APPENDIX Answers to the Self-Test Questions . . . . . . . . . . . . . . . . . . . . . 257
Lesson 1 Answers ................................................. 257
Lesson 2 Answers ................................................. 258
Lesson 3 Answers ................................................. 258
Lesson 4 Answers ................................................. 259
Lesson 5 Answers ................................................. 260
Lesson 6 Answers ................................................. 261
Lesson 7 Answers ................................................. 262
Lesson 8 Answers ................................................. 262
Lesson 9 Answers ................................................. 263
Lesson 10 Answers ................................................. 264
Lesson 11 Answers ................................................. 265
Lesson 12 Answers ................................................. 266
Lesson 13 Answers ................................................. 266
Lesson 14 Answers ................................................. 267
xvi Contents

Lesson 15 Answers ................................................. 268


Lesson 16 Answers ................................................. 269
Lesson 17 Answers ................................................. 269
Lesson 18 Answers ................................................. 270
Lesson 19 Answers ................................................. 271
Lesson 20 Answers ................................................. 272

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Acknowledgments
Once again I would like to thank the amazing team at McGraw-Hill Education, with
whom it is always a real pleasure to work on new book projects. In particular, I would
like to thank my Sponsoring Editor Brandi Shailer, Amanda Russell for overseeing the
project’s development, Editorial Supervisor Jody McKenzie, Production Supervisor
Jean Bodeaux, Copy Editor Megha Saini, and Jeff Weeks for the excellent cover design.
Thanks also goes again to Albert Wiersch (whom I have had the pleasure of working
with on a number of occasions) for his meticulous eye for detail during technical review.

xvii
This page intentionally left blank
Introduction

Why This Book?


The concept for this book grew out of Robin’s extremely popular online courses in
which thousands of students are enrolled. From their feedback it became evident that
the reason for this popularity was that students love the way the material is broken
down into easy-to-digest lessons, each of which can be completed in an hour or less.
They also like the no-nonsense, jargon-free, and friendly writing style.
Now, working together, Robin and McGraw-Hill Education have further revised,
updated, and developed his CSS & CSS3 course into this book, which not only will
teach you everything you need to learn in 20 lessons (of less than an hour each),
it also includes an average 15-minute detailed video walk-through for each lesson—
comprising 5 hours of footage in total.
Watch the videos after reading the lesson to reinforce key concepts, or use the
video as a primer to working through each print lesson. Used together, these course
materials make learning CSS & CSS3 easier than it has ever been, and is the ideal way
for you to add these essential skills to your web development toolkit.

To view the accompanying video for this lesson, please visit mhprofessional.com/
nixoncss/.

Who Should Read This Book?


Each chapter is laid out in a straightforward and logical manner as a lesson, with
plenty of examples written using simple and clear CSS. Before moving on to each
subsequent lesson, you have the opportunity to test your new knowledge with a set
of 10 questions about what you have just learned. You can also work along with every
lesson by watching its accompanying video tutorial.

xix
xx Introduction

Even if you have never used any CSS before, you will still learn everything you
need from this book, because the first section provides a thorough grounding in what
CSS is, what it can do for you, and how to use it. And if you have used CSS before, it
will act as a great revision source to ensure you are fully up-to-date before moving on
to learning all the latest additions to CSS3.
Between the lessons, the self-test questions, and the videos, this course will ensure
that you become expert at CSS very quickly.
To save you typing them in, all the example files from the book are saved in a freely
downloadable zip file available at the companion website: 20lessons.com.

What This Book Covers?


This book covers every aspect of CSS, starting with basic syntax and language rules,
such as where and how you include CSS in your web documents. Then the differences
between styling elements by type, class, and ID are explained, along with how to refine
the elements to which CSS will be supplied using selectors.
The term cascade (in cascading style sheets) is fully explained, as is how to create
professional results with fonts and typography, and how to lay out compelling pages.
All the CSS and CSS3 selectors are detailed, along with the new CSS3 ways to manage
colors, backgrounds, borders, opacity, and more. How to transform and animate
elements in 2D and 3D is also explained, with simple examples that you can easily
incorporate in your own websites.
By the time you finish the book’s 20 lessons, you’ll have a thorough grounding
in CSS, and be able to use it to ensure your web pages look as good as possible.

How to Use This Book?


This book has been written in a logical order so that each lesson builds on information
learned in the previous ones. You should begin at Lesson 1 and then work sequentially
through the book, proceeding to the next lesson only when you can correctly answer
the self-test questions in the previous one.
If you already use CSS, you may wish to just browse through the first few lessons
before tucking into the CSS3 section.
Lessons should take you less than an hour to finish, including viewing the
accompanying video walk-through provided with each one. With over 5 hours of
video in total, that’s an average of 15 minutes dedicated to each lesson.
Introduction xxi

How Is This Book Organized?


The first section of this book covers everything you need to know as a newcomer to
CSS, including these lessons: Introduction to CSS, Learning the CSS Rules, Applying
Declarations to IDs and Classes, Accessing Selectors, Working with the Cascade, Selecting
Fonts and Typography, Manipulating Color and Position, Handling Pseudo-Selectors
and Using Shorthand Properties, and Understanding the Box Model.
The second section moves on to explaining everything that’s new in CSS3, including
these lessons: Introduction to CSS3, Using Selectors and Attribute Selectors, Setting
Backgrounds, Attaching Borders, Controlling Box Shadows, Overflow, and Columns,
Adding Colors and Opacity, Creating Text Effects and Changing the Box Model, Linking
to Web Fonts, Making 2D Transformations, Applying Specific Transformations, and
Directing 3D Transformations. The appendix lists all the answers to the self-test questions
in each chapter.
This page intentionally left blank
PART I
Basic CSS
This page intentionally left blank
LESSON

1
Introduction to CSS

To view the accompanying video for this lesson, please visit mhprofessional.com/
nixoncss/.

W hen CSS was invented it was based around a Document Object Model (DOM),
a means of separating out all the different elements within a web page into
discrete objects, each with its own properties and values. This led logically to the
introduction of style sheets, enabling the content of a web page to be completely
separated from its styling; it also made HTML documents easily modifiable by
languages such as JavaScript to provide dynamic user interaction.
Because web pages use a DOM, it is easy for you to style every aspect of it using
CSS. For example, each heading will be within pairs of tags such as <h1> and </h1>,
and a single CSS declaration can set the styling of all such occurrences within a
document, changing the font used, its size, any font decoration, and so on.
This lets you completely change the design of a page without altering the HTML.
Some style settings can even apply dynamic effects to page elements, such as changing
their color and other properties when the mouse passes over them, or create transition
effects by using proprietary browser extensions.

The example files from this book are in a file you can download at 20lessons.com.
Note The files for this lesson are saved in it as embeddedstyles.htm, example.htm,
importedstyles.htm, importedstyles2.htm, styles.css, styletag.htm, usingclasses.htm,
and usingids.htm.

How the Document Object Model Works


The DOM separates different parts of an HTML document into a hierarchy of objects,
each one having its own properties. The term property is used for referring to an
attribute of an object, such as the HTML it contains, its width and height, and so on.

3
4 Part I Basic CSS

The outermost object possible is the window object, which is the current browser
window, tab, iframe, or popped-up window. Underneath this is the document object,
of which there can be more than one (such as several documents loaded into different
iframes within a page). And inside a document there are other objects such as the
head and body of a page.
Within the head there can be other objects such as the title and meta objects,
while the body object can contain numerous other objects, including headings,
anchors, forms, and so forth. For example, Figure 1-1 shows a representation of the
DOM of an example document, with the document shown inside the outer window,
and having the title Hello, a <meta> tag in the head, and three HTML elements
(a link, a form, and an image) in the body section.
Of course, even the simplest of web pages has more structure than is shown here,
but it serves to illustrate how the DOM works; starting from the very outside is the
window, inside which there’s a single document, and within the document are the
various elements or objects, which connect to each other.
In the figure, values are shown with a darker background and in italics. For example,
the value robots is the value of the property name, which is a property of <meta>,
and so on. Although it isn’t shown in the figure, the <meta> tag should have another
matching property called content, which would contain a string specifying which
robots (web crawlers) may access the web page.
Other properties are href, which has a value of http://google.com (and is
itself a property of <meta>, and so on), and <title>, which has the value Hello.
All the other items are objects or object argument names. If the figure were to extend
farther down and sideways, other objects and properties attached to the ones shown
would come into view. A couple of the places where these would appear are shown by
unconnected dotted lines.

window

head head

meta title a form img

name Hello href input name

robots http://google.com type

FIGURE 1-1 Example of a DOM showing head and body sections


Lesson 1 Introduction to CSS 5

In HTML tags you can generally use the single or double quotation marks
Note interchangeably. Therefore <a href="http://google.com"> is equivalent
to <a href='http://google.com'>. Wherever possible, though, I tend to
use single quotes because they don’t require pressing the Shift key to type them
in. Also there are sometimes occasions when you need two levels of nested quotes,
where I would then choose double quotation marks for the outer string, and then
apply single quotes within it, like this: <p style="font-family:'Times
New Roman';">.

Representing this as HTML, the structure of the head section looks like this:
<head>
<meta name='robots' content='index, follow'>
<title>Hello</title>
</head>
And the body section of HTML might look like this:
<body>
<img src='/images/welcome.jpg'>
<a href='http://google.com'>Visit Google</a>
or enter your username and password to continue...
<form id='login' method='post' action='login.php'>
<input type='text' name='name'>
<input type='password' name='password'>
<input type='submit'>
</form>
</body>

Remembering that these two sections of HTML are part of the same document,
we would bring them both together inside an <html> tag (preceded by a !DOCTYPE
declaration), like this:
<!DOCTYPE html>
<html>
<head>
<meta name='robots' content='index, follow'>
<title>Hello</title>
</head>
<body>
<img src='/images/welcome.jpg'>
<a href=http://google.com>Visit Google</a>,
or enter your username and password to continue...
<form id='login' method='post' action='login.php'>
<input type='text' name='name'>
<input type='password' name='password'>
<input type='submit'>
</form>
</body>
</html>
6 Part I Basic CSS

Of course, all web pages are different, but they will usually follow this same
form.

Correct HTML Structure and Nesting


To follow recommended HTML structure and to ensure your documents are readable
by the maximum number of browsers and other clients, attribute values within
tags should be contained in either single or double quotation marks like this:
<a href='http://yahoo.com'>, even though nearly all browsers allow you to
omit them, like this: <a href=http://yahoo.com>.
You should also close (end) every tag, and do so in the correct order. For example,
you shouldn’t close a document by issuing </html> followed by </body> because
the proper nesting of tags would be broken by this reversal. The correct way to close
a document is with </body>, followed by </html>.

About Cascading Style Sheets


Using CSS you can apply styles to your web pages to make them look exactly how you
want. This works because CSS is connected to the DOM so that you can quickly and
easily restyle any element. For example, if you don’t like the default look of the <h1>,
<h2>, and other heading tags, you can assign new styles to override the default
settings for the font family and size used, or whether bold or italics should be set, and
many more properties too.
One way you can add styling to a web page is by inserting the required CSS into
the head of a web page between the <head> and </head> tags. So, to change the
style of the <h1> tag you might use the following CSS:
<style>
h1 {
color :olive;
font-size :18pt;
font-family:'Times New Roman';
}
</style>

FIGURE 1-2 A simple document with a CSS-styled heading


Another random document with
no related content on Scribd:
producing a lovely colored light. These glow-worms are somewhat
rare in this country, but are common in England, where our fire-fly is
unknown. The glow-worms of tropical countries are as large as
good-sized caterpillars, and give out a light of corresponding size.
Some of the beetles of tropical countries are much more radiant
than the glow-worms. They have a bright ring around their bodies,
which sheds such a light that it is said that the negroes use them for
lamps. These negroes we are told by travelers make small round
cages of thin wooden slats placed near each other, and closed at the
top and bottom. In these they put several beetles, and thus light up
their rooms free of cost.

THE GREAT LANTERN FLY.


But the most splendidly illuminated insect in the world is the great
lantern fly. Its monstrous head is a blaze of light. When it flies
through the air it is like a streamer of fire. When it alights upon a leaf,
with its beautiful wings outstretched, and its head gleaming with star-
like rays, that light up everything for some little distance, it is a
gorgeous sight indeed.
But, although this insect is so bright-headed it is not so wise as
some others whose heads are not brilliant at all. Perhaps this head is
like some shops we have seen, where pretty much all the furniture is
on the outside.
OWLS ON A FROLIC.

THE OWLS UPSETTING THE LAMP.

The owls are abroad on a mad carouse,


Waking the echoes far and wide;
They whirl in a crowd through the ruined church,
Or up to the belfry glide.

The little screech-owl makes a horrid din;


While the great white owl looks wise;
And the horned owl nods his head, and blinks;
As around the lamp he flies.

The lamp is a cup, half filled with oil,


That swings from a broken beam;
And, over the traveler sleeping below,
It throws but a dusky gleam.

The owls have no fear of the burning wick—


’Tis only a cotton loop—
They’re after the oil in the swinging cup,
And down on its brim they swoop.

The weary traveler, sound asleep,


Hears naught of the noise o’erhead,
A rickety chair as a bedstead serves,
His overcoat is his bed.

With the sweep of the wings the lamp upsets,


While the gurgling oil o’erflows
With a drip, and a rush, on the great owl’s tail,
A splash on the traveler’s nose.

He’s up in a trice, and, seizing a broom,


He arms himself for a fight.
But all is still in the ruined church;
For the owls are out—and his light.
COMMON AND UNCOMMON SPONGES.

A FINE SPONGE.
They are all wonderful enough, no matter how common they may
be. It takes thousands and thousands of minute creatures, to make a
sponge, and these creatures are so little understood that about all
we know of them is that they must belong to the very lowest order of
animal life, and that they do build sponges. That is not much to
know, but it is not long since the sponge was first known to be an
animal production at all, and our scientific men may yet find out
something more definite about these curious little architects.
Perhaps they may have lately found out something, and I have not
heard of it. This would be the least wonderful thing about sponges.
The ordinary form of the sponge is familiar to nearly everybody
who has ever been washed, and this picture gives a good idea of a
fine large one, as it is found growing at the bottom of the sea. I say
growing, because it seems to be growing there, like a vegetable. But
it does not grow, in the ordinary sense of the word, any more than a
wasp’s nest grows.
But there are sponges with which we are not at all familiar, and
which are curious, apart from the manner of their construction. Such
a one is the sponge called the “Cup of Neptune.”
This is several feet high, and is formed like a great goblet. It would
make a very good cup for Neptune, if he drank brandy or rum, for it
would soak up all that he poured into it, and he could not get a drop,
unless he squeezed his cup pretty hard—and even then the liquor
might all run out of the bottom.
As a rule, civilized and well educated people are more easily
surprised and astonished at uncommon and wonderful works of
nature than uncivilized or ignorant people, for the latter do not know
enough to be astonished. They see nothing strange in the
development of a plant from its seed—nothing grand in a high
mountain, nor anything very beautiful in a flower. They look at these
things as a child looks at his hand. The hand is a very curiously
constructed instrument, full of intricate mechanism, but the child
does not know or think of that. It is not until he grows older and his
mind is cultured that he appreciates the wonderful construction and
the varied action of his hand.
So it is with savages. They do not comprehend that many strange
works of nature are worthy of admiration, and they take it for granted
that things are as they are because they ought to be, just as they
think of their own bodies, if they think of them at all.
THE CUP OF NEPTUNE.
But this great goblet-like sponge is strange enough to astonish
even a savage.
MAGHAR’S LEAP.

It chanced upon a time, a very great many years ago, while fairies
and magicians still dwelt upon earth, that a youth and maiden—
brother and sister—were walking in a forest, talking about their
recent misfortunes, and laying plans for their future. The youth was
clad in armor, according to the warlike fashion of those times. But he
had under his arm a book, which was not in accordance with the
fashion of those times. The maiden wore a dress of some coarse
woolen stuff; and, in her hands she held a sheet of parchment, and a
pen.
Suddenly there broke into their quiet talk the sound of clashing
arms, and the mad plunging of horses. Sybil, the maiden, stopped
terrified.
“Oh!” she cried, “it is the noise of battle! Too well I know those
sounds. Let us go quickly back!”
“Let us go forward a little way,” said Maghar, the youth, “to yon
opening in the woods. Or, stop here, if you fear, and I will go alone
and look out.”
“No,” said Sybil, “if you go I will follow.”
Together they looked out upon the open plain. Two hostile armies
had met unexpectedly, and a fierce conflict had commenced.
“Alas!” said Sybil, shuddering. “There are the savage infidels that
laid waste our home!”
“Yes,” said Maghar, excitedly, “and here, on this side, are our
countrymen, and neighbors! I must bear a hand in this fight!”
“And leave me alone!” cried Sybil. “I have only you left! Your single
arm will not count for much in a battle!”
“It would be a shame to me,” said Maghar, “to sneak off, like a
coward, and leave our friends and Christian soldiers, when their
forces are few, and every warrior counts. Have I not my armor? I
shall find shield and spear on the battle-field on some poor fellow
who has already fallen in the fray. Do not fear, sister! Go back to
Christern’s cottage. There you will be safe; and I will return in a few
hours.”
So saying, he led Sybil back into the forest to the path leading to
Christern’s cottage; gave the book into her hands; and, kissing her
good-bye, he ran out of the woods as fast as the weight of his armor
would allow.

SYBIL’S WATCH.

But Sybil did not return to the cottage. She was too anxious about
her brother; and, going to the entrance of the wood, she crouched
among the trees, where she was hidden from view, and watched the
progress of the fight. She was ready to fly if the tide of battle brought
the armies too near. But they seemed to be gradually moving away
from her. She soon singled out her brother. He had secured a spear
and shield, and mounted a riderless horse. In a few minutes he was
lost in the throng, and she saw him no more.
Her mind was filled with sad forebodings. This Infidel army had
invaded the country, and laid it waste; had killed her parents, and
overthrown, and utterly ruined the beautiful castle that had been her
home. A few things had been saved by old Christern, a much loved
servant of the family, and these constituted the property of Maghar
and Sybil. Old Christern’s cottage, in the depths of the forest, was
the refuge of the orphans. There they had lived for several weeks,
and no way of retrieving their fortunes seemed open to them.
Maghar was a fine scholar. His father had had him taught to read his
own language and Latin, and to write a very beautiful hand. That was
the extent of his knowledge; and it was a great deal at a time when
very few of the richest people knew their letters.
And now, in their poverty, there seemed to be very little use for his
learning. Nobody cared anything about it. He might copy manuscript
for some learned man, and get a living this way, for printing and
paper had not then been invented; and all books were written on
parchment. But Maghar had a contempt for a clerk, as he called a
copyist, and did not fancy this method of supporting his sister and
himself. Nevertheless, the two were that day on their way to the
abode of a great and learned man to see if he wished anything of
this kind done; and if he would buy their only book—a Latin volume,
written on parchment, and beautifully illuminated and bound in
wooden covers.
Sybil went over these things in her mind as she watched the
battle, trying, in vain, to distinguish the form of her brother. She soon
saw to her dismay, that the Infidel forces had turned the flank of the
Christian army, and that the ranks of the latter were broken, and they
were retreating, closely followed by their enemies. She stood up
now, and strained her eyes to watch them until they had all
disappeared over the crest of a hill. Then she sadly returned to
Christern’s cottage to tell the old man of this new and terrible
misfortune.
Days passed away, and Maghar did not return. Christern learned
that the Christian army was broken, and the soldiers scattered.
Some had returned to their homes. The wounded were cared for
among their friends. The dead were buried. But Maghar was with
none of these. No one could tell anything about him, except that he
had fought bravely.
Then Sybil determined to seek out the great and learned man to
whom Maghar had intended to offer his services as copyist. She was
somewhat afraid of him, for he was known to be a powerful
magician. But he could, no doubt, tell her the fate of Maghar, and
she would try to overcome her fears.
She took off the coarse peasant’s dress she had been wearing,
and arrayed herself in her best robe of fine white cashmere, which
was one of the things that Christern had managed to save. She
loosened her beautiful hair, which fell nearly to her feet. This last she
did to show the deep sorrow she was in. She also took with her the
Latin volume, as a present, to propitiate the powerful magician.
The great man lived in the simplest manner in a rocky cavern.
Sybil found him outside his dwelling, seated on a mossy stone,
sorting some plants that lay in his lap. He did not look up as she
approached, and she had a good opportunity to study his
countenance, which was so sweet and gentle that her fear of him
vanished; and she came forward quite boldly, greeted him, and
presented her book.
But the magician waved the volume aside. “I know why you seek
me, sister of Maghar,” he said, kindly.
“Oh, can you tell me aught of my brother?” cried Sybil.
“I know not where he is. The oracles would not enlighten me
without your presence. Come into my dwelling, and we will consult
them.”
So saying he conducted her into his cave through a low, dark
passage way. Great was Sybil’s astonishment when she found
herself in a vast room, with a lofty ceiling. Around the circular walls
was a continuous row of lamps, kept constantly burning. Their light
was reflected from myriads of stalactites that hung from the roof,
glowing with all the colors of the rainbow, making the rough, rocky
chamber as brilliant and gorgeous as a fairy palace. In the centre of
the room stood a brazier, filled with burning coals, and near it, a
large iron harp, with silver strings, and a sort of cupboard, made of
iron. A few rough couches were scattered around. And this was all
the furniture the room contained.
The magician invited Sybil to take a seat. He then proceeded to
place on his head a crown, woven of vines of magical virtues. He
took from the cupboard some singular-looking vessels, and mixed in
them various powders and liquids. Then, pouring all their contents
into a copper pot, he placed it on the coals, seated himself on a
stone near it, drew his harp in front of him, and motioned to Sybil to
stand before it. He looked so pleasantly upon her she did not feel
afraid, but her heart beat fast, not knowing what fearful thing she
might see.
She saw nothing whatever but the harp, and the old man; for, as
soon as the clouds of fragrant white smoke that poured out from the
brazier, had completely enveloped the two, the magician swept his
fingers over his harp, and began to sing. Then Sybil forgot
everything else, for his chant was of Maghar.
SYBIL AND THE MAGICIAN.
He sang of the great deeds Maghar had done in the battle, and
how he had made himself famous. He was the last prisoner taken by
the Infidels; and was now confined in a castle several leagues
distant. The Infidel army was there encamped. They would like to
slay Maghar outright, but were afraid of the vengeance of the
Christian armies near them if they murdered a man held in such
esteem. He was at present undisturbed, but the probability was that,
after a time, they would decide to starve him to death, and give out
word that he had died from sickness. His sister had thus a little time
in which to work to save him.
Here the song ended, and the weeping girl begged the great
magician to save her brother. This he said was not in his power. She
must find a good fairy, and make it her friend. The small creature
could get into the castle, see her brother, and, together, they could
devise a way of escape. He might, perhaps, be able to help them
then. He told her what roads to follow to reach the castle; and,
assuring her that such a good girl would surely find a good fairy to
assist her in her trouble, he dismissed her with his blessing.
That very day Christern and Sybil set out for the castle. They
reached the place after three days’ journey. They told no one what
their errand was in that part of the country; and there were so many
homeless people in the land that their appearance excited no
surprise. Christern soon found employment among the wood-cutters,
and fitted up a deserted hut as a temporary dwelling.
But though they could, every day, look upon the walls of the castle
in which Maghar was confined, they seemed no nearer to him than
before. He was in the hands of the cruel infidels, and where were
there any fairies? There were plenty in that part of the country, the
wood-cutters said, which, at first, was encouraging. But, on inquiry, it
turned out that not one of them had ever seen a fairy, or knew
anybody who ever had seen one. Sybil was in despair as the days
went by, and she blamed her friend, the magician, that he had given
her no help, after all.
She often walked through the woods, near nightfall, to meet
Christern. One evening, as the two were returning together to their
hut, they saw a large wild boar approaching, followed by several
young ones. As this creature is very savage when it has its young to
defend, Christern and Sybil thought it wise to step aside among the
trees, and leave the path to the boar and its interesting family. After
these had passed they continued their way, but had not gone far
when they saw a young boar lying in the path. Christern stooped
over to examine it.
THE BOAR FAMILY.

“It got in with that litter,” said he, “and did not belong to it, so the
old boar has gored it badly. But it is not dead. I’ll take it home, make
a sty for it, and, if it lives, I’ll fatten it, and kill it when it is fit for
eating.”
The wounded animal lifted an appealing glance to Sybil. Its eyes
wore an almost human expression of suffering, and a most
beseeching plea for help. The girl’s heart was touched.
“It is not badly hurt,” she said. “Its flesh is torn, but if I wash its
wounds, and bind them up, and find a nice place in the woods,
where I can make it comfortable, and feed it, it will get well. It is a
free, wild creature, and must not be shut up in a close sty. Think of
my dear brother shut up when he wants to be free!”
Christern thought Sybil’s plan a foolish one, but this last argument
silenced him. He had not a word to say in reply. So the girl washed
off the blood from the boar’s wounds with her fine cambric
handkerchief, which she then tore into strips to bind them up. She
found, in a secluded place, a soft cushion of moss on which she laid
him, and partly covered him with leaves to keep him warm. She then
brought from the hut some of her own scanty supper, and gave it to
the little boar.
After this she visited her patient two or three times a day, nursing
and feeding him. But, on the afternoon of the fourth day, he had
disappeared, and Sybil returned to the hut feeling quite lonely at the
loss of the little creature that had been so glad to see her.
The next night, as Christern was returning late from his work,
trudging slowly through the forest, with his lantern swinging in his
hand, and his wallet slung over his back on the end of his walking
stick, something brushed close by the old man’s ear with a buzzing
of tiny wings.
“That dragon-fly is out late,” said the old man to himself.
Very soon the wings brushed by him again with a louder whizzing.
“It is a bat!” said the old man, shaking his head. “Shoo! shoo!” But
the third time the whirring wings flew almost into his face.
“Good evening, old Christern!” said a tiny voice, such as might
come from a humming-bird, if it could speak.
The startled old man stopped and flashed the light of his lantern
around among the trees. And there, with wee wings outspread, was
a fairy skimming through the air! Christern had never seen a fairy,
but he knew this was one as soon as he saw him. And a jolly,
rollicking fellow he was!
CHRISTERN AND THE FAIRY.

“You don’t know me, old fellow?” said the fairy.


Christern shook his head.
“Wanted to shut me up in a sty, and fatten me, eh? I wouldn’t be
much of a mouthful now, would I? Don’t you wish you could get me?”
And the saucy fellow soared high up among the trees.
Christern nearly dropped his lantern in his astonishment. “You
don’t ever mean to tell me that boars are fairies?” he said, at last.
“I mean to say nothing of the kind!” cried the fairy, indignantly.
“Your horrid, beastly boars are no relations of ours, even! I’ll tell you
how it was,” he said, coming nearer Christern, and speaking in a
confidential tone. “Our fairies all have wings, and can fly, but there
are other kinds without wings. Some of these are good, but some are
bad, and they are full of spite against us because we are better off
than they. I offended a tribe of these not long ago, and they had
influence with a wicked old witch who changed me into a little boar. I
was to remain in that shape for a week. She would have made the
time longer, if she could. But they all thought I would be killed in that
time. And so I should have been but for your Sybil. And there was
another thing worse than death. If I was deprived of my liberty during
that week, I could never again regain my natural shape. So, if you
had put me in your sty, I would have been eaten up one of these
days as a boar. From this awful fate your Sybil saved me. So I am
doubly indebted to her, and I want to do something for her.”
“Oh, you are the good fairy, who is to save our Maghar!” cried the
old man, joyfully.
Thereupon he related the whole sad story, and the fairy told him
he would consult with his tribe that night; and, if he and Sybil would
come to that spot on the following night he would let them know what
could be done.
Sybil’s delight was unbounded. She now felt sure that her brother
would be saved. But, nevertheless she accompanied Christern to the
place of meeting, half fearing that the frisky fairy would play her
some trick. But he was there, before them, and had dressed himself
in his best suit of green in honor of the occasion.
As soon as they appeared he began chattering as fast as ever he
could.
“We fairies have hit upon a splendid plan,” he said. “But there is no
time to lose. Sybil, I have seen your brother, but he did not see me. I
was at the castle this morning before cock-crow. I flew in through a
loop-hole. Nobody saw me. It took me a long time to find out in what
room your brother was kept, but, at last, I made it out. I intended to
stay until I did. He is in a room, high up in the north tower. He has
been pretty well, but now his jailers have begun the plan of starving
him; and he will soon be too weak to save himself as we propose,
which is the only way open to him. It requires steady nerves, and
great courage. But do not weep, for we will save him, only it must be
done speedily. Do you, Christern, be ready to go with me to the
castle at break of day. Pretend you are a beggar. There are so many
of these you will pass unsuspected. I will point out to you a small
postern door at the back of the castle, stay about that; and I will hide
near it. I could slip inside easily enough, and tell Maghar what to do,
but he does not know me, and would not trust me. So you must get
inside the castle some way and see him. And, not only that, but you
must get out again. And this is our plan for doing this. We fairies
have three magical cocks. At a signal from me these cocks will
appear on the crest of the hill at the back of the castle, and will sing
a song. This will so astonish the sentinels that they will be thrown off
their guard. I will then slip in through a loop-hole, unlock the postern
door, and let you in. We will tell Maghar how he can escape. Then
the cocks will appear again, and while the attention of the guards is
distracted, we will get out of the castle. Remember now to be here at
daybreak.”
And the fairy disappeared, much to Sybil’s regret, who had a
hundred questions to ask him about her brother’s appearance, and
treatment. He had not even told her what his plan was for her
brother’s escape. But he did not come back, and she was obliged to
be satisfied with the information she had.
The programme was carried out in every particular. Christern
acted his part of beggar so well that he managed to get near the
postern door, unsuspected, with the fairy snugly tucked into a fold of
his ragged dress. On arriving at the place the fairy concealed himself
in some vines. At the appointed signal three magnificent cocks
appeared abreast on the top of the hill.
THREE MAGICAL COCKS.

The like of these cocks had never been seen in that country, and
they immediately attracted the attention of everybody. But when they
opened their mouths, and began to sing the words of a war song, the
sentinels forgot everything, and deserted their posts to get as near
the wonderful songsters as possible without alarming them.

You might also like