XML technologies / CSS / CSS Cookbook / General information / Adding styles to a web page
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
Learn the ways to add styles to a page.
Solution
Three methods are available: internal, external and inline. An internal stylesheet is defined in the document’s head section:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>CSS Kochbuch</title>
...
<style>
<!--
#header {
width: 100%;
height: 100px;
font-size: 150%
}
#content {
font-family: verdana, arial, sans-serif;
margin-left: 20px;
margin-right: 20px
}
.title {
font-size: 120%
}
-->
</style>
</head>
<body>
...Note:
Note the HTML comments after the opening and before the closing tag style. They keep CSS from accidentally appearing in the layout or being rendered unexpectedly.
External stylesheets live in separate files linked from HTML. This code is saved as screen.css:
/* CSS-Dokument */
h1 {
font-size: 150%;
}
h2 {
font-size: 120%;
}
p {
font-family: Verdana, Arial, Helvetica, sans-serif;
}Warning: The HTML element style is invalid in external CSS, as are HTML comments.
To attach the file, write this inside the tags head:
<link href="screen.css" rel="stylesheet" type="text/css" media="screen" />Alternatively, use the statement @import inside the tags style:
<style>
<!-- @import url(screen.css) screen; -->
</style>Note: Older browsers may handle the statement @import poorly.
Inline styles work like the deprecated tag font: definitions are embedded in markup with the attribute style:
<h1 style="font-family: verdana, arial, sans-serif; font-size: 150%; color: blue;">Seitenüberschrift</h1>
<p style="font-family: sans-serif; font-size: 90%;">Hallo Welt!</p>Discussion
The three stylesheet types are:
- External
The tag
linkattaches CSS-only files. Change a font color once in that file to update all pages using it.- Internal
A standalone page may benefit from embedded styles not shared elsewhere. Define them in the document’s
headsection within tagsstyle.- Inline
Inline styles resemble the tag
font, styling a specific tag. Designers rarely use them.
External and internal stylesheets save considerable maintenance time over inline styles.
Imagine a site where tags font control blue text and its size. To turn it black, find each occurrence of <p> and edit manually:
<p><font size="2" color="blue">Hier steht der Text</font></p>External CSS needs only two steps: open the file and change the color.
p {
color: blue;
}Internal CSS takes one step because styles share the HTML document. Find the definition near the top and replace blue with black:
<style>
<!--
p {
color: blue
}
-->
</style>When inline styles make sense
Changing inline colors is as laborious as changing tags font:
<p style="font-color: blue">Hier steht der Text.</p>Why use them? Unique content may need unique styles, avoiding cluttering a shared sheet with one-off rules. If styles may change or be reused, prefer attributes id with internal or external sheets.
When internal stylesheets make sense
Most sites prefer external sheets. During initial layout development, however, embedded styles may be useful. Move them outside when the design is complete or unwieldy, then continue editing there. A page unrelated to the rest or with a special layout may also suit internal CSS, keeping the shared file uncluttered.
See also
The stylesheet section in the HTML 4.01 specification.
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