Core CSS:: by Molly E. Holzschlag
Core CSS:: by Molly E. Holzschlag
CONTENTS INCLUDE:
Element Selectors
•
1D Selectors
•
Descendent Selectors
•
Attribute Selectors
Combining Selectors
G et Mo re Re fcard z! Visit
•
•
Hot Tips and more... By Molly E. Holzschlag
Just in case you’ve not read Core CSS: Part I, 1’ll briefly
re fca rd z.co m
There’s also a universal selector, *, which when Fig ure 2. Se le cting all e le me nts with a class of warning.
Tip
document. It’s used in several hacks,
→
Get More Refcardz
including the infamous “star html” hack, (They’re free!)
Co re CSS: Part II
i m g [ s r c = ” p h o t o . j p g ” ] { b o r d e r : 2px s o l i d # 0 0 0 ; }
The selector will match only an image element with an Figure 14. Applying
MENTED style using
IN INTERNET pattern matching.
EXPLORER Note however
6.0 OR BELOW.
attribute
word must be first in the string. Had we switched the third
of s r c = ” i m a g e s / p h o t o . j p g ” and no other image elements will paragraph’s
be selected (Figure 12).
Pseudo Elements
As with pseudo classes, pseudo elements are predefined
elements within CSS. There are four of which to be aware,
as
described in Table 4.
Pseudo Element Purpo se Example
:first-line S elects only the first line of text in a blockquote:first-
line
given element. {font-weight:
bold;}
Figure 12. Applying a border to only the photo using the DZone, In c.
complete | www.dzone.com
:first-letter S e le cts only the first letter of text in a p:first-letter
(name+value) attribute selector.
5
tech facts at your
Core CSS: Part 11
fingertips
Pseudo Elements, continued 1n a situation where the generated content is largely decorative
or practical in some sense but does not inhibit access to important
First line and letter pseudo elements
Both the :first-line and :first-letter pseudo elements are typically data, this is fine. Take a look at the generated source by Firefox
and see the quotes called for do not appear in the code at all.
you’ll
used to add typographic features to a given set of text. The
following HTML block shows what happens in the document: But what if we were to generate the message itself? 1n the
HTML:
<p>Le t ’ s be hone s t . We a l l ma ke mi s t a ke s . Some t i mes
we can be t oo h ard on our s e l ve s , o r ot he r s , f or < b l o c kq u o t e > < / b l oc k q u o t e >
t hos e mi s t a ke s . I t ma ke s me r eme mbe r t ha t l ong ago And in the CSS:
and f ar a way, s ome one v e ry wi s e s a i d: </ p>
b
f ol or gc k
i vqeu od ti ev :ianfet ”e rf o{ nc ot -nstieznet:: “9To
0 p ex r; }r i s h u m a n , t o
<bl oc kquot e >To e r r i s huma n, t o f or gi ve di vi ne. </
bl oc kqu ot e >
Figure 17 shows the generated results.
<p>Ha vi ng bot h t he c a pac i t y t o be f or gi vi ng of
ot her s and t he a bi l i t y t o f or gi ve your s el f i s pa r t
of l e ar ni ng how t o be wi s e. </p>
Using the decorative pseudo elements, here are the CSS
examples from Table 4:
blockquote:first-letter {font-size: 250%} Figure 17. You can generate actual content, but it will only
p:first-line {font-weight: bold;} appear on the presentational surface.
Figure 15 shows the results. However, when we look at the source code, we see that the
generated content does not appear within the code (Figure 18).
Fig ure 15. Using first letter and line p s e u d o e le me nts to apply style .
No tice that in the case of :first-line, the “line” is d e fine d as wha te ve r
am ount of characters make up the first line. Because this is not
a lways the d e sire d result, using min-width and max-width p ro p e rtie s
to lim it line le ng th wherever p o ssib le can add ress this issue .
Both the first letter and line pseudo elements have Figure 18. While we can visually see the generated content on
good the screen, it does not appear within the actual body of the
support across browsers, including 1E 6.0. document.
of “CSS
Now reset”
all these or “normalization”
selectors will share which uses this technique.
the declaration properties.
Figure 16. Using pseudo elements to generate and style the quote
marks.
Grouping is useful
The caveat, and the cause of misuse and therefore controversy
when you have a lot of
has to do with the fact that the content generated by pseudo
shared
elements results in pseudo content. 1n practical terms, this →
means the content never actually appears in the content layer,
DZone, Inc.
Tip
www.dzone.com
elements as shown, and then create more
only the presentational layer! |
specific rules for individual elements. You
6
tech facts at your
Core CSS: Part 11
fingertips
DZone, Inc.
1251 NW Maynard
ISBN-13: 978-1-934238-22-6
Cary, NC 27513
ISBN-10: 1-934238-22-8
50795
888.678.0399
DZone communities deliver over 4 million pages each month to 919.678.0300
more than 1.7 million software developers, architects and decision Refcardz Feedback Welcome
makers. DZone offers something for everyone, including news, refcardz@dzone.com
$7.95
tutorials, cheatsheets, blogs, feature articles, source code and Sponsorship Opportunities 9 781934 238226
more. “DZone is a developer’s dream,” says PC Magazine. sales@dzone.com
Copyright © 2008 DZone, 1nc. 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, Version 1.0
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.