CSS3 is a wonderful thing, but it’s easy to be bamboozled by the transforms and animations (many of which are vendor-specific) and forget about the nuts-and-bolts selectors that have also been added to the specification. A number of powerful new pseudo-selectors (16 are listed in the latest W3C spec) enable us to select elements based on a range of new criteria.
Before we look at these new CSS3 pseudo-classes, let’s briefly delve into the dusty past of the Web and chart the journey of these often misunderstood selectors.
There are a couple of ways to indicate the language of a document, and if you’re using HTML5, it’ll likely be by putting
You may have already used
Sixteen new pseudo-classes have been introduced as part of the W3C’s CSS Proposed Recommendation, and they are broken down into four groups: structural pseudo-classes, pseudo-classes for the states of UI elements, a target pseudo-class and a negation pseudo-class.
The W3C is the home of CSS.
Let’s now run through the 16 new pseudo-selectors one at a time and see how each is used. I’ll use the same notation for naming classes that the W3C uses, where
Note that we could arguably use ID and class selectors for much of this form, but it’s a great opportunity to take our new pseudo-classes out for a spin and demonstrate how you might use them in a real-world example. Here’s the HTML:
Our form, before and after.
Level 3 selectors on the W3C website.
It’s worth noting that you could style the
iPhone Form Example
Let’s move over to our sample code and give the document some basic text and background styles:
This would highlight every other row in an unordered list. You might find this technique extremely handy when using tables. For example:
The
Note that
We can also use some simple algebra to make things even more exciting. Consider the following:
Whenever we use
This gives us:
This gives us:
Let’s say you want to select the first five items in a list. Here’s the CSS:
This gives us:
WebDesign & Such has created a demo of zebra striping, which is a perfect example of how you might use
Zebra striping a table with CSS3.
If none of your tables need styling, then you could do what Webvisionary Awards has done and use
The effect is subtle on the website, but it adds a layer of detail that would be missed in older browsers.
The :nth-child selectors in action on Webvisionary Awards.
iPhone Form Example
We could use
Here, we’re looking for the first three children of the
Before we look at these new CSS3 pseudo-classes, let’s briefly delve into the dusty past of the Web and chart the journey of these often misunderstood selectors.
[Editor's note: A must-have for professional Web designers and developers: The Printed Smashing Books Bundle is full of practical insight for your daily work. Get the bundle right away!]
A Brief History Of Pseudo-Classes
When the CSS1 spec was completed back in 1996, a few pseudo-selectors were included, many of which you probably use almost every day. For example::link
:visited
:hover
:active
, after which comes the name of the pseudo-class. It’s amazing to think that these pseudo-classes arrived on the scene before HTML4 was published by the W3C a year later in December 1997.CSS2 Arrives
Hot on the heels of CSS1 was CSS2, whose recommended spec was published just two years later in May 1998. Along with exciting things like positioning were new pseudo-classes::first-child
and :lang()
.:lang
There are a couple of ways to indicate the language of a document, and if you’re using HTML5, it’ll likely be by putting
just after the doc type (specifying your local language, of course). You can now use :lang(en)
to style elements on a page, which is useful when the language changes dynamically.:first-child
You may have already used
:first-child
in your documents. It is often used to add or remove a top border on the first element in a list. Strange, then, that it wasn’t accompanied by :last-child
; we had to wait until CSS3 was proposed before it could meet its brother.Why Use Pseudo-Classes?
What makes pseudo-classes so useful is that they allow you to style content dynamically. In the
example above, we are able to describe how links are styled when the user interacts with them. As we’ll see, the new pseudo-classes allow us to dynamically style content based on its position in the document or its state.Sixteen new pseudo-classes have been introduced as part of the W3C’s CSS Proposed Recommendation, and they are broken down into four groups: structural pseudo-classes, pseudo-classes for the states of UI elements, a target pseudo-class and a negation pseudo-class.
The W3C is the home of CSS.
Let’s now run through the 16 new pseudo-selectors one at a time and see how each is used. I’ll use the same notation for naming classes that the W3C uses, where
E
is the element, n
is a number and s
is a selector.Sample Code
For many of these new selectors, I’ll also refer to some sample code so that you can see what effect the CSS has. We’ll take a regular form and make it suitable for an iPhone using our new CSS3 pseudo-classes.Note that we could arguably use ID and class selectors for much of this form, but it’s a great opportunity to take our new pseudo-classes out for a spin and demonstrate how you might use them in a real-world example. Here’s the HTML:
01 | < form > |
02 | < hgroup > |
03 | < h1 >Awesome Widgets |
04 | < h2 >All the cool kids have got one :) |
05 |
|
06 | < fieldset id = "email" > |
07 | < legend >Where do we send your receipt? |
08 | < label for = "email" >Email Address |
09 | < input type = "email" name = "email" placeholder = "Email Address" /> |
10 |
|
11 |
12 | < fieldset id = "details" > |
13 | < legend >Personal Details |
14 | < select name = "title" id = "field_title" > |
15 | < option value = "" selected = "selected" >Title |
16 | < option value = "Mr" >Mr |
17 | < option value = "Mrs" >Mrs |
18 | < option value = "Miss" >Miss |
19 |
|
20 |
21 | < label for = "firstname" >First Name |
22 | < input name = "firstname" placeholder = "First Name" /> |
23 |
24 | < label for = "initial" >Initial |
25 | < input name = "initial" placeholder = "Initial" size = "3" /> |
26 |
27 | < label for = "surname" >Surname |
28 | < input name = "surname" placeholder = "Surname" /> |
29 |
|
30 |
31 | < fieldset id = "payment" > |
32 | < legend >Payment Details |
33 |
34 | < label for = "cardname" >Name on card |
35 | < input name = "cardname" placeholder = "Name on card" /> |
36 |
37 | < label for"cardnumber">Card number |
38 | < input name = "cardnumber" placeholder = "Card number" /> |
39 |
40 | < select name = "cardType" id = "field_cardType" > |
41 | < option value = "" selected = "selected" >Select Card Type |
42 | < option value = "1" >Visa |
43 | < option value = "2" >American Express |
44 | < option value = "3" >MasterCard |
45 |
|
46 |
47 | < label for = "cardExpiryMonth" >Expiry Date |
48 | < select id = "field_cardExpiryMonth" name = "cardExpiryMonth" > |
49 | < option selected = "selected" value = "mm" >MM |
50 | < option value = "01" >01 |
51 | < option value = "02" >02 |
52 | < option value = "03" >03 |
53 | < option value = "04" >04 |
54 | < option value = "05" >05 |
55 | < option value = "06" >06 |
56 | < option value = "07" >07 |
57 | < option value = "08" >08 |
58 | < option value = "09" >09 |
59 | < option value = "10" >10 |
60 | < option value = "11" >11 |
61 | < option value = "12" >12 |
62 |
|
63 | < select id = "field_cardExpiryYear" name = "cardExpiryYear" > |
64 | < option value = "yyyy" >YYYY |
65 | < option value = "2011" >11 |
66 | < option value = "2012" >12 |
67 | < option value = "2013" >13 |
68 | < option value = "2014" >14 |
69 | < option value = "2015" >15 |
70 | < option value = "2016" >16 |
71 | < option value = "2017" >17 |
72 | < option value = "2018" >18 |
73 | < option value = "2019" >19 |
74 |
|
75 |
76 | < label for"securitycode">Security code |
77 | < input name = "securitycode" type = "number" placeholder = "Security code" size = "3" /> |
78 |
79 | < p >Would you like Insurance? |
80 | < input type = "radio" name = "Insurance" id = "insuranceYes" /> |
81 | < label for = "insuranceYes" >Yes Please! |
82 | < input type = "radio" name = "Insurance" id = "insuranceNo" /> |
83 | < label for = "insuranceNo" >No thanks |
84 |
85 |
|
86 |
87 | < fieldset id = "submit" > |
88 | < button type = "submit" name = "Submit" disabled>Here I come! |
89 |
|
90 |
|
Our form, before and after.
1. Structural Pseudo-Classes
According to the W3C, structural pseudo-classes do the following:… permit selection based on extra information that lies in the document tree but cannot be represented by other simple selectors or combinators.What this means is that we have selectors that have been turbo-charged to dynamically select content based on its position in the document. So let’s start at the beginning of the document, with
:root
.Level 3 selectors on the W3C website.
E:root
The:root
pseudo-class selects the root element on the page. Ninety-nine times out of a hundred, this will be the
element. For example:1 | :root { background-color : #fcfcfc ; } |
element instead, which is perhaps a little more descriptive:1 | html { background-color : #fcfcfc ; } |
Let’s move over to our sample code and give the document some basic text and background styles:
1 | :root { |
2 | color : #fff ; |
3 | text-shadow : 0 -1px 0 rgba( 0 , 0 , 0 , 0.8 ); |
4 | background : url (…/images/background.png) no-repeat #282826 ; } |
E:nth-child(n)
The:nth-child()
selector might require a bit of experimentation to fully understand. The easiest implementation is to use the keywords odd
or even
, which are useful when displaying data that consists of rows or columns. For example, we could use the following:1 | ul li:nth-child(odd) { |
2 | background-color : #666 ; |
3 | color : #fff ; } |
1 | table tr:nth-child(even) { … } |
:nth-child
selector can be much more specific and flexible, though. You could select only the third element from a list, like so:1 | li:nth-child( 3 ) { … } |
n
does not start at zero, as it might in an array. The first element is :nth-child(1)
, the second is :nth-child(2)
and so on.We can also use some simple algebra to make things even more exciting. Consider the following:
1 | li:nth-child( 2 n) { … } |
n
in this way, it stands for all positive integers (until the document runs out of elements to select!). In this instance, it would select the following list items:- Nothing (2 × 0)
- 2nd element (2 × 1)
- 4th element (2 × 2)
- 6th element (2 × 3)
- 8th element (2 × 4)
- etc.
nth-child(even)
. So, let’s mix things up a bit:1 | li:nth-child( 5 n) { … } |
- Nothing (5 × 0)
- 5th element (5 × 1)
- 10th element (5 × 2)
- 15th element (5 × 3)
- 20th element (5 × 4)
- etc.
1 | li:nth-child( 4 n + 1 ) { … } |
- 1st element ((4 × 0) + 1)
- 5th element ((4 × 1) + 1)
- 9th element ((4 × 2) + 1)
- 13th element ((4 × 3) + 1)
- 17th element ((4 × 4) + 1)
- etc.
n
as negative, you’ll be able to select the first x number of items like so:1 | li:nth-child(-n + x) { … } |
1 | li:nth-child(-n + 5 ) { … } |
- 5th element (-0 + 5)
- 4th element (-1 + 5)
- 3rd element (-2 + 5)
- 2nd element (-3 + 5)
- 1st element (-4 + 5)
- Nothing (-5 + 5)
- Nothing (-6 + 5)
- etc.
WebDesign & Such has created a demo of zebra striping, which is a perfect example of how you might use
nth-child
in practice.Zebra striping a table with CSS3.
If none of your tables need styling, then you could do what Webvisionary Awards has done and use
:nth-child
to style alternating sections of its website. Here’s the CSS:1 | section > section:nth-child(even) { |
2 | background :rgba( 255 , 255 , 255 ,. 1 ) |
3 | url ( "../images/hr-damaged2.png" ) 0 bottom no-repeat ; |
4 | } |
The :nth-child selectors in action on Webvisionary Awards.
iPhone Form Example
We could use
:nth-child
in a few places in our iPhone form example, but let’s focus on one. We want to hide the labels for the first three fieldsets from view and use the placeholder text instead. Here’s the CSS:1 | form:nth-child(-n+ 3 ) label { display : none ; } |
No comments:
Post a Comment