HTML Cookbook Basic HTML Course

It's Easy!

CSS Step By Step


Alternate version

External (Linked) Stylesheets

We have been using an internal (embedded) stylesheet on our pages. Now we'll set up an external (linked) stylesheet.

An external stylesheet is a text file that only has style declarations in it and has a .css extension. The stylesheet is applied to your page in the head section with the link tag.


Link Tag

We discussed the link tag earlier in the course. It allows you to load information into a page.

Here is the link tag form for CSS:
<link type="text/css" rel="stylesheet" href="URL" />


The link tag loads the external stylesheet into a browser.

The attributes in the link tag can be listed in any order.

type tells the world that the link is to a text file that has css in it.

rel stands for the relationship that the file has to your page. In this case, it's a stylesheet.

href="URL" gives the name and location of the file (your external stylesheet), such as style.css.

Normally you use a relative URL for our stylesheet, but you can link to any stylesheet on the web. Holy Cow!

External stylesheets contain only declarations and comments. Give your file a name and add the .css extension. I call my stylesheets style1.css (black background) and style2.css (white background).

Let's compare the 2 types of stylesheets.

Internal stylesheet
<style type="text/css">
<!--

body { background-color: #000000: color: white;}

a:link { color: #0000ff; }
a:visited { color: #800000; }
a:active { color: #ff00ff; }

-->
</style>


External stylesheet: style.css
body { background-color: #000000: color: white;}

a:link { color: #0000ff; }
a:visited { color: #800000; }
a:active { color: #ff00ff; }



We see that the declarations in both stylesheets are the same. The difference is that the internal stylesheet needs the <style>...</style> part to identify it as a stylesheet while the external sytlesheet is identified as a stylesheet in the link tag.


Why use an external stylesheet?

An internal stylesheet can only be used on the page that contains it. An external stylesheet can be applied to any page. That means:


Assignment

  1. Make an external stylesheet. (You may make a copy of your sampler page, then copy the declarations from your internal stylesheet and paste them into a new text file.) This file should have a name of your choosing and the extension .css.
  2. Add your new external stylesheet to your page with the link tag, then delete your internal stylesheet.
  3. Post your new page proudly - you are ze mighty cree-ay-tor!

Side Note for SSI Users

The concept behind external stylesheets is the same as the basic concept for SSI and can be thought of in the same way. A change to your file affects all pages that contain the file.

Our Mantra:
I love CSS!
CSS is my friend!
I AM the mighty creator!

Happy coding!
Gnubee

Links
Tips
Tools
References
Rules
HTML Help Gnubee - Newsgroup
HTML Course Contents (Home)

Valid XHTML 1.0!

Valid CSS!