Page 1
© 2005-2012 R. Scott GrannemanLast updated 20120120
You are free to use this work, with certain restrictions.For full licensing information, please see the last slide/page.
Washington University in St. LouisR. Scott Granneman
[email protected]
SelectorsCSS Building Blocks
1
Page 2
What’s a selector?It identifies objects on a web page
to which you wish to applyCSS declarations
2
Page 4
Selectors Level 3W3C Recommendation
29 September 2011http://www.w3.org/TR/css3-selectors/
4
Page 10
19 selectors in CSS 1 & 2
8
Page 12
Under developmentsince December 15, 2005
10
Page 13
Not one large single specInstead, divided into
many separate documentscalled modules
11
Page 14
Currently 50+ modules!Different modules
have different statuses
12
Page 19
Which modules have reachedW3C Recommendation status?
3 in 2011! Selectors (19 new ones!)
! Namespaces! Color
14
Page 20
Others have reachedW3C Candidate Recommendation
status! Backgrounds & Colors
! Media Queries! Multi-Column Layout
15
Page 21
We’ll cover CSS 3 only a little bit
16
Page 22
The W3C categorizes CSS selectors,but I like the way that WestCiv
(makers of StyleMaster& fine CSS tutorials)
categorize them,so we’re using their method
For details, see:http://www.westciv.com/style_master/academy/css_tutorial/selectors/
17
Page 23
Web BrowserEngines
18
Page 24
Rendering Engine Name BrowsersBrowsers
Trident
Gecko
WebKit
Presto
19
Page 25
mshtml.dll
– 4.0.x– 5.0.x– 5.5.x– 6.0.x
3.1 7.04 8.0.x5 9.0.x6 10.0.x
45
5.56789
10
20
Page 26
1.71.8
1.8.11.9
1.9.11.9.2
25
1 6 61.5 7 72 8 83 9 9
3.53.645
21
Page 27
85100412522
530.17533.16
11.12345
522.11.3526.12.2533.16
345
22
Page 28
533534.3534.7
534.10534.13534.16534.24534.30535.1
5 535.1 146 535.2 157 535.7 168 535.11 179 535.16 18
10111213
23
Page 29
12
2.12.1.1
2.2.152.5.242.6.302.7.62
7 2.8.131 11.19 2.9.168 11.5
9.5 2.9.201 11.69.6 ? 12
10.0-110.510.611
24
Page 31
AKA HTML Element Selectors
26
Page 32
HTML elements as selectors:p {}h1 {}ul {}
table {}
27
Page 33
Used when you want to affectevery instance of an element
Example:p { color: red; font-weight: bold;}
Result: text in every <p>...</p>is red & bold
28
Page 34
Rendering Engine Support
4+
1+
85+
1+
29
Page 35
ClassSelectors
30
Page 36
Define a class in CSS:.emphasis {}.bookTitle {}
Apply it in HTML:<p class="emphasis">...</p>
<span class="bookTitle">...</span>
31
Page 37
2 kinds of class selectors! Solitary class selectors! Element class selectors
32
Page 38
Solitary class selectorscan be applied
to any HTML element.emphasis {}
33
Page 39
Element class selectorscan only be used
with specific HTML elementp.emphasis {}a.emphasis {}
34
Page 40
A class can be appliedas many times on a page
as you wish<p class="emphasis">
<span class="emphasis"><a class="emphasis">
... all on the same page
35
Page 41
" #a-z Space
A-Z Starting with #
0-9 Starting with -
-
_
Rules for CSS class names
36
Page 42
Class names should reflect function,not appearance
Good Bad
.emphasis .bold
.bookTitle .italic
.footer .bigRed
.center
37
Page 43
Multiple words in a class name?These are all OK
.maincontentnav.mainContentNav.MainContentNav
Just be consistent!
38
Page 44
Rendering Engine Support
7+
1+
85+
1+
39
Page 46
Define an ID in CSS:#navigation {}#utilityNav {}
Apply it in HTML:<div id="navigation">...</div><p id="utilityNav">...</p>
41
Page 47
2 kinds of ID selectors! Solitary ID selectors! Element ID selectors
42
Page 48
Solitary ID selectorscan be applied to any HTML element
#navigation {}
43
Page 49
Element ID selectorscan only be used
with specific HTML elementdiv#navigation {}p#utilityNav {}
44
Page 50
An ID can be appliedonly once per page
Not once per site – once per page<div id="navigation"><p id="utilityNav">
45
Page 51
" #a-z Space
A-Z Starting with #
0-9 Starting with -
-
_
Rules for CSS ID names
46
Page 52
ID names should reflect function,not appearance
Good Bad
#navigation #right
#utilityNav #narrow
#footer #bottom
47
Page 53
Multiple words in a ID name?These are all OK
#maincontentnav#mainContentNav#MainContentNav
Just be consistent!
48
Page 54
You’re probably going to use IDsa lot
when we get to CSS positioning
49
Page 55
Rendering Engine Support
4+
1+
85+
1+
50
Page 56
DescendentSelectors
51
Page 57
Used to be calledContextual Selectorsbefore renaming to
Descendant Selectors
52
Page 58
Selects an objectwhen it is the descendant
(child, grandchild,great grandchild, etc.)
of another objectNot necessarily the immediate child,
although that works,but any descendant
ul li {}ul li a {}
p.intro a {}
53
Page 59
CSS:ul li {}ul li a {}p.intro a {}p img {}
54
Page 60
CSS:p.intro a {}p img {}
HTML:<p class="intro"> ... <a> ... </
a> ... </p>
<p class="intro"> ... <span><a> ... </a></span> ... </p>
<p> ... <a><img> ... </img></a> ... </p>
55
Page 61
You can often usea descendant selector
instead of a class
56
Page 62
Given this HTML:<p> ... <img class="headshot"
src="..."> ... </p>
Instead of this CSS:.headshot {}
You could use this CSS:p img {}
And this HTML:<p> ... <img src="..."> ... </p>
57
Page 63
Rendering Engine Support
4+
1+
85+
1+
58
Page 64
LinkPseudo Class
Selectors59
Page 65
Links have 4 different states:! Normal (an unvisited link)
! Visited! Hover (mouse cursor is over link)! Active (moment of being clicked)
60
Page 66
CSS:a:link
a:visiteda:hovera:active
Combine withelement class selectors:
a.offsite:hover {}
61
Page 67
Some of the 4 link statesare mutually exclusive
(can’t be visited & unvisitedat the same time)& some are not
(can be both visited & activeat the same time)
62
Page 68
If CSS rules contradict,the last one wins
Specify your rules in this order:a {}
a:link {}a:visited {}a:hover {}a:active {}
63
Page 69
Rendering Engine Support
3+
1+
85+
1+
a:link
64
Page 70
Rendering Engine Support
3+
1+
85+
1+
a:visited
65
Page 71
Rendering Engine Support
3+
1+
85+
1+
a:hover
66
Page 72
Rendering Engine Support
3+
1+
85+
1+
a:active
67
Page 73
PseudoElementSelectors
68
Page 74
:first-line:first-letter
&:before:after
69
Page 75
:first-line
Selects 1st lineof a block-level element
& can only be usedwith certain properties
p:first-line {}
70
Page 76
:first-letter
Selects 1st letterof a block-level element
& can only be usedwith certain properties
p:first-letter
Often used to createa “drop cap” effect
71
Page 77
:before
Selects element& inserts content
(text, images, counters, etc.)before it
72
Page 78
:after
Selects element& inserts content
(text, images, counters, etc.)after it
73
Page 79
CSSh1:before {content:"Chapter: ";}
HTML<h1>1</h1>
User seesChapter: 1
74
Page 80
CSSa.flight:after {content:"http://
www.example.com/plane.gif";}
HTML<a class="flight" href="http://www.expedia.com">Expedia</a>
User seesExpedia $
75
Page 81
CSSa:after {content: attr(href)}
HTML<a href="http://www.domain.com">Link
to site</a>
User seesLink to site http://www.domain.com
76
Page 82
Rendering Engine Support
mshtml 5.5+*
1+
85+*
1+
:first-line
77
Page 83
IE 8Rules with !important
are ignored inside :first-line
78
Page 84
text-transformproperty doesn’t work
79
Page 85
Rendering Engine Support
mshtml 5.5+*
1+
85+
1+
:first-letter
80
Page 86
IE 6Combining :first-letter rules
with othersmay be problematic
IE 8Rules with !important
are ignored inside :first-letter
81
Page 87
Rendering Engine Support
8+
1.9.1+
85+*
1+
:before
82
Page 88
Some styles can’t be appliedto :before,
such as animations & transitions
83
Page 89
Rendering Engine Support
8+
1.9.1+
85+*
1+
:after
84
Page 90
Some styles can’t be appliedto :after,
such as animations & transitions
85
Page 91
SelectorGroups
86
Page 92
Allows you to applythe same declarations
to several selectors at oncep, input, blockquote, td {font-
family:"Times New Roman";}
a:link, a:visited, a:active, a:hover {background-color:#ffff00;}
87
Page 93
Rendering Engine Support
4+*
1+
85+
1+
88
Page 94
IE 8If IE doesn’t support
one of the grouped selectors,ALL of them fail!
89
Page 95
UniversalSelectors
90
Page 96
* {font-family: Verdana, sans-serif;}
* matchesevery (appropriate) HTML element
91
Page 97
Instead of thisp, blockquote, td, option { font-family: Verdana, Arial, sans-serif; font-size: 12px;}
Use this* { font-family: Verdana, Arial, sans-serif; font-size: 12px;}
92
Page 98
The Global White Space Reset* { margin: 0; padding: 0;}
93
Page 99
Rendering Engine Support
7+
1+
85+
1+
94
Page 100
DynamicPseudo Class
Selectors95
Page 101
:hover & :active can be appliedto any element,
not just <a>Also added :focus
96
Page 102
:hover (user’s mouse cursoris over element)
:active (form element at momentof being clicked)
:focus (form elementwhen keyboard’s cursor in or on it)
p:hover {}input:active {}
textarea:focus {}p.intro:hover {}
97
Page 103
Rendering Engine Support
7+
1+
419.3+
1+
:hover
98
Page 104
Rendering Engine Support
8+
1+
85+
1+
:active
99
Page 105
Rendering Engine Support
8+
1+
85+
1+
:focus
100
Page 106
LanguageSelectors
101
Page 107
Select elementsbased on language encoding
Every language hasa 2-letter encoding
+ a dash& 2 more letters for dialects
102
Page 108
fr = Frenchzh = Chineseen = English
en-US = American EnglishHTML
<p lang="fr">
103
Page 109
Different languagesuse different formatting
Bold & italic,indenting,
quotation marks,etc.
To use with CSS:cite:lang(fr) {}
blockquote.literature:lang(zh) {}
104
Page 110
Rendering Engine Support
8+
1.2+
525+
1+
105
Page 111
ChildSelectors
106
Page 112
Descendant selectors work for bothdirect children & any descendants,
no matter how deepThe child selector works only
for direct children,not grandchildren
or any other descendantsdiv>p {}
div>div {}
107
Page 113
Rendering Engine Support
7+
1+
85+
1+
108
Page 114
First ChildSelectors
109
Page 115
The child selector worksonly if the element
is the 1st child of a parentp:first-child {}
means the first <p>of any parent element,not the first child of <p>
110
Page 116
Rendering Engine Support
7+
1+
85+
1+
111
Page 117
AdjacentSelectors
112
Page 118
Selects siblings:2 or more elements
that are at the same level2nd element is selected, not both!
Elements must be listedin the order
in which they appear in HTMLh1 + h2 {margin-top:-20px;}img + p {margin:auto; text-align:center; width:70%;}
113
Page 119
Rendering Engine Support
7+
1+
85+
1+
114
Page 120
AttributeSelectors
115
Page 121
Allows you to selecta particular element
based on 1 of 4 attribute conditions
116
Page 122
(1) Does element foohave attribute bar?
This CSS:a[title] {}
matches this HTML:<a href="http://www.domain.com"
title="Stuff">
It doesn’t matter what the title is,just that it has a title.
117
Page 123
(2) Does element foohave a specific attribute value bar?
This CSS:a[title="Balloon"] {}
matches this HTML:<a href="http://www.domain.com"
title="Balloon">
but not this HTML:<a href="http://www.domain.com"
title="Red Balloon">
118
Page 124
(3) Does element foo have a specific attribute value bar among a list of
space separated values?This CSS:
a[title~="Balloon"] {}
matches this HTML:<a href="http://www.domain.com"
title="Red Balloon">
and this HTML:<a href="http://www.domain.com"
title="Blue Balloon">119
Page 125
(4) Does element foo have a specific attribute value bar among a list of
hyphen separated values?This CSS:
a[lang|="en"] {}
matches this HTML:<a href="http://www.domain.com"
lang="en-US">
and this HTML:<a href="http://www.domain.com"
lang="en-UK">120
Page 126
Rendering Engine Support
7+*
1+
85+
1+
x[attr]
121
Page 127
Matches every td & th in tablewhen attribute is colspan
(regardless of whethercolspan is used!)
122
Page 128
Rendering Engine Support
7+
1+
85+
1+
x[attr=“value”]
123
Page 129
Rendering Engine Support
7+
1+
85+
1+
x[attr~=“value”]
124
Page 130
Rendering Engine Support
7+
1+
85+
1+
x[attr|=“value”]
125
Page 132
QuirksModeCompatibility Master Table
www.quirksmode.org/compatibility.html
127
Page 133
Western CivilisationComplete CSS Guide:Selectors introduction
www.westciv.com/style_master/academy/css_tutorial/selectors/
128
Page 134
Comparison of layout engines(Cascading Style Sheets)
http://en.wikipedia.org/wiki/Comparison_of_layout_engines_
(Cascading_Style_Sheets)
129
Page 135
CSS Compatibilityand Internet Explorer
http://msdn.microsoft.com/en-us/library/cc351024(VS.85).aspx
130
Page 136
Thank you!
Email: [email protected] : www.granneman.com
Publications: www.granneman.com/pubsBlog: blog.granneman.comTwitter: scottgranneman
131
Page 137
© 2005-2012 R. Scott GrannemanLast updated 20120120
You are free to use this work, with certain restrictions.For full licensing information, please see the last slide/page.
Washington University in St. LouisR. Scott Granneman
[email protected]
SelectorsCSS Building Blocks
132
Page 138
Licensing of this workThis work is licensed under the Creative Commons Attribution-ShareAlike License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/1.0or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
In addition to the rights and restrictions common to all Creative Commons licenses, the Attribution-ShareAlike License features the following key conditions:
Attribution. The licensor permits others to copy, distribute, display, and perform the work. In return, licensees must give the original author credit.
Share Alike. The licensor permits others to distribute derivative works under a license identical to the one that governs the licensor’s work.
Questions? Email [email protected]
133