mastering css3 selectors

99
hello. Friday, 15 October 2010

Upload: rachel-andrew

Post on 13-Jan-2015

2.585 views

Category:

Technology


0 download

DESCRIPTION

Presentation for Think Vitamin Online CSS3 Conference.

TRANSCRIPT

Page 1: Mastering CSS3 Selectors

hello.

Friday, 15 October 2010

Page 2: Mastering CSS3 Selectors

Rachel Andrew

@rachelandrew

rachelandrew.co.ukedgeofmyseat.comgrabaperch.com

Friday, 15 October 2010

Page 3: Mastering CSS3 Selectors

Mastering CSS3

SelectorsFriday, 15 October 2010

Page 4: Mastering CSS3 Selectors

CSS3 is the next version of CSS

Friday, 15 October 2010

Page 5: Mastering CSS3 Selectors

CSS3 is Modular

Friday, 15 October 2010

Page 6: Mastering CSS3 Selectors

Some CSS3 modules are more complete

than others

Friday, 15 October 2010

Page 7: Mastering CSS3 Selectors

W3C Maturity LevelsWorking DraftCandidate RecommendationProposed RecommendationW3C Recommendation

http://www.w3.org/2005/10/Process-20051014/tr#maturity-levels

Friday, 15 October 2010

Page 8: Mastering CSS3 Selectors

CSS3 is supported by browsers

Some browsers and some (parts of) modules.

Friday, 15 October 2010

Page 9: Mastering CSS3 Selectors

Using CSS3

Friday, 15 October 2010

Page 10: Mastering CSS3 Selectors

Selectors module

W3C Proposed Recommendationhttp://www.w3.org/TR/css3-selectors/

Friday, 15 October 2010

Page 11: Mastering CSS3 Selectors

h1 {}

p {}

.thing {}

#uniquething {}

.thing p

Basic Selectors

Friday, 15 October 2010

Page 12: Mastering CSS3 Selectors

The problem with CSS2 selectors

Friday, 15 October 2010

Page 13: Mastering CSS3 Selectors

<h1>My heading</h1><p class=”first”> ... </p><p> ... </p>

Adding classes

Friday, 15 October 2010

Page 14: Mastering CSS3 Selectors

CSS3 Selectors“Common sense” new selectors

target elements more precisely without adding classes

Friday, 15 October 2010

Page 15: Mastering CSS3 Selectors

Structural pseudo-class selectors

Friday, 15 October 2010

Page 16: Mastering CSS3 Selectors

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

Friday, 15 October 2010

Page 17: Mastering CSS3 Selectors

:first-child

target an element when it is the first child of a parent element

Friday, 15 October 2010

Page 18: Mastering CSS3 Selectors

:first-child

Friday, 15 October 2010

Page 19: Mastering CSS3 Selectors

div#wrapper p {! ! font-size: 1.5em;}

:first-child

Friday, 15 October 2010

Page 20: Mastering CSS3 Selectors

div#wrapper p:first-child {! ! font-size: 1.5em;}

:first-child

Friday, 15 October 2010

Page 21: Mastering CSS3 Selectors

:first-child

Friday, 15 October 2010

Page 22: Mastering CSS3 Selectors

:last-child

target an element when it is the last child of a parent element

Friday, 15 October 2010

Page 23: Mastering CSS3 Selectors

:last-child

Friday, 15 October 2010

Page 24: Mastering CSS3 Selectors

ul#navigation li:last-child {! ! border-bottom: none;}

:last-child

Friday, 15 October 2010

Page 25: Mastering CSS3 Selectors

:last-child

Friday, 15 October 2010

Page 26: Mastering CSS3 Selectors

:nth-child

select multiple elements according to their position in the document tree

Friday, 15 October 2010

Page 27: Mastering CSS3 Selectors

:nth-child

Friday, 15 October 2010

Page 28: Mastering CSS3 Selectors

