19 Learning Advanced CSS Selectors

 

This chapter covers

  • Learning the powerful ID and universal selectors
  • Leveling your style game with the descendant, child, and sibling selectors
  • Targeting your styles by combining two or more selectors
  • Becoming a style master by understanding CSS inheritance, cascading, and specificity

On the surface, CSS seems like a simple topic: you apply values to some properties, combine them into a rule, and then apply that rule to a page element. Repeat a few more times, and voilà: your page is beautiful. But the apparent simplicity of CSS is only skin deep. Underneath the straightforward implementation of declarations and rules are obscure caves of complexity and unfathomed depths of dynamism. This chapter serves as an introduction to this hidden world, which is home to some of the most powerful and practical CSS concepts.

Working with ID Selectors

In Chapter 7, I introduced you to CSS selectors, which enable you to specify the page object you want to style:

selector {
    property1: value1;
    property2: value2; 
    ...
}

So far, you've learned that the selector part of this CSS rule can be the name of an HTML tag (a type selector) or the name of a CSS class (a class selector). A large collection of CSS selectors exists, however. Many of these selectors are rather obscure, but the more common ones are powerful tools indeed. The lessons in this chapter introduce five of these selectors, beginning with the ID selector.

Lesson 19.1: Using ID selectors

Web Page Genealogy: Parents, Descendants, and Siblings

Working with Contextual Selectors

Lesson 19.2: The Descendant Combinator

Lesson 19.3: The Child Combinator

Lesson 19.4: The Sibling Combinator

Taking Things Up a Notch by Combining Selectors

Lesson 19.5: The ::before and ::after Pseudo-Elements

Resetting CSS with the Universal Selector

Styles: What a Tangled Web Page They Weave

Lesson 19.6: Understanding Inheritance

Lesson 19.7: Learning About the Cascade

Lesson 19.8: Introducing Specificity

Summary