css3 and selectors

106
CSS3 & S ELECTORS

Upload: rachel-andrew

Post on 17-May-2015

3.121 views

Category:

Technology


1 download

DESCRIPTION

Presentation given at the CSS Summit 2011

TRANSCRIPT

Page 1: CSS3 and Selectors

CSS3 &

SELECTORS

Page 2: CSS3 and Selectors

RACHEL ANDREW

@rachelandrew

rachelandrew.co.ukedgeofmyseat.com

grabaperch.com

Page 3: CSS3 and Selectors

WHAT IS

CSS3?

Page 4: CSS3 and Selectors

CSS3 IS THE NEXT VERSION

OF CSS

Page 5: CSS3 and Selectors

CSS3 IS

MODULAR

Page 6: CSS3 and Selectors

SOME CSS3 MODULES ARE MORE COMPLETE

THAN OTHERS

Page 7: CSS3 and Selectors

W3C MATURITY LEVELS

Working DraftCandidate RecommendationProposed RecommendationW3C Recommendation

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

Page 8: CSS3 and Selectors

CSS3 IS SUPPORTED BY BROWSERS

Some browsers and some (parts of) modules.

Page 9: CSS3 and Selectors

USING CSS3

Page 10: CSS3 and Selectors

SELECTORS MODULE

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

Page 11: CSS3 and Selectors

h1 {}

p {}

.thing {}

#uniquething {}

.thing p

BASIC SELECTORS

Page 12: CSS3 and Selectors

THE PROBLEM

WITH CSS2 SELECTORS

Page 13: CSS3 and Selectors

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

ADDING CLASSES

Page 14: CSS3 and Selectors

CSS3 SELECTORS

“Common sense” new selectorstarget elements more precisely without adding

classes

Page 15: CSS3 and Selectors

STRUCTURAL PSEUDO-CLASS SELECTORS

Page 16: CSS3 and Selectors

a:link {}a:visited {}

PSEUDO-CLASS

SELECTORS

Page 17: CSS3 and Selectors

a:hover {}a:active {}

DYNAMIC

PSEUDO-CLASS

Page 18: CSS3 and Selectors

:first-child

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

Page 19: CSS3 and Selectors

:first-child

Page 20: CSS3 and Selectors

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

:first-child

Page 21: CSS3 and Selectors

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

:first-child

Page 22: CSS3 and Selectors

:first-child

Page 23: CSS3 and Selectors

:last-child

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

Page 24: CSS3 and Selectors

:last-child

Page 25: CSS3 and Selectors

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

:last-child

Page 26: CSS3 and Selectors

:last-child

Page 27: CSS3 and Selectors

:nth-child

select multiple elements according to their position in the document tree

Page 28: CSS3 and Selectors

:nth-child

Page 29: CSS3 and Selectors

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

:nth-child

Page 30: CSS3 and Selectors

:nth-child

Page 31: CSS3 and Selectors

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

:nth-child

Page 32: CSS3 and Selectors

:nth-child

Page 33: CSS3 and Selectors

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

:nth-child

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

Page 34: CSS3 and 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.

Page 35: CSS3 and Selectors

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

:nth-of-type

Page 36: CSS3 and Selectors

:nth-of-type

Page 37: CSS3 and Selectors

:only-child

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

Page 38: CSS3 and Selectors

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

:only-child

Page 39: CSS3 and Selectors

:only-child

Page 40: CSS3 and Selectors

:empty

matches an element if it is empty

Page 41: CSS3 and Selectors

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

:empty

Page 42: CSS3 and Selectors

FOR INPUT ELEMENTS

Structural pseudo-classes for use with forms.

Page 43: CSS3 and Selectors

:checked

the checked state of a checkbox or radio button

Page 44: CSS3 and Selectors

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

}

:checked

Page 45: CSS3 and Selectors

ENABLED AND DISABLED

detecting input element states

Page 46: CSS3 and Selectors

input:enabled {color: #000;

}

:enabled

Page 47: CSS3 and Selectors

input:disabled {color: #999;

}

:disabled

Page 48: CSS3 and Selectors

THE NEGATION PSEUDO-CLASS

:not(selector)

Page 49: CSS3 and Selectors

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

:not

Page 50: CSS3 and Selectors

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

:not

Page 51: CSS3 and Selectors

:not

Page 52: CSS3 and Selectors

