css selectors

121
Rachel Andrew: ConFoo 2015 CSS Selectors

Upload: rachel-andrew

Post on 16-Jul-2015

755 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: CSS Selectors

Rachel Andrew: ConFoo 2015

CSS Selectors

Page 2: CSS Selectors

what do we mean by CSS3?

Page 3: CSS Selectors

CSS3 is Modular

Page 4: CSS Selectors

CSS3 doesn’t really exist …

Page 5: CSS Selectors

• Editor’s Draft

• Working Draft

• Candidate Recommendation

• Proposed Recommendation

• W3C Recommendation

W3C Maturity Levels

Page 6: CSS Selectors

• W3C Recommendation

• http://www.w3.org/TR/selectors/

Selectors module Level 3

Page 7: CSS Selectors

Selectors Module Level 4

• Latest Editor’s Draft 4 February 2015

• http://dev.w3.org/csswg/selectors-4/

Page 8: CSS Selectors

h1 {}

p {}

.thing {}

#uniquething {}

.thing p

Basic Selectors

Page 9: CSS Selectors

The problem with CSS2 selectors

Page 10: CSS Selectors

<h1>My heading</h1>

<p class="first"> ... </p>

<p> ... </p>

Adding classes

Page 11: CSS Selectors

Level 3 Selectors

“Common sense” new selectors

target elements more precisely without adding classes

Page 12: CSS Selectors

Select elements based on attributes

Attribute Selectors

Page 13: CSS Selectors

