2. CSS Rule Recap
• CSS Rule consists of:
• Selector { Declaration Block }
• Declaration Block consists of zero or more Declarations
• Declaration consists of Property: Value pair ending in semicolon ;
• Example:
body {
color: rgb(91,91,91);
background-color: ivory;
font-family: Verdana, Geneva, sans-serif;
}
3. Specificity
• CSS rules for an element may appear multiple times
within a web page:
• inline, embedded, external
• If there are multiple instances of a CSS rule, which one
applies?
• The rule with the highest specificity weight
• If all specificity weights the same, the rule that appears last
• http://www.smashingmagazine.com/2007/07/27/css-
specificity-things-you-should-know
4. Selector and Specificity
• Read selectors from right to left
• selector
• Which element is going to be affected by the rule?
• The rightmost part of selector is the type of element
• Everything to the left specifies which set of elements that are affected
5. Types of selectors
• A contextual selector specifies the context of a matching
element
• The type of element
• The position an element is in relation to other elements
• An attribute selector uses an element's attributes and
attribute values to target a rule to a specific element
• Common attributes used for targeting are:
• id – can only appear only once in the HTML document
• class – may appear multiple times in the HTML document
6. Contextual selectors
Selector Description
* Matches any element
elem Matches the element elem located anywhere in the
document
elem1, elem2 Matches any of the elements elem1, elem2, etc.
parent descendent Matches the descendent element that is nested within
the parent element at some level
parent > child Matches the child element that is a child of the parent
element
elem1 + elem2 Matches elem2 that is immediately preceded by the
sibling element elem1
elem1 - elem2 Matches elem2 that follows sibling element elem1
7. Attribute selectors
Selector Selects
elem#id Element elem with the ID value id
#id Any element with the ID value id
elem.class All elem elements with the class attribute value class
.class All elements with the class value class
elem[att] All elem elements containing the att attribute
elem[att="text"] All elem elements whose att attribute equals text
elem[att~="text"] All elem elements whose att attribute contains the word text
elem[att|="text"] All elem elements whose att attribute is a hyphen-separated
list of words beginning with text
elem[att^="text"] All elem elements whose att attribute begins with text
[CSS3]
elem[att$="text"] All elem elements whose att attribute ends with text [CSS3]
elem[att*="text"] All elem elements whose att attribute contains the value text
[CSS3]
8. How to calculate specificity of a rule
Weight Multiply by
1 # of element names or pseudo elements
10 # of classes or pseudo class
100 id selector
1000 If an in-line rule
Special cases:
• Universal attribute * = 0
• !important overrides all
• If two rules have the same specificity weight, the last one applies
• Visual Specificity Calculator: http://specificity.keegan.st/
15. Contextual Selector: article p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
16. Contextual Selector: article p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element that is a
descendant of an article
element
17. Contextual Selector: article > p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
18. Contextual Selector: article > p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element that is a
child of an article element
19. Contextual Selector: h1, p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
20. Contextual Selector: h1, p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any h1 or p element
21. Contextual Selector: h1 + p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
22. Contextual Selector: h1 + p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects a p element that
immediately follow an h1
element
23. Contextual Selector: article h1 + p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
24. Contextual Selector: article h1 + p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects a p element that
immediately follows any h1
element that is a descendant of
an article element
25. Contextual Selector: h1 - p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
26. Contextual Selector: h1 - p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element that is a
sibling that follows after any h1
element
27. Contextual Selector: article h1 - p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
28. Contextual Selector: article h1 - p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any element that is a
child of an article element
29. Contextual Selector: article > *
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
30. Contextual Selector: article > *
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Editor's Notes
>: Stylin’ p.38
*: Stylin’ p.45
+: Stylin’ p.45-46 Figure 2.15, the <p> immediately after the <h1> has the rule applied
[]: Stylin’ p.46
>: Stylin’ p.38
*: Stylin’ p.45
+: Stylin’ p.45-46 Figure 2.15, the <p> immediately after the <h1> has the rule applied
[]: Stylin’ p.46
Specificity.html
DW Code Navigator: ALT-CMD-CLICK (Mac)
DW Webkit engine (Open Source, same base as Safari)