2.4. External Style SheetsAn external style sheet is nothing more than a text file containing all your CSS rules. It never contains any HTML codeso don't include the <style> tagand always ends with the extension .css. You can name the file whatever you like, but it pays to be descriptive. Use global.css , for example, to indicate a style sheet used by every page on the site, or use form.css to name a file containing styles used to make a Web form look good.
Tip: If you have a page with an internal style sheet, but want to use an external style sheet, then just cut all of the code between the <style> tags (without the tags themselves ). Then create a new text file, and paste the CSS into the file. Save the file with a .css extension global.css , for exampleand link it to your page, using one of the techniques described next . Once you create an external style sheet, you must connect it to the Web page you wish to format. You can attach a style sheet to a Web page using HTML's <link> tag or CSS's own @import directivea command that basically does the same thing as the link tag. All current Web browsers treat these two techniques the same, and both let you attach style sheets to a Web page, so choosing one is mostly a matter of preference. (For the one exception, see the box below.) Note: The @import directive can do one thing the <link> tag can't: attach external style sheets to an external style sheet. This advanced technique is discussed in Section 14.2.3.1.
2.4.1. Linking a Style Sheet Using HTMLOne method of adding an external style sheet to a Web page is to use the HTML <link> tag. You write the tag slightly differently depending on whether you're using HTML or XHTML. For example, here's HTML: <link rel="stylesheet" type="text/css" href="css/global.css"> Here's XHTML: <link rel="stylesheet" type="text/css" href="css/global.css" /> The only difference is how you end the tag. The link tag's an empty element, since it has only an opening tag and no matching, closing </link> tag. In XHTML, you need to add a closing slash (like this: />) to terminate the tag; HTML doesn't require the extra slash. Otherwise, the link tag's the same in HTML and XHTML and requires three attributes:
Tip: You can attach multiple style sheets to a Web page by adding multiple <link> tags, each pointing to a different style sheet file. This technique's a great way to organize your CSS styles, as you can see in Chapter 14 (Section 14.2.3.1). 2.4.2. Linking a Style Sheet Using CSSCSS includes a built-in way to add external style sheetsthe @import directive. You add the directive inside of an HTML <style> tag, like so: <style type="text/css"> @import url(css/global.css); </style> Unlike HTML's <link> tag, @import is part of the CSS language and has some definite un-HTML-like qualities:
Note: You'll learn how rules interact and how you can create a rule that overrides other rules in Section 5.3. You can even create an external CSS file that contains only @import directives linking to other external style sheets; a technique often used to help organize your styles (see Section 14.2.3.1). Here's an example: <style type="text/css"> @import url(css/global.css); @import url(css/forms.css); p { color:red; } </style> Technically, you should place all the @import lines before any CSS rules, as shown here, but it's okay if you forget. Web browsers are supposed to ignore any style sheets you import after a CSS rule, but all current Web browsers ignore that restriction. |