runway index

The Virtual World

In-line Styles :: Embedded Styles :: I.D.tags :: Links :: DIV :: SPAN :: Further Reading

Chapter Index

Introduction
History
Getting Connected
Browsers
Copyright
Search Engines
HTML
Linking documents
Images
Newsgroups
Tables
Forms
Frames
Photoshop
Page and Site Design
Publishing
Javascript
Style Sheets

 


Introduction

We have style sheets in desk top publishing packages such as PageMaker and QuarkXPress. They have even made their way into the more sophisticated word processing packages. However, until recently we had no way of using them on the web.

A style sheet is a collection of definitions. You may want all your headings to be 20 point Helvetica, bold and coloured red. Make a style definition along those lines, and all you then have to do is apply that style definition to all the parts of your text that you want to display as headings. The point is, you apply the style in one go, rather than a selection of individual bits of formatting.

It gets better. Suppose you want to change your mind at a later date. All you have to do is change the style definition. Maybe you want all your headings to be slightly bigger, say 24 point, but not bold, and you want the red to be lighter. You don't have to go through all your text finding headings and changing their formatting, all you do is re-write your style definition and all the headings change.

You can define not only the font, style, and size, but also the margins of your text, and whether it is enclosed in a border, and whether there is a background colour.

There is, however, one snag (there usually is): all of this is only visible in version 4.x browsers. Internet Explorer version 3.0 recognises styles, but not in all their glory.

These styles are known as Cascading Style Sheets (CSS for short). Let's look at how we create them. There are, as usual, several ways of implementing CSS.

In-line Styles

We can create them in-line, i.e., as we go along we apply style attributes to our text. To do this we add the STYLE attribute to any HTML tag. An example is given below:

<H1 STYLE="color:red; font-family:helvetica; font-size:24pt">This is a main heading</H1>

The actual text is in black, the code in green. We have given the style a name, and have given that style several attributes. Each set of attributes is closed with a semi-colon. The name of each attribute and its characteristic is separated by a colon.

You can use style tags in conjunction with many HTML tags. You will probably use it mainly on the heading tags and the paragraph tag.

This usage of styles is the least powerful. The whole point of styles is that you can change them from a central location and their effect then becomes either document wide, or site wide.

Embedded Styles

Embedded styles are defined in the HEAD of your page and they therefore apply to the whole document. A typical construction would be as follows:

<HEAD>
<TITLE>
Stylesheets</TITLE>
<STYLE>
<!--
.classIndent {font-family:arial, helvetica, sans serif;
font-weight:italic; font-size: 14pt; color:#CC1E00; left:10%;}
-->
</STYLE>
</HEAD>

I have enclosed the style definitions within the STYLE tag.

Secondly, I have commented out those definitions so that browsers that are not style-aware will not pay any attention to the code (<!-- Š -->).

Next, I have given your style a name. Names always begin with a fullstop. They are not case sensitive. Here, we have given the style the name .classIndent. I always preface a style name with the word 'class' so I can locate what is going on in my code.

The definition is enclosed in braces, and includes a hexadecimal color (remember American spelling). Also included is a left indent. There are two ways of dealing with this, either a percentage of the space available, or a fixed width measured in pixels. If I had chosen a fixed width it would have been written in the following style: 24px.

If I now want to apply this style in the body of my page I add the style name as a tag. Here is an example:

<BODY>
<P CLASS="classIndent">
This is a short example of some indented text. It will take on the characteristics of the style as defined in the HEAD of this page.</P>

I.D. Tags

You can apply a tag to a defined item on the page. In this instance you must define the item's ID first. to define an ID you preface the name in the style definition with the # symbol, and then use the ID attribute in the tag.

The definition in the HEAD of your page would be similar to the embedded tag:

<HEAD>
<TITLE>
Stylesheets</TITLE>
<STYLE>
<!--
#IDIndent {font-family:times; font-weight:bold; font-size:12pt; color:red; left:25px;}
-->
</STYLE>
</HEAD>

You would apply this to the body of your page as follows:

<BODY>
<P ID="IDIndent">
This is a short example of some indented text. It will take on the characteristics of the style as defined in the HEAD of this page.</P>

Links

On a large web site where you want your style to be applied across several pages, or maybe even all of your pages, you would not place the style definitions in the head of one document because they would be hidden from all your other documents. To make them site-wide you need to have all your definitions in a special document.

First, create a text document which contains the style definitions. They can be in the form shown above, except that you do not need to put in the STYLE tags, or to comment out the definitions. merely put in the guts of the definition.

.classIndent {font-family:arial, helvetica, sans serif; font-weight:italic; font-size:14pt; color:#CC1E00; position:absolute; left:250; top:20; height:150; width:300; z-index:-1; background-color:yellow; overflow:scroll;}

#IDIndent {font-family:times; font-weight:bold; font-size:12pt; color: red; left: 25px;}

Save your document with a useful name and a .css suffix (indentstyles.css for example).

You then need to make a reference to this document in the HEAD of your pages.

<LINK REL="stylesheet" TYPE="text/css" HREF=" indentstyles.css">

Try the styles above to see what you get, and then experiment. Note the z-index. You can set your items on layers with a z reference applying to depth. Put something nested on top
(
z-index:-2).

DIV and SPAN

There are two other tags which can come in useful when using CSS. When you want to apply a style to a block of text and you don't want to add any other tag, such as <P>, you can use the tags <DIV> and </DIV>. These will give blank lines either side of your selection. If you don't want any separating space use the tags <SPAN> and </SPAN>.

Generally speaking both main browsers should in their version 4.x incarnations display your styles in the same way. They probably won't. You will have to test for them both. I have found that I can't define styles using reserved names in Netscape. Maybe that will change, maybe not. But it does mean that I can't redefine the headings and then get to use the headings as straight tags. You will have to experiment, and you will probably find things change from version to version. That is the Internet!

Further reading

The following documents on the web should produce useful reading:

  1. CSS Reference guides
  2. CSS Tutorials

Education Home Page

Runway Index

Back to the Top

© John Clare. runway 1998