XML technologies / CSS / Advanced websites with CSS / A first overview / Types of CSS rules / Where can styles in CSS be defined?

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

CSS can be defined in three places:

Inline declarations

An entire style can sit inside an HTML tag, hence “inline declaration.” Almost any element can receive a style attribute. This example makes a second-level heading red and uppercase:

<h2 style="color: red; text-transform: uppercase;">Eine ungew&ouml;hnliche &Uuml;berschrift</h2>

This book treats that as an exception. Separating content from presentation is a key CSS advantage, lost when styles sit inside tags. Use inline styles mainly for testing before moving them into a head stylesheet or external file.

Embedded CSS

Beginners and people testing CSS often define it in the HTML head. It is easy, though not my preferred approach; this book occasionally uses it.

Create a style-block inside the head section, highlighted below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>CSS Style Sheet-Beispiel</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <style type="text/css">
  <!--
  h1, h2 {
    color: green;
  }
  h3 {
    color: blue;
  }
  -->
  </style>
</head>
...

Rules inside the style block apply to matching parts of that document. Here the first rule makes first- and second-level headings (h1, h2) green. The second makes third-level headings (h3) blue.

Every rule starts on a new line, with each property indented on its own line inside braces. This has no technical effect but greatly improves readability, especially for JavaScript programmers.

External CSS

Finally, CSS can live in files independent of HTML. Reference them by adding a tag <link> to the head section.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>CSS-Stylesheet-Beispiel</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" type="text/css" href="corpstyle.css" />
</head>
...

Here, the file corpstyle.css contains styles referenced by the page. Its content might be:

h1, h2 {
  color: green;
}
h3 {
  color: blue;
}

I prefer this approach for several reasons:

It provides the strongest separation. One stylesheet can serve any number of pages, each linking to it, simplifying maintenance. Changing one external rule updates a shared element everywhere. Embedded or inline styles instead require editing each page’s HTML.

Browsers also cache CSS as separate files. A new page using the same sheet need not download it again, improving loading and rendering speed.

External stylesheets are also more professional, showing that you understand and use content-design separation. Discuss, analyze and test them with colleagues as a serious design component rather than an afterthought.

  

 

 

 

back to top