XML technologies / CSS / Advanced websites with CSS / Putting CSS in perspective / What is CSS for? / Colors and CSS
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
Stylesheets let you control the color of every HTML element that can be displayed in color. The most important are:
Text
Headings (a special form of text)
Backgrounds
Background colors of text and headings
Considering when, how and especially in which combinations CSS can define these elements’ colors opens up a whole range of new possibilities.
Change the text color and add a colored background, and a previously ordinary page (see the figure Black text on a white background) makes a completely different impression. The figure Yellow text on a black background shows the same page with yellow text on a black background. The figure The black-on-yellow version shows the reverse: black text on yellow. Whether the colored version is as readable as the black-and-white original in the figure Black text on a white background is debatable. Both versions certainly stand out more.
Figure: Black text on a white background.
Figure: Yellow text on a black background.
Here is the style rule producing the effect in the figure Yellow text on a black background. As you can see, it is simple but produces a striking result:
body {
color: yellow;
background-color: black;
}Specifying a value by color name is just one of several ways to define colors in CSS. (For details, see Adding some color.)
Here is the style rule producing the effect in the figure The black-on-yellow version. Do not be surprised: it is exactly the reverse of the rule for the figure Yellow text on a black background:
body {
color: black;
background-color: yellow;
}Figure: The black-on-yellow version.
You may find a strongly contrasting background overwhelming. The figure The page with yellow-on-black headings offers another variation on text and color. Only the headings use yellow text on black. The rest of the page is unchanged from the first example in the figure Black text on a white background.
The following rule produces the heading in the figure The page with yellow-on-black headings:
h1, h2, h3, h4, h5, h6 {
color: yellow;
background-color: black;
}As the code shows, we use no unusual stylesheet tricks, such as putting headings in a tag <div> or drawing a box around them. In the browser, the heading is a block-level element, which spans the browser’s full width without further instructions. Assigning a heading a value for the property background-color also applies it to the entire horizontal area surrounding the heading.
Figure: The page with yellow-on-black headings.
CSS offers color-conscious designers many more benefits, described in Adding some color. Here we simply introduce various issues you will encounter when using CSS properly.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” 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.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg
back to top