Top Banner
© 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 1
138

CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011 TR/css3-selectors/ 4

May 29, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

© 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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

to which you wish to applyCSS declarations

2

Page 3: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

How Many?

3

Page 4: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Selectors Level 3W3C Recommendation

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

4

Page 5: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

5

Page 6: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

5

Page 7: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

6

Page 8: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

6

Page 9: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

CSS 1 & 2

7

Page 10: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

19 selectors in CSS 1 & 2

8

Page 11: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

CSS 3

9

Page 12: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Under developmentsince December 15, 2005

10

Page 13: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Not one large single specInstead, divided into

many separate documentscalled modules

11

Page 14: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Currently 50+ modules!Different modules

have different statuses

12

Page 15: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

13

Page 16: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

13

Page 17: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

13

Page 18: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

13

Page 19: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Which modules have reachedW3C Recommendation status?

3 in 2011! Selectors (19 new ones!)

! Namespaces! Color

14

Page 20: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Others have reachedW3C Candidate Recommendation

status! Backgrounds & Colors

! Media Queries! Multi-Column Layout

15

Page 21: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

We’ll cover CSS 3 only a little bit

16

Page 22: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Web BrowserEngines

18

Page 24: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Name BrowsersBrowsers

Trident

Gecko

WebKit

Presto

19

Page 25: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

85100412522

530.17533.16

11.12345

522.11.3526.12.2533.16

345

22

Page 28: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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 30: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

TypeSelectors

25

Page 31: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

AKA HTML Element Selectors

26

Page 32: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

table {}

27

Page 33: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

4+

1+

85+

1+

29

Page 35: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

ClassSelectors

30

Page 36: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

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

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

31

Page 37: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

32

Page 38: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Solitary class selectorscan be applied

to any HTML element.emphasis {}

33

Page 39: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Element class selectorscan only be used

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

34

Page 40: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

" #a-z Space

A-Z Starting with #

0-9 Starting with -

-

_

Rules for CSS class names

36

Page 42: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Class names should reflect function,not appearance

Good Bad

.emphasis .bold

.bookTitle .italic

.footer .bigRed

.center

37

Page 43: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Multiple words in a class name?These are all OK

.maincontentnav.mainContentNav.MainContentNav

Just be consistent!

38

Page 44: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

39

Page 45: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

ID Selectors

40

Page 46: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

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

41

Page 47: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

42

Page 48: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Solitary ID selectorscan be applied to any HTML element

#navigation {}

43

Page 49: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Element ID selectorscan only be used

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

44

Page 50: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

An ID can be appliedonly once per page

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

45

Page 51: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

" #a-z Space

A-Z Starting with #

0-9 Starting with -

-

_

Rules for CSS ID names

46

Page 52: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

ID names should reflect function,not appearance

Good Bad

#navigation #right

#utilityNav #narrow

#footer #bottom

47

Page 53: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Multiple words in a ID name?These are all OK

#maincontentnav#mainContentNav#MainContentNav

Just be consistent!

48

Page 54: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

You’re probably going to use IDsa lot

when we get to CSS positioning

49

Page 55: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

4+

1+

85+

1+

50

Page 56: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

DescendentSelectors

51

Page 57: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Used to be calledContextual Selectorsbefore renaming to

Descendant Selectors

52

Page 58: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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 Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

54

Page 60: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

You can often usea descendant selector

instead of a class

56

Page 62: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

4+

1+

85+

1+

58

Page 64: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

LinkPseudo Class

Selectors59

Page 65: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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 Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

CSS:a:link

a:visiteda:hovera:active

Combine withelement class selectors:

a.offsite:hover {}

61

Page 67: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

3+

1+

85+

1+

a:link

64

Page 70: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

3+

1+

85+

1+

a:visited

65

Page 71: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

3+

1+

85+

1+

a:hover

66

Page 72: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

3+

1+

85+

1+

a:active

67

Page 73: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

PseudoElementSelectors

68

Page 74: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

:first-line:first-letter

&:before:after

69

Page 75: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

:first-line

Selects 1st lineof a block-level element

& can only be usedwith certain properties

p:first-line {}

70

Page 76: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

: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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

:before

Selects element& inserts content

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

72

Page 78: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

:after

Selects element& inserts content

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

73

Page 79: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

HTML<h1>1</h1>

User seesChapter: 1

74

Page 80: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

mshtml 5.5+*

1+

85+*

1+

:first-line

77

Page 83: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

IE 8Rules with !important

are ignored inside :first-line

78

Page 84: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

text-transformproperty doesn’t work

79

Page 85: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

mshtml 5.5+*

1+

85+

1+

:first-letter

80

Page 86: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

IE 6Combining :first-letter rules

with othersmay be problematic

IE 8Rules with !important

are ignored inside :first-letter

81

Page 87: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

8+

1.9.1+

85+*

1+

:before

82

Page 88: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Some styles can’t be appliedto :before,

such as animations & transitions

83

Page 89: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

8+

1.9.1+

85+*

1+

:after

84

Page 90: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Some styles can’t be appliedto :after,

such as animations & transitions

85

Page 91: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

SelectorGroups

86

Page 92: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

4+*

1+

85+

1+

88

Page 94: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

IE 8If IE doesn’t support

one of the grouped selectors,ALL of them fail!

89

Page 95: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

UniversalSelectors

90

Page 96: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

* matchesevery (appropriate) HTML element

91

Page 97: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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

93

Page 99: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

94

Page 100: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

DynamicPseudo Class

Selectors95

Page 101: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

:hover & :active can be appliedto any element,

not just <a>Also added :focus

96

Page 102: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

: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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

419.3+

1+

:hover

98

Page 104: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

8+

1+

85+

1+

:active

99

Page 105: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

8+

1+

85+

1+

:focus

100

Page 106: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

LanguageSelectors

101

Page 107: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Select elementsbased on language encoding

Every language hasa 2-letter encoding

+ a dash& 2 more letters for dialects

102

Page 108: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

fr = Frenchzh = Chineseen = English

en-US = American EnglishHTML

<p lang="fr">

103

Page 109: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Different languagesuse different formatting

Bold & italic,indenting,

quotation marks,etc.

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

blockquote.literature:lang(zh) {}

104

Page 110: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

8+

1.2+

525+

1+

105

Page 111: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

ChildSelectors

106

Page 112: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

108

Page 114: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

First ChildSelectors

109

Page 115: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

111

Page 117: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

AdjacentSelectors

112

Page 118: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

114

Page 120: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

AttributeSelectors

115

Page 121: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Allows you to selecta particular element

based on 1 of 4 attribute conditions

116

Page 122: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

(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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

(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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

(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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

(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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+*

1+

85+

1+

x[attr]

121

Page 127: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Matches every td & th in tablewhen attribute is colspan

(regardless of whethercolspan is used!)

122

Page 128: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

x[attr=“value”]

123

Page 129: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

x[attr~=“value”]

124

Page 130: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Rendering Engine Support

7+

1+

85+

1+

x[attr|=“value”]

125

Page 131: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Sources

126

Page 132: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

QuirksModeCompatibility Master Table

www.quirksmode.org/compatibility.html

127

Page 133: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Western CivilisationComplete CSS Guide:Selectors introduction

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

128

Page 134: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Comparison of layout engines(Cascading Style Sheets)

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

(Cascading_Style_Sheets)

129

Page 135: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

CSS Compatibilityand Internet Explorer

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

130

Page 136: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

Thank you!

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

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

131

Page 137: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

© 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: CSS Building Blocks - Scott Grannemanfiles.granneman.com/presentations/css/CSSSelectorsSlides.pdfSelectors Level 3 W3C Recommendation 29 September 2011  TR/css3-selectors/ 4

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