XML technologies / CSS / Advanced websites with CSS / Adding some color / Setting the body color

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Usually no property color is defined for the element body. Every modern browser defaults to black text on a white background. This is perfectly adequate for most basic layouts. However, some users may have changed their browser’s default background color. They will then see pages with that background color. Some designers set a bright color in their browser as a reminder not to forget the background color in their CSS files.

To define a text color for your entire page, you could simply create the following style rule:

body {
  color: red;
}

However, I advise against doing this, because the following basic principle applies when defining colors with CSS:

Whenever you specify a foreground color, you must also specify a background color! A stylesheet containing only one of the two is incomplete.

When using colors on a page, apply them not just to individual elements but, wherever possible, to all combined and adjacent elements as well. You cannot know whether a user has, for example, chosen a particular background color in their preferences against which your carefully selected text color looks poor. If the browser setting uses exactly the same color for the background, your page will appear empty.

Therefore, if you specify a foreground color with color, always combine it with a value for background-color.

body {
  color: white;
  background-color: maroon;
}

When defining a color for body or another element, always remember that this property also applies to child elements through inheritance, including headings, paragraphs and lists, unless you override the property for them with other style rules.

  

 

 

 

back to top