Introduction to CSS2
A D V E R T I S E M E N T
Introduction
This is one of very few CSS2 tutorials on the net. Not only does it have that
advantage, but also has the following advantages:
- It is easy to understand
- It covers every aspect of the specification, including those that are
frequently not touched upon by other tutorials such as cascading, box width
calculations, etc.
- It presents information in an efficient manner - it is short, so you
learn faster
What are style sheets
Style sheets provide a means for authors to specify how they wish documents
written in a markup language such as XML or HTML to be formatted. For example,
an author might wish to specify that a document should be green on pink - this
could be done using CSS, an established standard for styling documents.
You might ask 'Why do we need style sheets - can't you use HTML; for example,
the FONT element or the bgcolor attribute?'
There are two answers to this:
- HTML isn't designed for styling documents - when you write an HTML
document, you are specifying only the content that the element contains.
Given the piece of HTML: <H1>A heading</H1>, you have specified nothing
about the way the element should be rendered, only that the element is a
level 1 heading. If you wished to state that you want your headings to be
yellow Helvetica, then you could have <H1><FONT color="yellow"
face="Helvetica">A heading</FONT></H1>, but this is bad for several reasons:
- You have to add that piece of code to each heading that you want
styled - this is time-consuming, prone to error and makes files
excessively bloated (a typical page styled using HTML will be 1/3rd
formatting tags).
- If you want to change those headings to pink Arial, then you will
have to change each heading individually - a prohibitively laborious
task on a large site.
- HTML simply doesn't offer sufficient control over document formatting -
important formatting effects such as leading (the space between lines), text
shadows, and many other effects just can't be done using HTML.
Style sheets solve all of these problems. For example, say you want to make
all those headings green Arial - no problem, just type H1 {font-family: Arial;
color: green}. What if you want to change all of the headings on your site to
red with a blue background? Just change that to H1 {color: red; background:
blue}, and, at a stroke, the whole of the site is changed.
What can style sheets do? To take an example, on this page all of the headings are reddish brown - this
is so on every page on RichInStyle.com. This probably adds up to about 10000
headings.
If each of those headings were to be styled with a font element (e.g.,
<H2><font color="blue">A heading</font></h1>), that would take up 250kb - a
significant amount of space. By replacing this with a CSS rule, the whole site
can be styled with a single rule amounting to only about 40 bytes.
Assuming an average of 50,000 downloads per page, per month, that works out
at a saving of 12.5gb.
That is a lot of saved space, especially if you pay for bandwidth by
the gigabyte.
Just imagine the cumulative effect of that across all the margins, colors and
fonts on the site, and that is a lot of saved traffic, so not only are cost
reduced, but also the download speed will increase for viewers of your pages.
Why style sheets?
Style sheets have the following advantages:
- They separate content from formatting. This means that instead of
marking a quotation as italic, you mark it as a quote and then tell the
browser that you want all quotes to be italic. This means that it is a
two-second job to change quotes to bold, red, green or normal.
- They reduce download time by removing formatting information from
documents. Thus instead of having to specify that you want Times New Roman a
few dozen times in a file for headings, you specify once that you want
headings to be Times New Roman. They also are advantageous in that they need
only be downloaded once for an entire website.
- They give far more control over formatting than HTML - such features as
background images and colors on all elements - not just the whole document,
etc.
- They ensure a consistent appearance across a site
What reasons are there not to use style sheets?
- About 1 in 20 users have browsers that do not support style sheets.
- Not all browsers support style sheets properly.
Support
Be aware that since CSS is a large specification, most browsers do not
support it in its entirety. Given that this is so, don't be surprised if some
concepts described in this guide don't work.
Using style sheets
Style sheets allow you to say, for example, that you want headings in 30px
Arial with a pink background, that you want the whole document to have a left
margin of 1in, or whatever. The good thing about them is that they are the only
way that you can say that you want BODY or P to be displayed in a certain way -
in HTML you could make all P tags a certain color using HTML tags, but if you
wanted to change them it would take hours on a big website, but seconds using
CSS.
For example:
BODY {color: red;
background-color: white;
font-size: 16px;
font-family: Arial;
line-height: 20px;
margin-left: 5%}
As you can see, creating a style sheet is easy, but before you can do that,
it is necessary to decide how you are going to attach your style to your page.
|