XML technologies / CSS / CSS Cookbook / General topics / Different selectors for assigning styles
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
You want to use selectors to give particular parts of a website their own styles.
Solution
Special selectors let you apply styles only to specific parts of a page (see the following figure):
<html>
<head>
<title>CSS Kochbuch</title>
<style type="text/css">
<!--
* {
font-family: verdana, arial, sans-serif;
}
h1 {
font-size: 120%;
}
#navigation {
border: 1px solid black;
padding: 40px;
}
li a {
text-decoration: none;
}
p {
font-size: 90%;
}
-->
</style>
</head>
<body>
<h1>Seitenüberschrift</h1>
<p>Dies ist ein Beispielabsatz mit einem <a href="http://csscookbook.com">Link</a>. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna <em class="warning">aliquam erat volutpat</em>. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
<ul id="navigation">
<li><a href="http://csscookbook.com">Äpfel</a></li>
<li><a href="http://csscookbook.com">Bananen</a></li>
<li><a href="http://csscookbook.com">Kirschen</a></li>
</ul>
</body>
</html>Figure: A web page with CSS styles.
Discussion
CSS offers many, sometimes sophisticated, ways to specify which page elements receive styles.
To understand how parts of a page are selected, recognize that HTML markup creates a structure. Although HTML elements may initially seem unordered (see the following figure), they have a defined order.
Figure: The HTML elements used in the solution.
This structure usually remains hidden from visitors, but is essential to the browser’s rendering process.
When a browser requests a page and begins rendering, it organizes the elements into a particular structure. Although this organization concerns programming, a visual representation would closely resemble an organizational chart used by companies.
The next figure shows the HTML represented as a diagram.
Figure: The page’s HTML elements in a hierarchy.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln