css selectors (handouts) - scott...

46
© 2005-2012 R. Scott Granneman Last 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. Louis R. Scott Granneman [email protected] www.granneman.com Selectors CSS Building Blocks What’s a selector? It identifies objects on a web page to which you wish to apply CSS declarations How Many? 1 2 3

Upload: ngoduong

Post on 06-Feb-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

© 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

What’s a selector?It identifies objects on a web page

to which you wish to applyCSS declarations

How Many?

1

2

3

Page 2: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Selectors Level 3W3C Recommendation

29 September 2011http://www.w3.org/TR/css3-selectors/

4

5

5

Page 3: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

CSS 1 & 2

6

6

7

Page 4: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

19 selectors in CSS 1 & 2

CSS 3

Under developmentsince December 15, 2005

8

9

10

Page 5: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Not one large single specInstead, divided into

many separate documentscalled modules

Currently 50+ modules!Different modules

have different statuses

11

12

13

Page 6: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

13

13

13

Page 7: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Which modules have reachedW3C Recommendation status?

3 in 2011! Selectors (19 new ones!)

! Namespaces! Color

Others have reachedW3C Candidate Recommendation

status! Backgrounds & Colors

! Media Queries! Multi-Column Layout

We’ll cover CSS 3 only a little bit

14

15

16

Page 8: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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/

Web BrowserEngines

Rendering Engine Name BrowsersBrowsers

Trident

Gecko

WebKit

Presto

17

18

19

Page 9: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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

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

85100412522

530.17533.16

11.12345

522.11.3526.12.2533.16

345

20

21

22

Page 10: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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

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

TypeSelectors

23

24

25

Page 11: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

AKA HTML Element Selectors

HTML elements as selectors:p {}h1 {}ul {}

table {}

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

26

27

28

Page 12: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

4+

1+

85+

1+

ClassSelectors

Define a class in CSS:.emphasis {}.bookTitle {}

Apply it in HTML:<p class="emphasis">...</p>

<span class="bookTitle">...</span>

29

30

31

Page 13: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

2 kinds of class selectors! Solitary class selectors! Element class selectors

Solitary class selectorscan be applied

to any HTML element.emphasis {}

Element class selectorscan only be used

with specific HTML elementp.emphasis {}a.emphasis {}

32

33

34

Page 14: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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

" #a-z Space

A-Z Starting with #

0-9 Starting with -

-

_

Rules for CSS class names

Class names should reflect function,not appearance

Good Bad

.emphasis .bold

.bookTitle .italic

.footer .bigRed

.center

35

36

37

Page 15: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Multiple words in a class name?These are all OK

.maincontentnav.mainContentNav.MainContentNav

Just be consistent!

Rendering Engine Support

7+

1+

85+

1+

ID Selectors

38

39

40

Page 16: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Define an ID in CSS:#navigation {}#utilityNav {}

Apply it in HTML:<div id="navigation">...</div><p id="utilityNav">...</p>

2 kinds of ID selectors! Solitary ID selectors! Element ID selectors

Solitary ID selectorscan be applied to any HTML element

#navigation {}

41

42

43

Page 17: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Element ID selectorscan only be used

with specific HTML elementdiv#navigation {}p#utilityNav {}

An ID can be appliedonly once per page

Not once per site – once per page<div id="navigation"><p id="utilityNav">

" #a-z Space

A-Z Starting with #

0-9 Starting with -

-

_

Rules for CSS ID names

44

45

46

Page 18: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

ID names should reflect function,not appearance

Good Bad

#navigation #right

#utilityNav #narrow

#footer #bottom

Multiple words in a ID name?These are all OK

#maincontentnav#mainContentNav#MainContentNav

Just be consistent!

You’re probably going to use IDsa lot

when we get to CSS positioning

47

48

49

Page 19: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

4+

1+

85+

1+

DescendentSelectors

Used to be calledContextual Selectorsbefore renaming to

Descendant Selectors

50

51

52

Page 20: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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 {}

CSS:ul li {}ul li a {}p.intro a {}p img {}

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>

53

54

55

Page 21: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

You can often usea descendant selector

instead of a class

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>

Rendering Engine Support

4+

1+

85+

1+

56

57

58

Page 22: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

LinkPseudo Class

Selectors

Links have 4 different states:! Normal (an unvisited link)

! Visited! Hover (mouse cursor is over link)! Active (moment of being clicked)

CSS:a:link

a:visiteda:hovera:active

Combine withelement class selectors:

a.offsite:hover {}

59

60

61

Page 23: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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)

If CSS rules contradict,the last one wins

Specify your rules in this order:a {}

a:link {}a:visited {}a:hover {}a:active {}

Rendering Engine Support

3+

1+

85+

1+

a:link

62

63

64

Page 24: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

3+

1+

85+

1+

a:visited

Rendering Engine Support

3+

1+

85+

1+

a:hover

Rendering Engine Support

3+

1+

