Introduction
CSS (Cascading Style Sheets) is a great new technology which is taking the web by storm. The strange thing is, very few people have actually heard of it. You have probably seen Stylesheets in action on many websites. Anywhere you see a text link change color when you move your mouse over it probably uses stylesheets.
Why Should I Use Them?
This is actually a very good question. Why should you learn a new web language just so that links on your site change color? Some people even say this is tacky. CSS actually has a lot more use than that. Image being able to change what an HTML tag does, to be able to update a whole site by changing one file. It is possible with CSS.
Cascading Style sheets let you do many things you could never do before. If you want to change the <a> tag to always display text in bold, red, underlined you could do it with Cascading Style sheets. If you later decided you wanted it green, font size 6 with no underline you could change it just as easily. You can even link to a stylesheet file from the pages on your site. When you update the stylesheet file you can see the updates in the rest of your site.
Stylesheets also allow you to do things you never could with HTML. You can set background colors for text, you can indent text using centimetre values and you can place items exactly where you want them on a page. Most of this can be done from a linked file and can be updated quickly.
Stylesheets can be included in your pages in three ways. A linked file on your server, at the top of the page which applies to everything on the page or an inline style which works like a normal HTML tag.
Part 2 - Inserting Stylesheets & Rollovers
Introduction
After the last part you should know that CSS is an extremely powerful web language which can create amazing websites.
How To Include Stylesheets
There are 3 ways to include stylesheets on your page. You can create a link to an external file in the head of your document. This is very useful if you would like to make sitewide updates by changing one file. The second way is to include the stylesheet information in the head of the document. This allows you to have a style applied to the whole page without being the same as the rest of the site. Finally you can have inline styles, which let you change the style of one piece of text.
At this point I should mention that linked stylesheets are not supported by Netscape apart from versions 6.x
To insert a linked stylesheet you need to put the following into the head of your document:
Code: Select all
<link rel="stylesheet" type="text/css" href="url/of/stylesheet.css">
To insert a stylesheet into the head of a page insert the following into the head of your document:
Code: Select all
<style type="text/css">
<!--
Stylesheet in here
-->
</style>
Rollover Links
One of the most popular uses of stylesheets is to create mouse rollover effects on links on your site. They are extremely easy to create and are very versatile.
The code you should insert should either go in a file (saved as a .css) for linked stylesheets or in the <style> section of your pages:
Code: Select all
A:link {text-decoration: underline; color: #0000FF}
A:visited {text-decoration: underline; color: #990099}
A:hover {text-decoration: none; color: #FF0000}
A:active {text-decoration: none; color: #FF0000}
Code: Select all
A:
link
visited
hover
active
This tells the browser what to do to each type of link (hover is when the mouse is over the link).
The information inside the {} tells the browser what to apply to each tag.
Code: Select all
text-decoration: underline;
text-decoration: none;
Code: Select all
color: #FF0000
Code: Select all
text-weight: bold;
When you are adding new elements you must always remember to have a ; after each one.
Part 3 - Inline Styles, Classes & Font Effects
Introduction
In the last part I showed you how to make hyperlink mouseovers and how to use linked and whole page stylesheets. In this part I will show you how to just apply your styles to one part of the text and how to format fonts using stylesheets.
Inline Styles
Inline styles are added into an HTML tag so that you can just change the content of that tag. Apart from that, they are used exactly the same as a normal stylesheet. To include one in your H1 tag for example:
Code: Select all
<H1 STYLE="stylesheet information">Text</H1>
Classes
Another way of making different styles for different parts of your text is to use classes. These allow you to, for instance, have three different types of <p> tag stylesheet changes on your site. To do this you give each one a class. This is basically a word which corresponds to a change. For example you could have two classes called 'maintext' and 'footnote' for your <p> tag with different formatting information.
In your stylesheet you would have:
Code: Select all
P.maintext (stylesheet information}
P.footnote {stylesheet information}
Code: Select all
<p class="maintext">All the information</p>
<p class="footnote>All the information</p>
Other Font Formatting Options
Here are some of the other things you can do to your font's using stylesheets and the code they require. All of these should be placed inside the { } of your stylesheet or in inline styles. If you use more than one you should separate each with a semicolon:
Code: Select all
font-family: fontname, "font name", font;
Code: Select all
font-size: 16pt;
px - pixels
in - inches
cm - centimeters
mm - millimeters
Code: Select all
font-style: italic;
Code: Select all
font-weight: bold;
Code: Select all
text-decoration: underline;
Part 4 - Other Stylesheet Effects
Introduction
In this, the final part of the Stylesheets Tutorial, I will show you some of the other useful effects you can do with Stylesheets. Many of these are very good for making nice looking websites. Note that on this page all blue text should be placed within the { and } in your stylesheet on in an inline style.
Indenting and Aligning Text
One clever effect you can achieve with stylesheets is to indent text
Code: Select all
text-indent: 1cm
You can also use stylesheets to align text:
Code: Select all
text-align: left
Positioning Objects
Here is what you have always been looking for on when making a web site. You can exactly position anything on a web page, just like in a Desktop Publisher! To do this you should use:
Code: Select all
position: absolute; left: 100px; top: 100px
This is an extremely useful tag as it can be used to place objects with precision.
Colors and Backgrounds
The first, and most simple, color option you can do with stylesheets is to set the text's color using:
Code: Select all
color: #FF0066
Code: Select all
color: RGB(53,36,102)
Code: Select all
background-color: #FF0066
Code: Select all
background-image: url(http://www.yourwebsite.com/file.gif)
Conclusion
As you can see from this tutorial, stylesheets are one of the most useful technologies available on the web. Using stylesheets you can make your site easy to update and can add new effects to make it stand out from the crowd.