Using Document Structure

As I’ve mentioned before, CSS is powerful because it uses the structure of HTML documents to determine appropriate styles and how to apply them. That’s only part of the story since it implies that such determinations are the only way CSS uses document structure. Structure plays a much larger role in the way styles are applied to a document. Let’s take a moment to discuss structure before moving on to more powerful forms of selection.

Understanding the Parent-Child Relationship

In order to understand the relationship between selectors and documents, you need to once again examine how documents are structured. Consider this very simple HTML document:

 <base href="http://www.meerkat.web/">
 <title>Meerkat Central</title>
 <h1>Meerkat <em>Central</em></h1>
 Welcome to Meerkat <em>Central</em>, the <strong>best meerkat web site 
 on <a href="inet.html">the <em>entire</em> Internet</a></strong>!</p>
  <li>We offer:
    <li><strong>Detailed information</strong> on how to adopt a meerkat</li>
    <li>Tips for living with a meerkat</li>
    <li><em>Fun</em> things to do with a meerkat, including:
      <li>Playing fetch</li>
      <li>Digging for food</li>
      <li>Hide and seek</li>
  <li>...and so much more!</li>
 Questions? <a href="mailto:suricate@meerkat.web">Contact us!</a>

Much of the power of CSS is based on the parent-child relationship of elements. HTML documents (actually, most structured documents ...

Get Cascading Style Sheets: The Definitive Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.