16.5. Adding Styles to a DocumentStyle rules can be applied to documents in three ways: as inline style directions, as style elements embedded at the top of the document itself, and as external files that can be either linked to or imported into the document.
16.5.1. Inline StylesYou can add style information to an individual element by using the style attribute within the HTML tag for that element. The value of the style attribute is one or more standard style declarations, as shown here: <h1 style="color: red">This Heading will be Red</h1> <p style="font-size: 12px; font-family: 'Trebuchet MS', sans-serif"> This is the content of the paragraph to be set with the described styles.</p> Note that if the style attribute uses double quotation marks as shown, quoted values within the list (such as the font name "Trebuchet MS" in the example) must use single quotation marks. The reverse is also valid: if the document uses single quotes for attributes, then contained quoted values require double quotes. Although a perfectly valid use of style information, inline styles are equivalent to the font extension to HTML in that they pollute the document with presentation information. With inline styles, presentation information is still tied to individual content elements, so any changes must be made in each individual tag in every file. Inline styles are best used only occasionally to override higher-level rules. In fact, the style attribute has been deprecated in XHTML 1.1 and does not appear in other XML languages. 16.5.2. Embedded Style SheetsA more compact method for adding style sheets is to embed a style block in the top of the HTML document using the style element summarized here.
<style> ... </style> Attributes
The following example shows these sample rules embedded in an XHTML document: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <style type="text/css"> h1 {color: #666;} p {font-size: 90%; font-family: Verdana, sans-serif; } </style> <title>Style Sheets</title> </head> ... </html> The style element must be placed within the head tags in the document. Currently, Cascading Style Sheets is the only widely supported style sheet language, but the W3C has prepared for the possibility of additional languages to be added in the future by providing the type attribute within the style element. The only viable style type as of this writing is text/css. The type attribute is required in both HTML and XHTML; if it is omitted, some browsers may ignore the entire style sheet. In addition, the media attribute in the style element (not shown in the example) may be used to target the medium (screen, print, handheld, etc.) to which the style sheet should be applied. If it is not present, the default is "all" media. The media attribute is discussed in the "CSS for Other Media" section.
16.5.3. External Style SheetsThe most powerful way to use CSS is to collect all the style rules in a separate text document and create links to that document from all the pages in a site. In this way, you can make stylistic changes consistently across a whole site by editing the style information in a single document. This is a powerful tool for large-scale sites (and small ones, too, for that matter). 16.5.3.1. Style sheet contentThe style sheet document is a plain-text document that contains at least one style sheet rule. It may not contain HTML tags (after all, it isn't an HTML document) and so including HTML tags may cause parts of the style sheet to be ignored. HTML comments are also not permitted, however, comments may be inserted in the style sheet by using the CSS comment syntax shown here: /* This is the end of the chapter */ There are two ways to refer to external style sheets (which should be named with the .css suffix) from within a document: the link element and the @import directive. 16.5.3.2. Using linkThe best-supported method for referring to external style sheets is to create a link to the CSS document using the link element in the head of the document, as shown in this example: <head> <link rel="stylesheet" href="/pathname/stylesheet.css" type="text/css" /> </head> The rel attribute defines the linked document's relation to the current documenta "style sheet." The href attribute provides the URL of the style sheet document. Authors may link to more than one style sheet in a document and both will apply. 16.5.3.3. ImportingAn alternative to linking is to import an external style sheet into a document using the @import function in the style element: <style type="text/css"> <!-- @import url(http://pathname/stylesheet.css); p {font-face: Verdana;} --> </style> In this example, an absolute URL is provided, but a relative URL may also be used. @import commands must come before anything else (except @charset).
Importing allows multiple style sheets to be applied to the same document. When additional @import functions are added within the style element, the style information from the last file read (the one at the bottom of the list) takes precedence over the previous ones. The @import directive may also be used in the style sheet itself to reference information in other external .css files. See the sidebar "Modular Style Sheets" for more information. @import is not supported by Netscape 4, Internet Explorer 3, and Opera 3. This limitation is often used as part of a technique for hiding unsupported style information from these browsers. Fortunately, they make up a small fraction of browsers in use as of this writing. 16.5.4. CSS for Other MediaCSS 2 introduced the ability to target style sheets to specific presentation media. This is done using the media attribute in the link element or @media or @import rules in a style sheet. The complete list of accepted values for the media attribute follows, but currently, only screen, print, and all are widely supported. Support for handheld is getting a lot of attention by the W3C's Mobile Web Initiative. Multiple values may be provided in a comma-separated list.
|