CSS Cheat Sheet
CSS Cheat Sheet
com
CONTENTS INCLUDE:
n
Element Selectors
n
ID Selectors
n
Descendent Selectors
n
Attribute Selectors
n
Combining Selectors
n
Hot Tips and more...
Just in case youve not read Core CSS: Part I, Ill briey review
the purpose of a CSS selector. A selector in a style sheet signals
the browser to nd matches within those markup (HTML,
XHTML, XML) documents to which the style sheet is related.
There are more than a few selectors available for use (Table 1),
but even intermediate and advanced CSS authors dont always
have an opportunity to use some of them, largely due to cross-
browser support issues for a given selector. Core CSS: Part II will
cover CSS 2.0/2.1 selectors. Where a selector is unavailable in
contemporary Web browsers, a caution will be provided to alert
you to any support concerns.
To assist in visualizing how these selectors actually match, for
each example youll see an element, the corresponding CSS,
and a document tree that visualizes what is selected within a
sample document. Also provided are some use examples.
Element Selectors
Element selectors, also referred to as type selectors, select by
matching elements. They are very broad in scope. For example,
if I have a million documents with many more millions of h2
elements within them, by using an element selector I can single
handedly apply styles to all of those h2 elements using one
rule. Element selectors are supported in all CSS browsers and
are very widely used for these reasons.
Heres a CSS rule using an element selector:
h2 {color: #f00; font-family: Garamond; font-size:
22px; font-variant: small-caps;}
In the corresponding markup document(s), all h2s are selected
and the style is applied (Figure 1).
Element Selectors, continued
Class Selectors
Class selectors are extremely useful selectors that allow authors
to add a class attribute to a given element in the markup, with a
custom value. Then using that value preceded by a dot, write a
corresponding rule using the class name.
An example of an element with an added class attribute in the
markup document would be:
<p class=warning>Any paragraph in any document on
any page containing this class will have the class
rules apply.</p>
In the CSS:
.warning {color: red; font-weight: bold;}
Figure 2 shows how this class, as written, will apply to both
elements with the class attribute named warning.
ALL ABOUT SELECTORS
C
o
r
e
C
S
S
:
P
a
r
t
I
I
w
w
w
.
d
z
o
n
e
.
c
o
m
G
e
t
M
o
r
e
R
e
f
c
a
r
d
z
!
V
i
s
i
t
r
e
f
c
a
r
d
z
.
c
o
m
n
Authoritative content
n
Designed for developers
n
Written by top experts
n
Latest tools & technologies
n
Hot tips & examples
n
Bonus content online
n
New issue every 1-2 weeks
Subscribe Now for FREE!
Refcardz.com
Get More Refcardz
(Theyre free!)
Core CSS: Part II
By Molly E. Holzschlag
Figure 17. You can generate actual content, but it will only appear
on the presentational surface.
Figure 18. While we can visually see the generated content on the
screen, it does not appear within the actual body of the document.
Selectors can be combined, giving authors highly specic ways
of working to style and manage documents.
Grouping
Selector grouping is simply placing a number of selectors that all
share common properties separated by commas:
h1, h2, h3, h4, h5, h6, p, q, blockquote, td,
#content, .standard {color: #000; margin: 5px;}
Now all these selectors will share the declaration properties.
Combining Selector Types
As youve already seen in several of this refcards examples,
you can combine selector types in order to create what some
designers and developers refer to as complex selectors. Table 5
COMBINING SELECTORS
Hot
Tip
Grouping is useful when you have a lot of shared
features between elements. You can group those
elements as shown, and then create more specic
rules for individual elements. You might have heard
of CSS reset or normalization which uses this technique.
Core CSS: Part II
6
tech facts at your fingertips
DZone, Inc.
1251 NW Maynard
Cary, NC 27513
888.678.0399
919.678.0300
Refcardz Feedback Welcome
refcardz@dzone.com
Sponsorship Opportunities
sales@dzone.com
Copyright 2008 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical,
photocopying, or otherwise, without prior written permission of the publisher. Reference: The Zen of CSS Design, Molly E. Holzschlag and Dave Shea, Peachpit Press, February 2005.
Version 1.0
$
7
.
9
5
ISBN-13: 978-1-934238-22-6
ISBN-10: 1-934238-22-8
9 781934 238226
5 0 7 9 5
ABOUT THE AUTHOR
Proving once and for all that
standards-compliant design
does not equal dull design,
this inspiring tome uses
examples from the landmark
CSS Zen Garden site as the
foundation for discussions
on how to create beautiful,
progressive CSS-based Web sites.
RECOMMENDED BOOK
BUY NOW
books.dzone.com/books/zencss
Molly E. Holzschlag
Molly E. Holzschlag is a well-known Web standards advocate, instructor, and
author. She is an Invited Expert to the W3C, and has served as Group Lead
for the Web Standards Project (WaSP). She has written more than 30 books
covering client-side development and design for the Web. Currently, Molly
works to educate designers and developers on using Web technologies in
practical ways to create highly sustainable, maintainable, accessible, interac-
tive and beautiful Web sites for the global community. She consults with major companies and
organizations such as AOL, BBC, Microsoft, Yahoo! and many others in an effort to improve
standards support, workow, solve interoperability concerns and address the long-term
management of highly interactive, large-scale sites. A popular and colorful individual, Molly
has a particular passion for people, blogs, and the use of technology for social progress.
Web Site
http://www.molly.com
Combining Selectors, continued
shows some examples as well as the selectors denitionand to
help you practicethe selectors specicity. Read selectors from
the right of the selectorit helps!
The resources in Table 6 should help you get more information
on the topics discussed in this card.
More Core CSS Refcardz:
Core CSS: Part IIIDecember 2008
Core CSS: Part IAvailable Now!
RESOURCES
Table 6. Resources
URL Reference
http://www.w3.org/TR/CSS21/cascade.
html#specicity
Specicity in CSS 2.1 explained
http://www.w3.org/TR/REC-CSS2/cascade.
html#specicity
Specicity in CSS 2.0
http://gallery.theopalgroup.com/
selectoracle/
SelectOracle: Free online tool to help you
calculate selector specicity
http://developer.yahoo.com/yui/reset/ Yahoo! User Interface library reset
http://meyerweb.com/eric/thoughts/
2007/05/01/reset-reloaded/
Eric Meyers take on using reset or
normalization"
Table 5. Combining selectors to create highly specic rules
Combined Selector Meaning Specicity (CSS 2)
#content div.module > p Selects child paragraphs descending
from a <div> element that has a class
of module and is within the uniquely
identied portion of the document that
is identied as content
1,1,2
#main-nav ul li ol >
li:hover
Selects only the rst letter of text in a
given element
1,0,4 (CSS 2)
1,1,4 (CSS 2.1)
tr > td+td+td > table Any table element that is a child of
a table data element that is the third
sibling from a table row element.
0,0,5
#content ul > li + li
a[href=http://molly.
com/]
Any anchor with an href of http://
molly.com/ that is the second child
sibling from an unordered list element
descending from an element with an
ID of content.
1,0,4 (CSS 2)
1,1,4 (CSS 2.1)
Upcoming Refcardz: Available:
Visit refcardz.com for a complete listing of available Refcardz.
Design Patterns
Published June 2008
FREE
Get More FREE Refcardz. Visit refcardz.com now!
DZone communities deliver over 4 million pages each month to
more than 1.7 million software developers, architects and decision
makers. DZone offers something for everyone, including news,
tutorials, cheatsheets, blogs, feature articles, source code and more.
DZone is a developers dream, says PC Magazine.
Core Seam
Core CSS: Part III
Hibernate Search
Equinox
EMF
XML
JSP Expression Language
ALM Best Practices
HTML and XHTML
Essential Ruby
Essential MySQL
JUnit and EasyMock
Getting Started with MyEclipse
Spring Annotations
Core Java
Core CSS: Part II
PHP
Getting Started with JPA
JavaServer Faces
Core CSS: Part I
Struts2
Core .NET
Very First Steps in Flex
C#
Groovy
NetBeans IDE 6.1 Java Editor
RSS and Atom
GlassFish Application Server
Silverlight 2