85+

1+

a:active

65

66

67

Page 25: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

PseudoElementSelectors

:first-line:first-letter

&:before:after

:first-line

Selects 1st lineof a block-level element

& can only be usedwith certain properties

p:first-line {}

68

69

70

Page 26: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

: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

:before

Selects element& inserts content

(text, images, counters, etc.)before it

:after

Selects element& inserts content

(text, images, counters, etc.)after it

71

72

73

Page 27: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

CSSh1:before {content:"Chapter: ";}

HTML<h1>1</h1>

User seesChapter: 1

CSSa.flight:after {content:"http://

www.example.com/plane.gif";}

HTML<a class="flight" href="http://www.expedia.com">Expedia</a>

User seesExpedia $

CSSa:after {content: attr(href)}

HTML<a href="http://www.domain.com">Link

to site</a>

User seesLink to site http://www.domain.com

74

75

76

Page 28: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

mshtml 5.5+*

1+

85+*

1+

:first-line

IE 8Rules with !important

are ignored inside :first-line

text-transformproperty doesn’t work

77

78

79

Page 29: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

mshtml 5.5+*

1+

85+

1+

:first-letter

IE 6Combining :first-letter rules

with othersmay be problematic

IE 8Rules with !important

are ignored inside :first-letter

Rendering Engine Support

8+

1.9.1+

85+*

1+

:before

80

81

82

Page 30: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Some styles can’t be appliedto :before,

such as animations & transitions

Rendering Engine Support

8+

1.9.1+

85+*

1+

:after

Some styles can’t be appliedto :after,

such as animations & transitions

83

84

85

Page 31: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

SelectorGroups

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;}

Rendering Engine Support

4+*

1+

85+

1+

86

87

88

Page 32: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

IE 8If IE doesn’t support

one of the grouped selectors,ALL of them fail!

UniversalSelectors

* {font-family: Verdana, sans-serif;}

* matchesevery (appropriate) HTML element

89

90

91

Page 33: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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;}

The Global White Space Reset* { margin: 0; padding: 0;}

Rendering Engine Support

7+

1+

85+

1+

92

93

94

Page 34: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

DynamicPseudo Class

Selectors

:hover & :active can be appliedto any element,

not just <a>Also added :focus

: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 {}

95

96

97

Page 35: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

7+

1+

419.3+

1+

:hover

Rendering Engine Support

8+

1+

85+

1+

:active

Rendering Engine Support

8+

1+

85+

1+

:focus

98

99

100

Page 36: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

LanguageSelectors

Select elementsbased on language encoding

Every language hasa 2-letter encoding

+ a dash& 2 more letters for dialects

fr = Frenchzh = Chineseen = English

en-US = American EnglishHTML

<p lang="fr">

101

102

103

Page 37: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Different languagesuse different formatting

Bold & italic,indenting,

quotation marks,etc.

To use with CSS:cite:lang(fr) {}

blockquote.literature:lang(zh) {}

Rendering Engine Support

8+

1.2+

525+

1+

ChildSelectors

104

105

106

Page 38: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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 {}

Rendering Engine Support

7+

1+

85+

1+

First ChildSelectors

107

108

109

Page 39: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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>

Rendering Engine Support

7+

1+

85+

1+

AdjacentSelectors

110

111

112

Page 40: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

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%;}

Rendering Engine Support

7+

1+

85+

1+

AttributeSelectors

113

114

115

Page 41: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Allows you to selecta particular element

based on 1 of 4 attribute conditions

(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.

(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">

116

117

118

Page 42: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

(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">

(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">

Rendering Engine Support

7+*

1+

85+

1+

x[attr]

119

120

121

Page 43: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Matches every td & th in tablewhen attribute is colspan

(regardless of whethercolspan is used!)

Rendering Engine Support

7+

1+

85+

1+

x[attr=“value”]

Rendering Engine Support

7+

1+

85+

1+

x[attr~=“value”]

122

123

124

Page 44: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Rendering Engine Support

7+

1+

85+

1+

x[attr|=“value”]

Sources

QuirksModeCompatibility Master Table

www.quirksmode.org/compatibility.html

125

126

127

Page 45: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Western CivilisationComplete CSS Guide:Selectors introduction

www.westciv.com/style_master/academy/css_tutorial/selectors/

Comparison of layout engines(Cascading Style Sheets)

http://en.wikipedia.org/wiki/Comparison_of_layout_engines_

(Cascading_Style_Sheets)

CSS Compatibilityand Internet Explorer

http://msdn.microsoft.com/en-us/library/cc351024(VS.85).aspx

128

129

130

Page 46: CSS Selectors (handouts) - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsHandouts.pdf · CSS Building Blocks What’s a selector? It identifies objects on a web

Thank you!

Email: [email protected]: www.granneman.com

Publications: www.granneman.com/pubsBlog: blog.granneman.comTwitter: scottgranneman

© 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

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]

131

132

133