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
300
Start using CSS Grid Layout
rachelandrew
2
500
The New CSS Layout
rachelandrew
4
340
Your Speakers
rachelandrew
1
990
Flexible Boxes and Grids
rachelandrew
1
170
Configuration Management with Puppet for Developers
rachelandrew
1
130
Food hacking
rachelandrew
2
260
CSS Grid Layout for Paris Web
rachelandrew
0
1.1k
The business of front-end development
rachelandrew
1
600
Other Decks in Technology
See All in Technology
Iceberg Meetup Japan #1 : Iceberg and Databricks
databricksjapan
0
330
依存パッケージの更新はコツコツが勝つコツ! / phpcon_nagoya2025
blue_goheimochi
3
210
コンテナサプライチェーンセキュリティ
kyohmizu
1
140
急成長する企業で作った、エンジニアが輝ける制度/ 20250227 Rinto Ikenoue
shift_evolve
0
120
Windows の新しい管理者保護モード
murachiakira
0
200
EDRの検知の仕組みと検知回避について
chayakonanaika
11
4.7k
アジャイルな開発チームでテスト戦略の話は誰がする? / Who Talks About Test Strategy?
ak1210
1
480
組織におけるCCoEの役割とAWS活用事例
nrinetcom
PRO
4
120
Aurora PostgreSQLがCloudWatch Logsに 出力するログの課金を削減してみる #jawsdays2025
non97
1
180
クラウド食堂とは?
hiyanger
0
110
手を動かしてレベルアップしよう!
maruto
0
200
PHPで印刷所に入稿できる名札データを作る / Generating Print-Ready Name Tag Data with PHP
tomzoh
0
180
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
640
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Facilitating Awesome Meetings
lara
52
6.2k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
370
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="name@example.com" /> 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