Basic Selectors

<ul> 
 <li id="myID" class="myClass">item 1</li>
 <li class="myClass">item 2</li>    
 <li>item 3</li>
</ul> 
#myID
ID
.myClass
class
li
tag name

Relational selectors

  1. item 1
  2. item 2
  3. item 3
    • item a
    • item b
    • item c
  4. item 4
  5. item 5
  6. item 6
  7. item 7
ul li
descendant selector
matches nested <li>s
ol > li
child selector 
matches <li>s in <ol> but not nested <ul>
li.myClass + li
adjacent sibling 
NEW  (IE7+)
li.myClass ~ li
general sibling selector
matches later siblings, but not nested.

Try it out

Lots 'o Selectors

  • *
  • E
  • .class
  • #id
  • E F
  • E > F
  • E + F
  • E[attribute]
  • E[attribute=value]
  • E[attribute~=value]
  • E[attribute|=value]
  • :first-child
  • :lang()
  • :before
  • ::before
  • ::selection
  • :after
  • ::after
  • :first-letter
  • ::first-letter
  • :first-line
  • ::first-line
  • E[attribute^=value]
  • E[attribute$=value]
  • E[attribute*=value]
  • E ~ F
  • :root
  • :last-child
  • :only-child
  • :nth-child()
  • :nth-last-child()
  • :first-of-type
  • :last-of-type
  • :only-of-type
  • :nth-of-type()
  • :nth-last-of-type()
  • :empty
  • :not()
  • :target
  • :enabled
  • :disabled
  • :checked
  • :indeterminate
  • :default
  • :valid
  • :invalid
  • :in-range
  • :out-of-range
  • :required
  • :optional
  • :read-only
  • :read-write

Attribute selectors

element[attribute]
Select elements containing the named attribute
img[alt] {}
     <img src="image.jpg" alt="accessible">     
     <img src="image.jpg" title="inaccessible">  
form [type] {} 
     <input type=date>  
     <select>

IE6 sucks 
IE7 support basic non-empty attribute

Older attribute selectors

E[attr]
Element E that has the attribute attr with any value.
E[attr="val"]
Element E that has the attribute attr with the exact, case-sensitive if attribute is case sensitive, value val.
E[attr|=val]
Element E whose attribute attr has a value val or begins with val- ("val" plus "-").
p[lang|="en"]{/*    <p lang="en-us">  <p lang="en-uk"> */ }
E[attr~=val]
Element E whose attribute attr has within its value the space-separated full word val.
a[title~=more] {/* <a title="want more info about this?">}

CSS3 Attribute Selectors

E[attr^=val]
Element E whose attribute attr starts with the value val.
a[href^=mailto] {background-image: url(emailicon.gif);}  
a[href^=http]:after {content: " (" attr(href) ")";}
E[attr$=val]
Element E whose attribute attr ends in val . 
a[href$=pdf] {background-image: url(pdficon.gif);}
a[href$=pdf]:after {content: " (PDF)";}
E[attr*=val]
Element E whose attribute attr matches val anywhere within the attribute. Similar to E[attr~=val] above, except the val can be part of a word.

Note: Multiple attributes work! a[title][href]

Attribute Selectors Recap

input[placeholder] { 
/* matches any input with a placeholder */}
input[type=email] {
/* exact match */}
abbr[title~=unicorn] { 
/* matches unicorn but not unicorns */}
abbr[title|=en] { 
/* matches en-us and en-uk */}
a[href^=mailto] { 
/* starts with */}
a[href$=pdf]{ 
/* ends in */}
abbr[title*=unicorn] { 
/* matches unicorn and unicorns */}
E:[att] 
/* have the attribute at all */
E:[att=val]
/* exact */
E:[att~=val]
/* val is a space separated word */
E:[att|=val]
/* with a dash */
E:[att^=val]
/* begins with val */
E:[att$=val]
/* ends with val */
E:[att*=val]
/* val is anywhere as a substring */
.
@media print{
  abbr[title]:after { 
    content: "(" attr(title) ")";
  }
  a[href^=http]:after { 
    content: "(" attr(href) ")";
  }
}

Using attribute selectors

Play Time: Attribute Selectors

Note: The top line of the example is editable. The CSS will impact the contents on the rest of the page.

UI pseudo-classes

Based on current state of UI

  :enabled
  
  :disabled
  
  :checked
  

Example:

input[type=checkbox]:checked + label {
  color: red;
}

Form related UI pseudo-classes

:valid
:invalid
:required
:optional


:in-range
:out-of-range
:read-only
:read-write

:default

Example:

