O'Reilly logo

HTML, XHTML, and CSS Bible, Fifth Edition by Steven M. Schafer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix D. CSS 2.1 Selectors Quick Reference

IN THIS APPENDIX

  • Basic Element Selectors

  • Descendant Selectors

  • Child Selectors

  • Adjacent Sibling Selectors

  • Class Selectors

  • ID Selectors

  • Attribute Selectors

CSS selectors are specific patterns used to match elements that will have the corresponding properties applied to them. CSS has many different patterns to match many different aspects of elements—their name/type, class, ID, place in the document hierarchy, and more.

In addition to using a single pattern to match elements, you can also combine patterns to create more specific matches. For example, the following selector matches all h1 elements:

h1 { properties }

If you need more specificity, you can add a class selector as in the following example, which matches all h2 elements with a class of section :

h2.section  { properties }

You can take the selector even one step further by adding a descendant selector, as in the following example, which matches all h2 elements with a class of section that are also descendants of h1 elements:

h1 h2.section  { properties }

The following sections provide a quick reference into the various CSS selector patterns.

Basic Element Selectors

The basic element selectors are used to match specific elements by name (e.g., p, h1, and so on).

Syntax:

E { properties }

Matches all E elements.

Syntax:

* { properties }

Matches all elements.

Note

The universal selector (*) guarantees only a universal (all element) match if it is the sole criteria in the selector. If additional conditions are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required