205-02

Combinator selectors

Slide instructions

SPACEBAR to move forward through slides.

SHIFT & SPACEBAR to move backwards through slides.

LEFT ARROW & RIGHT ARROW to move through sections.

ESC to see overview and ESC again to exit.

F to enter presentation mode and ESC to exit.

Introduction

Combinators allow you to combine individual selectors into new types of selectors.

Descendant combinators (CSS1)

Descendant combinators are written using two or more selectors separated by whitespace.


/* syntax */
E F { }

/* example */
ul li a { }

How does it work?

Descendant combinators target only elements that are descendants of other elements.

In the following example, the <a> that is a descendant of the <p> will be selected, but not the <a> that is a descendant of the <div>.


/* CSS selector */
p a { }

<!-- HTML markup -->
<p>
  <a></a>
</p>
<div>
  <a></a>
</div>

The key to descendant combinators is understanding paths to elements. There is a path to every element starting with the <html> element.

Paths can be written in full or in part to the element or a partial path, depending on your need.


/* example paths */
html body .container .nav ul li a { }
body .container .nav ul li a { }
.container .nav ul li a { }
.nav ul li a { }
ul li a { }
ul a { }
li a { }
a { }

When writing descendant combinators, try to keep your selectors as short as possible. Each selector should only be as specific as needed.


/* unnecessarily long */
.nav ul li a { }

/* only as specific as needed */
.nav a { }

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

Descendant combinators are well supported across all modern browsers.

Exercise 1: Write a descendant combinator

Open this HTML file in an editor:
exercises-start/exercise-205-02-01.html

Write a CSS rule to style the <a> element to be color: firebrick.

However, only style the <a> element inside the <p> element (Link 2), not the <a> element inside the <div> element (Link 1).


<div>
  <a href="#">Link 1</a>
</div>
<p>
  <a href="#">Link 2</a>
</p>

/* Add styles here */
p a {
   color: firebrick;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-02-01.html

Child combinators (CSS2)

Child combinators are written using two selectors separated by a ">".


/* syntax */
E > F { }

/* example */
p > a { }

Whitespace within the selector is optional.


/* Valid */
p > a { }
p>a { }

How does it work?

Child combinators target any element that is a direct child of another element.

Only child elements, rather than descendant elements, will be selected.

In the following example, the <a> that is a child of the <div> will be selected, but not the <a> that is a descendant of the <div>.


/* CSS Selector */
div > a { }

<!-- HTML markup -->
<div>
  <a></a>
</div>
<div>
  <p> <a></a> </p>
</div>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

Child combinators are not supported by Internet Explorer 6.

Exercise 2: Write a child combinator

Open this HTML file in an editor:
exercises-start/exercise-205-02-02.html

Write a CSS rule to style the <a> element to be color: forestgreen.

However, only style the <a> that is a child of the <div> element (Link 1), not the <a> that is a descendant of the <div> element (Link 2).


<div>
  <a href="#">Link 1</a>
</div>
<div>
  <p>
    <a href="#">Link 2</a>
  </p>
</div>

/* Add styles here */
div > a {
  color: forestgreen;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-02-01.html

Adjacent sibling combinators (CSS2)

Adjacent sibling combinators are written using two selectors separated by a "+".


/* syntax */
E + F { }

/* example */
h2 + h3 { }

Whitespace within the selector is optional.


/* Valid */
h2 + h3 { }
h2+h3 { }

How does it work?

Adjacent sibling combinators target the sibling immediately following a defined element.

In the following example, only the <h3> that is adjacent to (or comes directly after) the <h2> will be selected.


/* CSS selector */
h2 + h3 { }

<!-- HTML markup -->
<h2>Content</h2>
<h3>Content</h3>
<h3>Content</h3>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

Adjacent sibling combinators are not supported by Internet Explorer 6.

Exercise 3: Write an adjacent sibling combinator

Open this HTML file in an editor:
exercises-start/exercise-205-02-03.html

Write a CSS rule to style the <h3> element to be color: hotpink.

However, only style the <h3> that comes directly after the <h2> element.


<h2>Heading level 2</h2>
<h3>Heading level 3</h3>
<h3>Heading level 3</h3>

/* Add styles here */
h2 + h3 {
  color: hotpink;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-02-03.html

General sibling combinators (CSS3)

General sibling combinators are written using two selectors separated by a "~".


/* syntax */
E ~ F { }

/* example */
h2 ~ h3 { }

Whitespace within the selector is optional.


/* Valid */
h2 ~ h3 { }
h2~h3 { }

How does it work?

General sibling combinators target any sibling that follows a defined element.

In the following example, any <h3> that appears after the <h2> in source order will be selected - as long as they share the same parent.


/* CSS selector */
h2 ~ h3 { }

<!-- HTML markup -->
<h2>Content</h2>
<h3>Content</h3>
<h3>Content</h3>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

Support

General sibling combinators are not supported by Internet Explorer 6.

Exercise 4: Write a general sibling combinator

Open this HTML file in an editor:
exercises-start/exercise-205-02-04.html

Write a CSS rule to style the <h3> element to be color: lawngreen.

However, only style all <h3> elements that come after the <h2> element, rather than those that come before.


<h3>Heading level 3</h3>
<h2>Heading level 2</h2>
<h3>Heading level 3</h3>
<h3>Heading level 3</h3>

/* Add styles here */
h2 ~ h3 {
  color: lawngreen;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-02-04.html

Russ Weakley

Site Twitter Github Slideshare Linkedin

Next deck

205-03: Link pseudo-classes