tr:nth-child(odd) td {! ! background-color: #f0e9c5;}

:nth-child

Friday, 15 October 2010

Page 29: Mastering CSS3 Selectors

:nth-child

Friday, 15 October 2010

Page 30: Mastering CSS3 Selectors

tr:nth-child(3) td {! ! background-color: #f0e9c5;}

:nth-child

Friday, 15 October 2010

Page 31: Mastering CSS3 Selectors

:nth-child

Friday, 15 October 2010

Page 32: Mastering CSS3 Selectors

tr:nth-child(2n+1) td {! ! background-color: #f0e9c5;}

:nth-child

http://reference.sitepoint.com/css/understandingnthchildexpressions

Friday, 15 October 2010

Page 33: Mastering CSS3 Selectors

:nth-of-type

select multiple elements according to their position in the document tree BUT only those elements that are

the same as the type the rule is applied to.

Friday, 15 October 2010

Page 34: Mastering CSS3 Selectors

p:nth-of-type(odd) {! ! background-color: #f0e9c5;}

:nth-of-type

Friday, 15 October 2010

Page 35: Mastering CSS3 Selectors

:nth-of-type

Friday, 15 October 2010

Page 36: Mastering CSS3 Selectors

:only-child

matches an element if it is the only child element of it’s parent.

Friday, 15 October 2010

Page 37: Mastering CSS3 Selectors

li {! list-style-type: disc;}!li:only-child {! list-style-type: none;}

:only-child

Friday, 15 October 2010

Page 38: Mastering CSS3 Selectors

:only-child

Friday, 15 October 2010

Page 39: Mastering CSS3 Selectors

:empty

matches an element if it is empty

Friday, 15 October 2010

Page 40: Mastering CSS3 Selectors

p {! padding: 0 0 1em 0;! margin: 0;}!p:empty {! padding: 0;}

:empty

Friday, 15 October 2010

Page 41: Mastering CSS3 Selectors

For input elements

Structural pseudo-classes for use with forms.

Friday, 15 October 2010

Page 42: Mastering CSS3 Selectors

:checked

the checked state of a checkbox or radio button

Friday, 15 October 2010

Page 43: Mastering CSS3 Selectors

#agreeterms:checked {border:2px solid blue;

}

:checked

Friday, 15 October 2010

Page 44: Mastering CSS3 Selectors

enabled and disabled

detecting input element states

Friday, 15 October 2010

Page 45: Mastering CSS3 Selectors

input:enabled {color: #000;

}

:enabled

Friday, 15 October 2010

Page 46: Mastering CSS3 Selectors

input:disabled {color: #999;

}

:disabled

Friday, 15 October 2010

Page 47: Mastering CSS3 Selectors

the Negation pseudo-class

:not(selector)

Friday, 15 October 2010

Page 48: Mastering CSS3 Selectors

<p> ... </p><p class=”box”> ... </p><p> ... </p>

:not

Friday, 15 October 2010

Page 49: Mastering CSS3 Selectors

p:not(.box) {! padding: 0 0 3em 0;! margin: 0;}!p.box {! border:1px solid #000;! margin: 0 0 2em 0;}

:not

Friday, 15 October 2010

Page 50: Mastering CSS3 Selectors

:not

Friday, 15 October 2010

Page 51: Mastering CSS3 Selectors

Pseudo-elements

Friday, 15 October 2010

Page 52: Mastering CSS3 Selectors

:first-letter

the first character of the first line of text

Friday, 15 October 2010

Page 53: Mastering CSS3 Selectors

div#wrapper:first-letter {! font-size: 200%;! font-weight: bold;! color: red;}

:first-letter

Friday, 15 October 2010

Page 54: Mastering CSS3 Selectors

:first-letter

Friday, 15 October 2010

Page 55: Mastering CSS3 Selectors

:first-line

the first formatted line of text

Friday, 15 October 2010

Page 56: Mastering CSS3 Selectors

div#wrapper:first-line {! font-size: 200%;! font-weight: bold;! color: red;}

:first-line

Friday, 15 October 2010

Page 57: Mastering CSS3 Selectors

:first-line

Friday, 15 October 2010

Page 58: Mastering CSS3 Selectors

:before

Render content before the element when using generated content

Friday, 15 October 2010

Page 59: Mastering CSS3 Selectors

<div id=”content”> ... </div>

:before

Friday, 15 October 2010

Page 60: Mastering CSS3 Selectors

#content:before { content: "Start here:";}

:before

Friday, 15 October 2010

Page 61: Mastering CSS3 Selectors

:before

Friday, 15 October 2010

Page 62: Mastering CSS3 Selectors

:after

Render content after the element when using generated content

Friday, 15 October 2010

Page 63: Mastering CSS3 Selectors

#content:after { content: "End here:";}

:after

Friday, 15 October 2010

Page 64: Mastering CSS3 Selectors

::selection

Content selected in browser by the user

Friday, 15 October 2010

Page 65: Mastering CSS3 Selectors

div#wrapper p::selection {!background-color: red;

}

::selection

Friday, 15 October 2010

Page 66: Mastering CSS3 Selectors

::selection

Friday, 15 October 2010

Page 67: Mastering CSS3 Selectors

Combinators

Combining selectors to target elements

Friday, 15 October 2010

Page 68: Mastering CSS3 Selectors

Descendant Selector

Select all elements that are descendants of a specified parent

Friday, 15 October 2010

Page 69: Mastering CSS3 Selectors

div#wrapper p {! font-size: 1.5em;}

Descendant Selector

Friday, 15 October 2010

Page 70: Mastering CSS3 Selectors

Child Selector

Select all elements that are immediate children of a specified parent

Friday, 15 October 2010

Page 71: Mastering CSS3 Selectors

<ul><li>Item 1 <ol>

<li>Sub list item 1</li> <li>Sub list item 2</li> </ol> </li><li>Item 2</li>

</ul>

Child Selector

Friday, 15 October 2010

Page 72: Mastering CSS3 Selectors

li {! color: #000;}!ul > li {! color: red;}

Child Selector

Friday, 15 October 2010

Page 73: Mastering CSS3 Selectors

Child Selector

Friday, 15 October 2010

Page 74: Mastering CSS3 Selectors

Adjacent Sibling

Select elements that are the adjacent siblings of an element

Friday, 15 October 2010

Page 75: Mastering CSS3 Selectors

div#wrapper h1 + p {! font-size: 1.5em;}

Adjacent Sibling

Friday, 15 October 2010

Page 76: Mastering CSS3 Selectors

Adjacent Sibling

Friday, 15 October 2010

Page 77: Mastering CSS3 Selectors

General Sibling

Select elements that are the siblings of an element

Friday, 15 October 2010

Page 78: Mastering CSS3 Selectors

div#wrapper h2~p {! color: red;}

General Sibling

Friday, 15 October 2010

Page 79: Mastering CSS3 Selectors

General Sibling

Friday, 15 October 2010

Page 80: Mastering CSS3 Selectors

Attribute Selectors

Select elements based on attributes

Friday, 15 October 2010

Page 81: Mastering CSS3 Selectors

form input[type="text"] {

}!form input[type="submit"] { }

Attribute Selectors

Friday, 15 October 2010

Page 82: Mastering CSS3 Selectors

Attribute Selectors

Friday, 15 October 2010

Page 83: Mastering CSS3 Selectors

label[for="fContact"] { ! float: none; ! width: auto;}

a[href ^="mailto:"] {! ! padding-right: 20px;! ! background-image:url(email.png);! ! background-repeat: no-repeat;! ! background-position: right center;}

Attribute Selectors

Friday, 15 October 2010

Page 84: Mastering CSS3 Selectors

Browser Support

Friday, 15 October 2010

Page 85: Mastering CSS3 Selectors

Browser Support

Friday, 15 October 2010

Page 86: Mastering CSS3 Selectors

Does it matter?

Friday, 15 October 2010

Page 87: Mastering CSS3 Selectors

Friday, 15 October 2010

Page 88: Mastering CSS3 Selectors

Friday, 15 October 2010

Page 89: Mastering CSS3 Selectors

Yes, it matters.

Friday, 15 October 2010

Page 90: Mastering CSS3 Selectors

Vendor-specific extensions

Implementing early stage CSS3

Friday, 15 October 2010

Page 91: Mastering CSS3 Selectors

border-radius

Friday, 15 October 2010

Page 92: Mastering CSS3 Selectors

.box {-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius: 10px;

}

border-radius

Friday, 15 October 2010

Page 93: Mastering CSS3 Selectors

In defence of vendor-specific extensions

Friday, 15 October 2010

Page 94: Mastering CSS3 Selectors

JavaScript

Plug the holes in browser support using JavaScript.

Friday, 15 October 2010

Page 95: Mastering CSS3 Selectors

div#wrapper p:first-child,div#wrapper p.first {! ! font-size: 1.5em;}

jQuery: first-child

<script src="http://code.jquery.com/jquery-latest.js"></script><script> $(document).ready(function(){! $("div#wrapper p:first-child").addClass("first"); });</script>

Friday, 15 October 2010

Page 96: Mastering CSS3 Selectors

ul#navigation li:last-child, ul#navigation li.last {! ! border-bottom: none;}

jQuery: last-child

<script src="http://code.jquery.com/jquery-latest.js"></script><script> $(document).ready(function(){! $("ul#navigation li:last-child").addClass("last"); });</script>

Friday, 15 October 2010

Page 97: Mastering CSS3 Selectors

tr:nth-child(odd) td, td.odd {! background-color: #f0e9c5;}

jQuery: nth-child

<script src="http://code.jquery.com/jquery-latest.js"></script><script> $(document).ready(function(){! $("tr:nth-child(odd) td").addClass("odd"); });</script>

Friday, 15 October 2010

Page 98: Mastering CSS3 Selectors

Scripts that “fix IE”

http://www.keithclark.co.uk/labs/ie-css3/http://ecsstender.org

Friday, 15 October 2010

Page 99: Mastering CSS3 Selectors

Thank you.

Friday, 15 October 2010