PSEUDO-ELEMENTS

Page 53: CSS3 and Selectors

:first-letter

the first character of the first line of text

Page 54: CSS3 and Selectors

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

:first-letter

Page 55: CSS3 and Selectors

:first-letter

Page 56: CSS3 and Selectors

:first-line

the first formatted line of text

Page 57: CSS3 and Selectors

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

:first-line

Page 58: CSS3 and Selectors

:first-line

Page 59: CSS3 and Selectors

:before

Render content before the element when using generated content

Page 60: CSS3 and Selectors

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

:before

Page 61: CSS3 and Selectors

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

:before

Page 62: CSS3 and Selectors

:before

Page 63: CSS3 and Selectors

:after

Render content after the element when using generated content

Page 64: CSS3 and Selectors

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

:after

Page 65: CSS3 and Selectors

::selection

Content selected in browser by the user

Page 66: CSS3 and Selectors

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

}

::selection

Page 67: CSS3 and Selectors

::selection

Page 68: CSS3 and Selectors

COMBINATORS

Combining selectors to target elements

Page 69: CSS3 and Selectors

DESCENDANT SELECTOR

Select all elements that are descendants of a specified parent

Page 70: CSS3 and Selectors

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

DESCENDANT SELECTOR

Page 71: CSS3 and Selectors

CHILD SELECTOR

Select all elements that are immediate children of a specified parent

Page 72: CSS3 and 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

Page 73: CSS3 and Selectors

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

CHILD SELECTOR

Page 74: CSS3 and Selectors

Child Selector

Page 75: CSS3 and Selectors

ADJACENT SIBLING

Select elements that are the adjacent siblings of an element

Page 76: CSS3 and Selectors

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

ADJACENT SIBLING

Page 77: CSS3 and Selectors

Adjacent Sibling

Page 78: CSS3 and Selectors

GENERAL SIBLING

Select elements that are the siblings of an element

Page 79: CSS3 and Selectors

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

GENERAL SIBLING

Page 80: CSS3 and Selectors

General Sibling

Page 81: CSS3 and Selectors

ATTRIBUTE SELECTORS

Select elements based on attributes

Page 82: CSS3 and Selectors

form input[type="text"] {

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

ATTRIBUTE SELECTORS

Page 83: CSS3 and Selectors

Attribute Selectors

Page 84: CSS3 and Selectors

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

ATTRIBUTE SELECTORS

Page 85: CSS3 and Selectors

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

ATTRIBUTE SELECTORS

Page 86: CSS3 and Selectors

BROWSER

SUPPORT

Page 87: CSS3 and Selectors

Browser Support

Page 88: CSS3 and Selectors

DOES IT

MATTER?

Page 89: CSS3 and Selectors
Page 90: CSS3 and Selectors
Page 91: CSS3 and Selectors

YES, IT

MATTERS.

Page 92: CSS3 and Selectors

JAVASCRIPT

Plug the holes in browser support using JavaScript.

Page 93: CSS3 and 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>

Page 94: CSS3 and 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>

Page 95: CSS3 and 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>

Page 96: CSS3 and Selectors

CSS “POLYFILLS”

plugging the holes in support

Page 97: CSS3 and Selectors

WHAT IS A POLYFILL?

A polyfill, or polyfiller, is a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively.

http://remysharp.com/2010/10/08/what-is-a-polyfill/

Page 98: CSS3 and Selectors

SELECTIVIZR

http://selectivizr.com/

Page 99: CSS3 and Selectors

ECSSTENDER

http://ecsstender.org/

Page 100: CSS3 and Selectors

CSS3 WORKFLOW

How I approach my projects.

Page 101: CSS3 and Selectors

CSS3 WORKFLOW

Develop without any polyfill or JavaScript fixes in place.

Page 102: CSS3 and Selectors

CSS3 WORKFLOW

Validate.

Page 103: CSS3 and Selectors

CSS3 WORKFLOW

Test & Fix in older browsers

Page 104: CSS3 and Selectors

CSS3 WORKFLOW

Decide if you need to use a polyfill and which kind

Page 105: CSS3 and Selectors

CSS3 WORKFLOW

Test again.

Page 106: CSS3 and Selectors

THANK YOU!

@rachelandrew

rachelandrew.co.ukedgeofmyseat.com

grabaperch.com