Cascading Style Sheets
|
Let's forget the 'cascading' part for the minute and start with the easier bit. 'Style' is a word that means many things to
various people. It can make a difference to the
clothes you wear or the type of car you drive. If you have used 'styles' in Microsoft Word, Quark XPress or some other program, you will appreciate the fact that you can put together a collection of type specifications - font, size, colour, leading, or whatever - and give that particular 'style' a name. A 'style' is a collection of attributes. So, you might have one style called 'body copy', another called 'captions' and others for different levels of headlines, sub heads and so on. HTML provides the 'structure' of the page and should be devoid of style. This is the basic fruitcake! It has a top, bottom and edge and some contents, but that's about it. Earlier versions of HTML have had both structural and style elements jumbled up with one another, but we are trying hard to get away from that now. The basic ingredients of a Web page are the words, numbers and pictures – the content. This 'document', as it's called, also has a top, bottom and edges and is usually read from the top down and from left to right – in Western languages anyway. It will probably have parts that are more important than others and this is where the first confusion between structure and style creeps-in. Is a 'heading' part of the structure or part of the style? The 'function' of a heading is definitely
structural - to present information more prominently
than the general body copy. In typography, there are
many ways to create a hierarchical order of
information, the most banal being 'big and bold'.
HTML does not dictate 'how' the prominence is
achieved, it merely provides a set of heading tags
from <h1> down to <h6> - but to add to the
confusion, are graded by size and boldness by
default - they don't have to be. <h1> can be the
same size and weight as your body copy if you like,
but displayed in red to make it stand out but then,
what if the page is being displayed on a black and
white screen? Suffice it to say, that some method of
creating prominence is required. The way you achieve
it depends on the circumstances. |
|
Structural Elements
|
Body, table, headings, lists, forms, preformatted, citation, strong, emphasis are all structural elements that describe 'where' and 'how' the information is presented.
|
|
Style Elements
|
Font face, size, color, bold, italic, underline, and so on, describe what it looks like.
|
|
So, the principle is that you keep the structure (HTML) and the style (CSS) completely separate so that they can be mixed 'n' matched. That is why the <font> tag, and its entourage, really has to go.
|
||
Family Affairs
|
If your parents both had red hair and green eyes, then it is very likely that you and your brothers and sisters have them too. There is also a pretty good chance that some of your children will have the same family characteristics. They will have inherited them.
Inheritance also plays a major part in the way that styles work on a Web page. Think of the <body> of the page as being the parent. Okay, it's a one parent family but that's not unusual these days. Any other items, the siblings, placed within the <body> should adopt the same characteristics as the parent by default. I say 'should' because that's the way it is supposed to work. In practice, some browsers just don't play the game - and there's your first 'gotcha'. Current browsers are inconsistent in the ways they handle the same style specification. The newest browsers are better, Netscape and Explorer 6, Opera etc. Netscape 4.x is pretty bad and ones earlier than that you can just forget about - luckily, they are now very rare. |
|
Now, I'm going to show you how, with minimal effort, you can totally abandon the <font> tag and embrace Cascading Style Sheets in all their glory.
First, the cake mix... |
||
<html>
<head> The stuff that doesn't show-up on the page </head> <body> The content that displays </body> </html> |
||
|
Shows a basic HTML page that is all structure and no style. It has a <h1> at the top, a <h3> below that, some body text, a numbered list and a table. Whatever you see here is down to your default browser settings entirely.
|
|
|
Here, I've just added a 'body' style into the <head> ... </head> section of the page. This is a fairly ordinary style, but serves its purpose. It goes like this: -
|
|
1
|
<style type="text/css" media="screen"><!--
|
|
2
|
body
|
|
3
|
{
|
|
4
|
font-size: 0.8em;
|
|
5
|
line-height: 1.4em;
|
|
6
|
font-family: Georgia, "Times New Roman", Times;
|
|
7
|
background-color: #fffff1;
|
|
8
|
margin-top: 20px;
|
|
9
|
margin-right: 50px;
|
|
10
|
margin-left: 50px
|
|
11
|
}
|
|
12
|
--></style>
|
|
Anatomy of a style declaration
|
I've split this all out onto separate lines for clarity but normally they would just join up into one log line.
The first line 1 <style type="text/css" media="screen"><!-- tells the browser that there is a style that it must use. Its 'type' attribute is 'text/css' because we are asking it to apply the style to all the text in the Web page. property: value ; If there is more than one, as I have here, they are separated by semi-colons but the final item doesn't need one. |
|
|
Uses exactly the same HTML as the two previous examples but the style properties change slightly to give a completely different look and feel to the page. This time I have used pixel measurements for the font-size and line-height, which means they are 'fixed' and will not change in different browsers. If you need them to change, to work at the maximum number of screen sizes and resolutions, stick to ems.
<style type="text/css" media="screen"><!-- body { color: #ccf; font-size: 12px; line-height: 24px; font-family: Verdana, Geneva, Arial, Helvetica; background-color: #609; margin-top: 40px; margin-right: 50px; margin-left: 200px } --></style>
So far, I have only giving a general specification for the <body> of the page. That should cover everything on the page because of the 'inheritance' involved. Often, you will want to change these defaults, to make headings a different colour, weight or size, for instance. As you start to become more specific, the new specifications you make over-ride the default ones. |
|
|
Now, I have reverted to the more usual 'all in one line' layout for compactness.
h1 { color: white; font-weight: bold; font-size: 24px } h3 { color: #f6f; font-weight: normal; font-size: 18px } ol { color: #6f3 } tr, td { color: #ff6; font-size: 12px; line-height: 24px; font-family: Verdana, Geneva, Arial, Helvetica } a:link { color: white; text-decoration: none; background-color: transparent } a:visited { color: #ccf; text-decoration: none; background-color: transparent } a:hover { color: #6f3; text-decoration: none; background-color: transparent } ol, is the 'ordered list' and can have its own specifications. tr and td affect the table tags. This is one you really have to watch because some browsers don't inherit the body settings inside tables. There is very little consistency in this particular respect between browsers at the minute. The solution is this; define what you want to happen inside a table deliberately, don't assume that the body style will be inherited, as you would expect. a:link, a:visited and a:hover set the colours for links interaction. text-decoration: none get rid of the underline on links but should only be used in situations where there will be no confusion as to what is a link and what isn't. |
|
That covers the basics of Style Sheets. Play around with the values, try different combinations and see what happens. Next month, I will go into some of the finer details and warn about more of those 'gotchas'.
|
||
Send your suggestions and comments to web@com4tzone.dk |