19 Learning Advanced CSS Selectors

 

HTML elements enable web page designers to mark up a document's structure, but beyond trust and hope, you don't have any control over your text's appearance. CSS changes that. CSS puts the designer in the driver's seat. — Håkon Wium Lie

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; 
    ...
}

Lesson 19.1: Using ID Selectors

CSS

HTML

Best Practices: Classes Versus IDs

Web Page Genealogy: Parents, Descendants, and Siblings

Working with Contextual Selectors

Lesson 19.2: The Descendant Selector

CSS

HTML

Lesson 19.3: The Child Selector

CSS

HTML

Lesson 19.4: The Sibling Selector

CSS

HTML

Taking Things up a Notch by Combining Selectors

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

CSS

HTML

Resetting CSS with the Universal Selector

Styles: What a Tangled Web Page They Weave

Lesson 19.6: Understanding Inheritance

CSS

HTML

Lesson 19.7: Learning About the Cascade

HTML

Lesson 19.8: Introducing Specificity

HTML

Summary