input:valid { border: 1px solid green;}
input:invalid { border: 1px solid red;}
input:required {border-width: 5px;}
input:optional {border-width: 10px;}
input:out-of-range { background-color: pink;}
input:in-range { background-color:lightgreen;}

UI selectors

Play Time: UI Selectors

Test them out for yourselves

Structural selectors

:nth-child() 

:nth-last-child() 

:nth-of-type() 

:nth-last-of-type() 

:first-child*

:last-child 

:first-of-type 

:last-of-type 

:only-child 

:only-of-type 

:root

:empty

:not(:empty) 
  • Target elements on the page based on their relationships to other elements in the DOM.
  • Updates dynamically if page updates.
  • Reduced need for extra markup, classes and IDs
* CSS2 / IE8

First, last, & only

 
:first-child

:last-child 

:first-of-type 

:last-of-type 

:only-child 

:only-of-type 

Easier to explain by example

nth pseudo-classes

:nth-child(3n) 

:nth-last-child(odd) 

:nth-of-type(5) 

:nth-last-of-type(3n+1) 

Target element or elements based on argument passed to the selector

  • :nth-of-type(even)
  • :nth-of-type(odd)
  • :nth-of-type(an+b)

Let's Play Note: Play with negative values for 'a'

Structural Selectors

tr:first-child,
tr:last-child {
  font-weight: bold;
}
tr:first-of-type,
tr:last-of-type{
  text-decoration:line-through;
}
tr:nth-child(even) {
  background-color: #CCC;
}
tr:nth-child(3) {
  color: #CCC;
}
tr:nth-of-type(odd) {
  background-color: #FFF;
}
tr:nth-of-type(4n) {
  color: #C61800;
}
tr:nth-of-type(3n-1) {
  font-style: italic;
}

Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10

Structural Selectors

  • 1
  • 1
  • 2
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 4
 li:first-of-type:last-of-type{width: 100%;}
 li:nth-of-type(1):nth-last-of-type(2),
 li:nth-of-type(2):nth-last-of-type(1){width: 50%;}
 li:nth-of-type(1):nth-last-of-type(3),
 li:nth-of-type(3):nth-last-of-type(1),
 li:nth-of-type(2):nth-last-of-type(2){width: 33.33%;}
 li:nth-of-type(1):nth-last-of-type(4),
 li:nth-of-type(2):nth-last-of-type(3),
 li:nth-of-type(3):nth-last-of-type(2),
 li:nth-of-type(4):nth-last-of-type(1){width: 25%;}

:empty pseudo-class

p:empty {
  	/* styles here */
    }

Matches:

<p></p>
<p><!-- this is a comment --></p>

:not - Negation pseudo-class

 div:not(.excludeMe)

Practice makes perfect

Other Pseudo Classes

  :lang
  :target
  

link & user action pseudo-classes

  :link
  :visited
  
  :hover
  :active
  :focus
  

:target example

:root

 
:root

Selects the document root, which is <html>

  • Declare font-size on :root if using rem units
  • Style :root only if showing <head> (as in our exercise files)
  • In CSS4, define Defining Variables on root. (see Variables module)

example

Pseudo elements

Pseudo elements

::first-line
::first-letter
::selection (not in specification)
::before
::after
    

Pseudo-classes select elements that already exist.
Pseudo-elements create "faux" elements you can style.

Use single colon because of IE

::first-letter

You can even pretty drop-caps with :first-letter, but make sure to apply it to p:first-of-type so your site not too ugly. Code for drop cap.

p:first-of-type::first-letter {
	position: relative;
	top: 8px;
	float: left;
	font-size: 3em;
	line-height: 1;
	color: #C7573A;
	padding: 0 4px 2px 0;
	font-weight: bold;	
}
 

::selection

.thisSlide *::selection {
   background-color: #C61800;
   color: #ffffff;
}
    

Prefix with -moz- for Firefox
Use single colon because of IE

Tips

  • selectivizr.com + JS Library = JS for backward compatibility
  • jQuery $(selector) == document.querySelectorAll(selector)
  • Many, many more pseudo-elements with prefixes currently treated as a shadow DOM
    • ::-webkit-progress-bar
    • ::-webkit-progress-value
    • ::-webkit-meter-even-less-good-value
    • ::-webkit-inner-spin-button /outer-spin-button
    • ::-webkit-validation-bubble / arrow-clipper /arrow /message
    • ::-webkit-scrollbar
    • ::-webkit-scrollbar-button / thumb / track
    • Mozilla Pseudo-elements and pseudo-classes

Specificity

Next ➹