Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CSS3 Selectors - ConFoo 2014
Search
Rachel Andrew
February 27, 2014
Technology
4
230
CSS3 Selectors - ConFoo 2014
My talk on CSS3 Selectors - with a bit of bonus info on the Level 4 Selectors Module.
Rachel Andrew
February 27, 2014
Tweet
Share
More Decks by Rachel Andrew
See All by Rachel Andrew
MirrorConf: Solving Layout Problems with CSS Grid & Friends
rachelandrew
1
290
Start using CSS Grid Layout
rachelandrew
2
480
The New CSS Layout
rachelandrew
4
330
Your Speakers
rachelandrew
1
980
Flexible Boxes and Grids
rachelandrew
1
160
Configuration Management with Puppet for Developers
rachelandrew
1
120
Food hacking
rachelandrew
2
260
CSS Grid Layout for Paris Web
rachelandrew
0
1.1k
The business of front-end development
rachelandrew
1
580
Other Decks in Technology
See All in Technology
AWS re:Invent 2024 ふりかえり勉強会
yhana
0
510
Opcodeを読んでいたら何故かphp-srcを読んでいた話
murashotaro
0
330
多領域インシデントマネジメントへの挑戦:ハードウェアとソフトウェアの融合が生む課題/Challenge to multidisciplinary incident management: Issues created by the fusion of hardware and software
bitkey
PRO
2
120
Zero Data Loss Autonomous Recovery Service サービス概要
oracle4engineer
PRO
1
4.8k
APIとはなにか
mikanichinose
0
120
12 Days of OpenAIから読み解く、生成AI 2025年のトレンド
shunsukeono_am
0
360
20241218_今年はSLI/SLOの導入を頑張ってました!
zepprix
0
180
最近のSfM手法まとめ - COLMAP / GLOMAPを中心に -
kwchrk
5
1.2k
podman_update_2024-12
orimanabu
1
290
pg_bigmをRustで実装する(第50回PostgreSQLアンカンファレンス@オンライン 発表資料)
shinyakato_
0
120
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
150
【令和最新版】ロボットシミュレータ Genesis x ROS 2で始める快適AIロボット開発
hakuturu583
1
270
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
244
12k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Gamification - CAS2011
davidbonilla
80
5.1k
Facilitating Awesome Meetings
lara
50
6.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
A designer walks into a library…
pauljervisheath
205
24k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Transcript
Rachel Andrew: ConFoo 2014 CSS3 Selectors Thursday, 27 February 14
what do we mean by CSS3? Thursday, 27 February 14
CSS3 is Modular Thursday, 27 February 14
Some CSS3 modules are more complete than others Thursday, 27
February 14
• Working Draft • Candidate Recommendation • Proposed Recommendation •
W3C Recommendation W3C Maturity Levels Thursday, 27 February 14
Some browsers and some (parts of) modules. CSS3 is supported
by browsers Thursday, 27 February 14
• W3C Recommendation • http://www.w3.org/TR/css3-selectors/ Selectors module Thursday, 27 February
14
h1 {} p {} .thing {} #uniquething {} .thing p
Basic Selectors Thursday, 27 February 14
The problem with CSS2 selectors Thursday, 27 February 14
<h1>My heading</h1> <p class=”first”> ... </p> <p> ... </p> Adding
classes Thursday, 27 February 14
CSS3 Selectors “Common sense” new selectors target elements more precisely
without adding classes Thursday, 27 February 14
Select elements based on attributes Attribute Selectors Thursday, 27 February
14
form input[type="text"] { } form input[type="submit"] { } Attribute Selectors
Thursday, 27 February 14
Attribute Selectors Thursday, 27 February 14
label[for="fContact"] { float: none; width: auto; } Attribute Selectors Thursday,
27 February 14
a[href ^="mailto:"] { padding-right: 20px; background-image:url(email.png); background-repeat: no-repeat; background-position: right
center; } Attribute Selectors Thursday, 27 February 14
Selecting of elements depending on their position Structural pseudo- class
selectors Thursday, 27 February 14
a:link {} a:visited {} Pseudo-Class Selectors Thursday, 27 February 14
a:hover {} a:active {} Dynamic Pseudo- Class Thursday, 27 February
14
target an element when it is the first child of
a parent element :first-child Thursday, 27 February 14
<div class=”wrapper”> <p> ... </p> <p> ... </p> <p> ...
</p> </div> :first-child Thursday, 27 February 14
:first-child Thursday, 27 February 14
.wrapper p { font-size: 1.5em; } :first-child Thursday, 27 February
14
.wrapper p:first-child { font-size: 1.5em; } :first-child Thursday, 27 February
14
:first-child Thursday, 27 February 14
target an element when it is the last child of
a parent element :last-child Thursday, 27 February 14
:last-child Thursday, 27 February 14
.navigation li:last-child { border-bottom: none; } :last-child Thursday, 27 February
14
:last-child Thursday, 27 February 14
select multiple elements according to their position in the document
tree :nth-child Thursday, 27 February 14
:nth-child Thursday, 27 February 14
tr:nth-child(odd) td { background-color: #f0e9c5; } :nth-child Thursday, 27 February
14
:nth-child Thursday, 27 February 14
tr:nth-child(3) td { background-color: #f0e9c5; } :nth-child Thursday, 27 February
14
:nth-child Thursday, 27 February 14
http://css-tricks.com/how-nth-child-works/ tr:nth-child(2n+1) td { background-color: #f0e9c5; } :nth-child Thursday, 27
February 14
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 Thursday, 27 February 14
p:nth-of-type(odd) { background-color: #f0e9c5; } :nth-of-type Thursday, 27 February 14
:nth-of-type Thursday, 27 February 14
matches an element if it is the only child element
of it’s parent. :only-child Thursday, 27 February 14
li { list-style-type: disc; } li:only-child { list-style-type: none; }
:only-child Thursday, 27 February 14
:only-child Thursday, 27 February 14
matches an element if it is empty :empty Thursday, 27
February 14
p { padding: 0 0 1em 0; margin: 0; }
p:empty { padding: 0; } :empty Thursday, 27 February 14
Structural pseudo-classes for use with forms. For input elements Thursday,
27 February 14
the checked state of a checkbox or radio button :checked
Thursday, 27 February 14
.agreeterms:checked { border:2px solid blue; } :checked Thursday, 27 February
14
detecting input element states enabled and disabled Thursday, 27 February
14
input:enabled { color: #000; } :enabled Thursday, 27 February 14
input:disabled { color: #999; } :disabled Thursday, 27 February 14
http://www.w3.org/TR/css3-ui/ (Working Draft) The CSS3 Basic User Interface Module Thursday,
27 February 14
:default :valid :invalid :in-range :out-of-range :required :optional :read-only :read-write CSS3
User Interface Module Thursday, 27 February 14
<input type="text" name="fName" id="fName" required="required" /> <input type="email" name="fEmail" id="fEmail"
required="required" placeholder="
[email protected]
" /> HTML5 attributes Thursday, 27 February 14
input:focus:required:invalid { background-image: url(error.png); background-position: 98% center; background-repeat: no-repeat; }
Adding an icon to required fields Thursday, 27 February 14
Adding an icon to required fields Thursday, 27 February 14
input:required:valid { background-image: url(accept.png); background-position: 98% center; background-repeat: no-repeat; }
Adding an icon to valid fields Thursday, 27 February 14
Adding an icon to valid fields Thursday, 27 February 14
input[type="email"]:focus:required:invalid { background-image: url(email_error.png); } Show a different icon for
the field type Thursday, 27 February 14
Show a different icon for the field type Thursday, 27
February 14
:not(selector) the Negation pseudo-class Thursday, 27 February 14
<p> ... </p> <p class=”box”> ... </p> <p> ... </p>
:not Thursday, 27 February 14
p:not(.box) { padding: 1em; border:2px solid #000; } :not Thursday,
27 February 14
:not Thursday, 27 February 14
matching virtual elements that don’t explicitly exist in the document
tree Pseudo-elements Thursday, 27 February 14
the first character of the first line of text :first-letter
Thursday, 27 February 14
.wrapper:first-letter { font-size: 200%; font-weight: bold; color: red; } :first-letter
Thursday, 27 February 14
:first-letter Thursday, 27 February 14
the first formatted line of text :first-line Thursday, 27 February
14
.wrapper:first-line { font-size: 200%; font-weight: bold; color: red; } :first-line
Thursday, 27 February 14
:first-line Thursday, 27 February 14
Render content before the element when using generated content :before
Thursday, 27 February 14
<div class=”content”> ... </div> :before Thursday, 27 February 14
.content:before { content: "Start here:"; } :before Thursday, 27 February
14
:before Thursday, 27 February 14
Render content after the element when using generated content :after
Thursday, 27 February 14
.content:after { content: "End here:"; } :after Thursday, 27 February
14
Generated content can be very useful... Thursday, 27 February 14
CSS Arrow Please Thursday, 27 February 14
Adding a bird Thursday, 27 February 14
.clearfix:after { visibility: hidden; display: block; font-size: 0; content: "
"; clear: both; height: 0; } .clearfix { display: inline-block; } /* start commented backslash hack \*/ * html .clearfix { height: 1%; } .clearfix { display: block; } /* close commented backslash hack */ Clear Fix Thursday, 27 February 14
Content selected in browser by the user ::selection Thursday, 27
February 14
.wrapper p::selection { background-color: red; } ::selection Thursday, 27 February
14
::selection Thursday, 27 February 14
Combining selectors to target elements Combinators Thursday, 27 February 14
Select all elements that are descendants of a specified parent
Descendant Selector Thursday, 27 February 14
.wrapper p { font-size: 1.5em; } Descendant Selector Thursday, 27
February 14
Select all elements that are immediate children of a specified
parent Child Selector Thursday, 27 February 14
<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 Thursday, 27 February 14
li { color: #000; } ul > li { color:
red; } Child Selector Thursday, 27 February 14
Child Selector Thursday, 27 February 14
Select elements that are the adjacent siblings of an element
Adjacent Sibling Thursday, 27 February 14
.wrapper h1 + p { font-size: 1.5em; } Adjacent Sibling
Thursday, 27 February 14
Adjacent Sibling Thursday, 27 February 14
Select elements that are the siblings of an element General
Sibling Thursday, 27 February 14
.wrapper h2~p { color: red; } General Sibling Thursday, 27
February 14
General Sibling Thursday, 27 February 14
Browser Support Thursday, 27 February 14
Browser Support Thursday, 27 February 14
or serve a simpler layout to older browsers Do Nothing.
Thursday, 27 February 14
Plug the holes in browser support using JavaScript. JavaScript Thursday,
27 February 14
div#wrapper p:first-child, div#wrapper p.first { font-size: 1.5em; } <script src="http://code.jquery.com/
jquery-latest.js"></script> <script> $(document).ready(function(){ $("div#wrapper p:first- child").addClass("first"); }); </script> jQuery: first-child Thursday, 27 February 14
tr:nth-child(odd) td, td.odd { background-color: #f0e9c5; } <script src="http://code.jquery.com/ jquery-latest.js"></script>
<script> $(document).ready(function(){ $("tr:nth-child(odd) td").addClass("odd"); }); </script> jQuery: nth-child Thursday, 27 February 14
plugging the holes in support CSS “PolyFills” Thursday, 27 February
14
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? Thursday, 27 February 14
http://selectivizr.com/ selectivizr Thursday, 27 February 14
Check your stats. Thursday, 27 February 14
Selectors Level 4 a look to the near future with
“CSS4 Selectors” Thursday, 27 February 14
the Negation Pseudo-class :not gets an upgrade in Level 4
Thursday, 27 February 14
:not Level 4 enables the passing of multiple selectors to
:not p:not(.excerpt, .intro) { font-weight: normal; } Thursday, 27 February 14
The Matches pseudo-class Applying rules to groups of selectors. Thursday,
27 February 14
:matches p:matches(.alert,.error,.warn) { color: red; } Thursday, 27 February 14
The local-link Pseudo-class Allows an author to style links depending
on the visitor’s location in the site. For example to differentiate between local and external links. Thursday, 27 February 14
:local-link target links that are in the same document. a:local-link
{ color: blue; } Thursday, 27 February 14
:local-link(0) Links in the same domain - local links. a:local-link(0)
{ border-bottom: 1px dashed blue; } Thursday, 27 February 14
:local-link Page: http://example.com/blog/foo Link A: http://example.com/blog/foo/a-post Link B: http://example.com/blog/bar/a-post a:local-link(1)
{ color: red; } a:local-link(2) { color: green; } Thursday, 27 February 14
Time Dimensional Pseudo-classes Defines :current :past and :future - useful
to show progress through a document, for example when displaying subtitles. Thursday, 27 February 14
:current :past p:current { color: blue; } p:past { color:
#999; } Thursday, 27 February 14
Tree Structural Pseudo-classes Taking nth-child to the next level with
:nth-match and :nth-last-match Thursday, 27 February 14
:nth-match tr:nth-match(even of .active .new) { background-color: yellow; } Thursday,
27 February 14
Grid Structural Pseudo-classes :column, :nth-column, :nth-last-column Thursday, 27 February 14
Grid Structural Pseudo-Classes :nth-column(even) { background: blue; } Thursday, 27
February 14
The “Parent Selector” Identifying the subject of a selector with
a ! Thursday, 27 February 14
Identifying the subject of the selector <ul> <li><a href=”home”>Home</a></li> <li><a
class=”active”>About</a></ li> </ul> ul li a.active { color: blue; } ul li! a.active { border:1px solid blue; } Thursday, 27 February 14
CSS Level 4 selectors Browsers are beginning to implement these
selectors. See what your browser supports: http://css4-selectors.com/browser-selector-test/ Thursday, 27 February 14
http://www.rachelandrew.co.uk/presentations/css3-selectors @rachelandrew Thank You! Thursday, 27 February 14