form input[type="text"] {

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

Attribute Selectors

Page 14: CSS Selectors

Attribute Selectors

Page 15: CSS Selectors

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

Attribute Selectors

Page 16: CSS Selectors

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

Attribute Selectors

Page 17: CSS Selectors

Selecting of elements depending on their position

Structural pseudo-class selectors

Page 18: CSS Selectors

What is a pseudo-class?

A pseudo-class selector acts as if you have added a class to an element in the HTML mark-up.

Page 19: CSS Selectors

a:link {} a:visited {}

Pseudo-Class Selectors

Page 20: CSS Selectors

a:hover {} a:active {}

Dynamic Pseudo-Class

Page 21: CSS Selectors

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

:first-child

Page 22: CSS Selectors

<div class="wrapper"> <p> ... </p> <p> ... </p> <p> ... </p> </div>

:first-child

Page 23: CSS Selectors

:first-child

Page 24: CSS Selectors

.wrapper p { font-size: 1.5em; }

:first-child

Page 25: CSS Selectors

.wrapper p:first-child { font-size: 1.5em; }

:first-child

Page 26: CSS Selectors

:first-child

Page 27: CSS Selectors

Pseudo-classes

To target an element with our pseudo-class we append the pseudo-class to the selector:

p:first-child

li:first-child

.classname:first-child

Page 28: CSS Selectors

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

:last-child

Page 29: CSS Selectors

:last-child

Page 30: CSS Selectors

.navigation li:last-child { border-bottom: none; }

:last-child

Page 31: CSS Selectors

:last-child

Page 32: CSS Selectors

select multiple elements according to their position in the document tree

:nth-child

Page 33: CSS Selectors

:nth-child

Page 34: CSS Selectors

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

:nth-child

Page 35: CSS Selectors

:nth-child

Page 36: CSS Selectors

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

:nth-child

Page 37: CSS Selectors

:nth-child

Page 38: CSS Selectors

http://css-tricks.com/how-nth-child-works/

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

:nth-child 2n+1 is the same as odd.

Page 39: CSS Selectors

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.

:nth-of-type

Page 40: CSS Selectors

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

:nth-of-type

Page 41: CSS Selectors

:nth-of-type

Page 42: CSS Selectors

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

:only-child

Page 43: CSS Selectors

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

:only-child

Page 44: CSS Selectors

:only-child

Page 45: CSS Selectors

matches an element if it is empty

:empty

Page 46: CSS Selectors

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

:empty

Page 47: CSS Selectors

pseudo-classes for use with forms.

UI Element States

Page 48: CSS Selectors

the checked state of a checkbox or radio button

:checked

Page 49: CSS Selectors

.agreeterms:checked { border:2px solid blue; }

:checked

Page 50: CSS Selectors

detecting input element states

enabled and disabled

Page 51: CSS Selectors

input:enabled { color: #000; }

:enabled

Page 52: CSS Selectors

input:disabled { color: #999; }

:disabled

Page 53: CSS Selectors

http://dev.w3.org/csswg/css-ui-3/ (latest editor’s draft)

The CSS3 Basic User Interface Module

Page 54: CSS Selectors

:default:valid :invalid :in-range :out-of-range :required :optional :read-only :read-write

CSS3 User Interface Module

Page 55: CSS Selectors

<input type="text" name="fName" id="fName" required="required" /> <input type="email" name="fEmail" id="fEmail" required="required" placeholder="[email protected]" />

HTML5 attributes

Page 56: CSS Selectors

input:focus:required:invalid { background-image: url(error.png); background-position: 98% center; background-repeat: no-repeat; }

Adding an icon to required fields

Page 57: CSS Selectors

Adding an icon to required fields

Page 58: CSS Selectors

input:required:valid { background-image: url(accept.png); background-position: 98% center; background-repeat: no-repeat; }

Adding an icon to valid fields

Page 59: CSS Selectors

Adding an icon to valid fields

Page 60: CSS Selectors

input[type="email"]:focus:required:invalid { background-image: url(email_error.png); }

Show a different icon for the field type

Page 61: CSS Selectors

Show a different icon for the field type

Page 62: CSS Selectors

:not(selector)

the Negation pseudo-class

Page 63: CSS Selectors

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

:not

Page 64: CSS Selectors

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

:not

Page 65: CSS Selectors

:not

Page 66: CSS Selectors

matching virtual elements that don’t explicitly exist in the document tree

Pseudo-elements

Page 67: CSS Selectors

What is a pseudo-element?

Pseudo-element selectors act as if a new element, such as a span, was added to your document and then the style applied to that.

Page 68: CSS Selectors

the first character of the first line of text

:first-letter

Page 69: CSS Selectors

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

:first-letter

Page 70: CSS Selectors

:first-letter

Page 71: CSS Selectors

the first formatted line of text

:first-line

Page 72: CSS Selectors

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

:first-line

Page 73: CSS Selectors

:first-line

Page 74: CSS Selectors

Render content before the element when using generated content

:before

Page 75: CSS Selectors

<div class="content"> ... </div>

:before

Page 76: CSS Selectors

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

:before

Page 77: CSS Selectors

:before

Page 78: CSS Selectors

Render content after the element when using generated content

:after

Page 79: CSS Selectors

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

:after

Page 80: CSS Selectors

Generated content can be very useful...

Page 81: CSS Selectors

CSS Arrow Please

Page 82: CSS Selectors

.clr:after { content: ""; display: table; clear: both; }

Clear Fix

Page 83: CSS Selectors

Generated Content is used in ebook creation.

@page:right{ @bottom-left { margin: 10pt 0 30pt 0; content: "My Book Title"; font-size: 8pt; color: #000; } }

Page 84: CSS Selectors

Combining selectors to target elements

Combinators

Page 85: CSS Selectors

Select all elements that are descendants of a specified parent

Descendant Selector

Page 86: CSS Selectors

.wrapper p { font-size: 1.5em; }

Descendant Selector

Page 87: CSS Selectors

Select all elements that are immediate children of a specified parent

Child Selector

Page 88: CSS 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 89: CSS Selectors

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

Child Selector

Page 90: CSS Selectors

Child Selector

Page 91: CSS Selectors

Select elements that are the adjacent siblings of an element

Adjacent Sibling

Page 92: CSS Selectors

.wrapper h1 + p { font-size: 1.5em; }

Adjacent Sibling

Page 93: CSS Selectors

Adjacent Sibling

Page 94: CSS Selectors

Select elements that are the siblings of an element

General Sibling

Page 95: CSS Selectors

.wrapper h2~p { color: red; }

General Sibling

Page 96: CSS Selectors

General Sibling

Page 97: CSS Selectors

Browser Support

Page 98: CSS Selectors

Browser Support

Page 99: CSS Selectors

or serve a simpler layout to older browsers

Do Nothing.

Page 100: CSS Selectors

plugging the holes in support

CSS “PolyFills”

Page 101: CSS Selectors

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/

What is a polyfill?

Page 102: CSS Selectors

http://selectivizr.com/

selectivizr

Page 103: CSS Selectors

Check your stats.

Page 104: CSS Selectors

Selectors Level 4a look to the near future with “CSS4 Selectors”

Page 105: CSS Selectors

the Negation Pseudo-class

:not gets an upgrade in Level 4

Page 106: CSS Selectors

:not

Level 4 enables the passing of multiple selectors to :not

p:not(.excerpt, .intro) { font-weight: normal; }

Page 107: CSS Selectors

The Matches pseudo-class

Applying rules to groups of selectors.

Page 108: CSS Selectors

:matches

p:matches(.alert,.error,.warn) { color: red; }

Page 109: CSS Selectors

Time Dimensional Pseudo-classes

Defines :current :past and :future - useful to show progress through a document, for example when displaying subtitles.

Page 110: CSS Selectors

:current :past

p:current { color: blue; }

p:past { color: #999; }

Page 111: CSS Selectors

Grid Structural Selectors

Column Combinator

Pseudo-classes:

:nth-column, :nth-last-column

Page 112: CSS Selectors

Column Combinator

The column combinator, which consists of two pipes (||) represents the relationship of a column element to a cell element belonging to the column it represents.

Page 113: CSS Selectors

Column Combinator col.totals || td {

background: #333; color: #fff; font-weight: bold; }

<table> <col span="2"> <col class="totals"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>3</td> <td>3</td> <td>6</td> </tr> </table>

Page 114: CSS Selectors

nth-column, nth-last-column

Selects the table cells inside a column according to the columns selected by nth-column or nth-last-column.

Page 115: CSS Selectors

Grid Structural Pseudo-Classes

:nth-column(even) { background: blue; }

Page 116: CSS Selectors

Selector Profiles

• CSS Selectors Level 4 introduces selector profiles

• The fast profile is suitable for all contexts, including in browser processing

• The complete profile is for things that can be used when performance isn’t an issue

• http://dev.w3.org/csswg/selectors-4/#profiles

Page 117: CSS Selectors

The has() selector

• Currently the only selector not available in the fast profile.

• This may change as the specification develops.

• Takes a selector list as an argument and will match if any of those selectors would match one element.

Page 118: CSS Selectors

Any a elements that contain an image will get a black border.

a:has( > img ) { border: 1px solid #000; }

Page 119: CSS Selectors

If an li does not contain a paragraph.

li:not(:has(p)) { padding-bottom: 1em; }

Page 120: CSS Selectors

CSS Level 4 selectors

Browsers are beginning to implement these selectors.

See what your browser supports: http://css4-selectors.com/browser-selector-test/

Page 121: CSS Selectors

http://www.rachelandrew.co.uk/presentations/css3-selectors

@rachelandrew

Thank You!