Intro To Cascading Style Sheets
Volume Number: 14
Issue Number: 6
Column Tag: WebTech
Introduction to Cascading Style Sheets
by Paola Aliverti
Contributing Editor Tantek Çelik
HTML authoring has just gotten easier: style sheets land on
the Web
Cascading Style Sheets have been sporadically seen on the web for a while. It isn't until
recently, with the latest versions of Microsoft Internet Explorer and Netscape
Navigator, that they can be more widely appreciated. Style sheets on the Web work
pretty much like style sheets in a word processor, or in a desktop publishing
application. You can define a style called 'note' with blue text, enclosed in a border,
with 24pt type set. If you need to make visual changes at a later date, all you need to do
is change the defined style, without going through all the text in a page, or all the
pages. CSS allows you to establish a set of rules to apply throughout the document, or,
in our case, a site. Just like word-processing style sheets, Cascading Style Sheets on
the Web help in a more consistent and time-saving preparation of your documents, and
in styling and formatting of your documents.
The W3 Consortium has so far come up with the first list of specifications (CSS1), and
is working on the second series of specs. Both Netscape 4.0 and Internet Explorer 4.0
support most of the CSS1 specs. Authoring tools are currently on the market to help
you with CSS (Macromedia Dreamweaver, and Cascade, for example). You can find
up-to-date information about the current CSS standards on the Web Consortium web
site, at: http://www.w3.org/Style/CSS/.
HTML was designed for structure, not for style and formatting. That's why solutions
like frames, and tables came along. The problem is that both of those solutions are
neither straightforward nor consistent. Frames cause problems with bookmarks, and
tables bloat code and promote inflexible formats.
Tables and frames will not disappear from everyday use, but style sheets will make
your life much easier. If you are a web author, there will no longer be a need for
tables hacks, spacer GIFs, or text-based GIFs. Style sheets allow for easy maintenance
of your site, easier design, and improved control over the page layout. They help build
scalable sites, uniform sites with a consistent look among browsers and platforms. If
you are a web user, surfer, or cybernaut, you will like style sheets because the pages
are smaller, your downloads faster, and the layout 'spiffier'. You will also like the
browser scalability.
CSS allows you to re-define the look of HTML tags, add your own styling to them, or
create new Classes with new characteristics.
For example, you can decide to have non-underlined links:
link
Or to have a blue tags:
H1 blue
Or place a dotted border around a paragraph:
paragraph with dotted border
NB: If you are among those who have not closed their tags, it's time to start.
Browsers behave much more predictably when you close the tags properly!
The basics
How does style sheet code work? Let's start with some basic terminology. In a style
sheet code, there is a selector and a declaration.
A:link { text-decoration: none }
A:link is the selector, and
{ text-decoration: none } is the style declaration
The style declaration has a property (in this case 'text-declaration'), and a value
('none'). The selector can be a tag, or a newly-defined class. More specifics on
different selectors can be found in the CSS1 recommendation:
• http://www.w3.org/pub/WWW/TR/REC-CSS1.
Of course, even though the CSS1 properties are defined by the official spec, not all
CSS-savvy browsers support all CSS1 properties. For a list of what is supported by
Navigator and Explorer, you can look at:
• http://www.webreview.com/guides/style/
• http://www.pcwebopedia.com/CSS.htm
• http://webreference.internet.com/html/css.html
They are not always up-to-date, but they are the best I've found so far.
There are different ways to add style sheets to your HTML code:
• Embedding a Style Sheet
• Linking to External Style Sheet
• Importing an External Style Sheet
• Adding Styles Inline
The above-mentioned examples (except the A:link example) are all inline styles. All
you need to do is add the STYLE attribute to the tag you want to modify, and specify a
properties and values.
If you are looking at CSS not only for styling, but for efficient site management, you
don't want to be repeating the style for each instance of each tag. Let's take a look at
other ways to implement style sheets...
Embedded Style Sheets
You can add your CSS code at the beginning of each HTML file you want to affect, by
putting the following inside your HEAD (in your HTML silly!!!)
embedded.html
Welcome to the fruit factory!
Welcome
You will find the latest and greatest news on
HREF="topo.html">this page!
Remember to hide the content of your style sheets from the old browsers with the
HTML comment tag, as older browsers will ignore the tags, but
not the text in between.
Notice that different property/value pairs for the same selector should be separated
by a semicolon ";".
External Style Sheet
This is my favorite implementation of CSS, as it allows you to style several files in
your site, and update them by changing only one file. You can create separate external
files for the different sections of your web site. A file called 'eggplant.css' can define
the look and feel of the pages in all the sections of 'eggplant.com' site.
eggplant.css
H1 {color: blue}
A:link {text-decoration: none;
color: green;
font-size: larger;
font-weight: bold}
P {border: dotted blue;
text-align: center}
In the 'eggplant.com' site's pages, I will have the following code in the to link to
the 'eggplant.css' style sheet:
external.html
Welcome to the fruit factory!
Welcome
You will find the latest and greatest news on
HREF="news.html">this page!
Importing an External Style Sheet
Let's say that Eggplant is partnering with TopoCo. in a sale of Topo Mouse dolls, and I
need to add TopoCo.'s traditional style to my Eggplant site. I can do this by adding to my
previously-mentioned 'eggplant.css' file one line of code: @import url(topo.css)
import.css
H1 {color: blue}
A:link {text-decoration: none}
P {border: dotted blue}
@import url(topo.css)
You can import as many style sheets as you like (but beware of possible conflicts, as
the next sections explain...). There are already collections of style sheets available on
the web http://www.microsoft.com/gallery/files/styles, and expect more in the
future. Importing style sheets can come pretty handy when you don't feel like creating
the files yourself. Of course, you should be aware not only of the legal implications of
taking someone else's work, and use it in your own site, but also of the stability of the
site you are linking to.
As far as I can tell, though, it's something you don't have to worry about, yet, because
only one version of the common browsers supports imported style sheets (Internet
Explorer 4.0 for the Mac).
Order of importance
As you probably already guessed from looking at the examples above, you can mix and
match any of the methods of implementation of CSS. Which one is more important? And
which one will prevail, in case two of them conflict? Although many references
attempt to explain (see comments about CSS1, and CSS2 specs in the next section), the
correct order of importance is the following:
1. Inline Styles.
2. Embedded, Linked, and Imported Style Sheets.
3. User preferences.
4. Browser's default settings.
Now let's move on to the next section to find out what prevails when styles conflict.
Cascading Abilities, Inheritance, Conflicts
As you probably already guessed from looking at the examples above, browsers need
firm rules of cascading and inheritance for different style sheets, or implementations
thereof, in order to avoid chaos. The specs of CSS1 differ a bit from CSS2 in this
matter. According to my research, browsers have already started to support CSS2
rules. I will skip the CSS1 rules, and just discuss the CSS2 cascade rules, which,
apart from being the latest, also seem more logical. Here is the first set of rules:
1. Follow specific declarations, or, if there aren't any, inherit values.
2. Sort declarations by importance.
3. Sort by specificity of selector.
4. Finally, among selectors of equal specificity, the latest onewins.
As most of the current versions of CSS browsers do not support factor 2 (the
'important' modifier), I will concentrate on the cascading declarations and inheritance.
The first rule can be divided in two simple rules:
1. Cascading takes precedence over inheritance.
2. Follow the order in which the styles appear, considering the latest
(closest to the text to which it refers) style the most important.
Let's take a look at what cascading and inheritance mean.
Cascading declarations and inheritance
inherit.html