External Style Sheets
.css file. In fact, once you become adept at using style sheets, this will be —and should be— your go-to method of applying CSS.How to Create an External Style Sheet
An external style sheet is simply a text file containing a list of CSS rules sets. The file is saved with a
.css extension and saved to any directory that can be accessed by the web pages using it. Unlike embedded style sheets, the CSS rules sets do not have to be wrapped in the <style>...</style> tags.Here's an example using the same rule sets as those on the previous page in this tutorial:
Example 1 - CSS RULE SETS IN AN EXTERNAL STYLE SHEET
H1 {
font-family: 'Times New Roman';
font-size: 36px;
background: #ffffff;
color: maroon;
}
H2 {
font-family: arial,verdana,sans-serif;
font-size: 20px;
background: #ffffff;
color: black;
}
P {
font-family: arial,verdana,sans-serif;
font-size: 16px;
background: #ffffff;
color: navy;
}
Now all we have to do is save the above rule sets to a file called, say,
main.css and presto, you've got an external style sheet. So how do you get your web pages to use this style sheet? Well this is accomplished using...
The link Element
The
link element can be used, among other things, to specify that a web page should use an external style sheet. The link element only requires a start tag <link> and is inserted in between the <head>...</head> tags of your web page (a.k.a., document head). It can be used as many times as you like.The
link element employs three important attributes: rel, type and href. For CSS, the value of the rel attribute is always stylesheet and the value of the type attribute is always text/css. The only part of the code you really have to concerned with is the value of the href attribute which will change according to which .css file you're referring to. This value can be any relative or absolute path.Here's an example of the
link element inserted in the document head of a web page:Example 1 - THE link ELEMENT DISPLAYED IN THE DOCUMENT HEAD
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
...web page content...
</body>
</html>
In the above, the
link element specifies that the web page should use an external style sheet called main.css. Now... here's the magic of it:
Since you can have as many web pages as you like using the
link element to refer to the same external style sheet, i.e., main.css, then what you have essentially done is created a kind of central hub to apply formatting to many different web pages at once. Click here to view an example of a web page using an external style sheet...
Click here to view another web page using the same external style sheet...
Heh heh! Now we're cooking with gas, right?
Right. So now you're thinking that you would like to make some paragraphs with green text and other paragraphs with blue text (*chuckle*... or something like that) and can this be done using CSS?
Well most certainly!
Let's move on to learn how to create class selectors which will help you target specific web page components...
SEE ALSO:
| <~ BACK | TOP | NEXT ~> |
