|
||||||||||
Basic concepts |
||||||||||
|
||||||||||
Advanced concepts |
||||||||||
|
There are three basic ways of declaring style data. The first is in the <HEAD> of pages, the second is at tag level and the third is in a separate external style sheet.
In each case, style sheets do not contain any HTML, just style commands. For example:
P {color: red}
is a style statement.
Style sheets are linked using the <LINK rel="stylesheet" type="text/css"
href="name.css">
tag, which must go in the header of a page.
The tag's various attributes indicate things about the style sheet - the rel attribute, the type of link (a style sheet); the type attribute, the type of style sheet (always text/css); and the href attribute, the location of the style sheet.
<HTML>
<HEAD>
<LINK rel="stylesheet" type="text/css" href="fluorescent.css">
</HEAD>
<BODY>
...
</BODY>
</HTML>
Once you have linked the style sheet to your page, you then have to create the style sheet. For example:
BODY {color: black;
font-family: Geneva, Helvetica, Arial, sans-serif;
background-color: white}
If you saved the example above as a style sheet, then every page that links to it will have the specified styles.
Linked style sheets are advantageous insofar as one style sheet can be attached to hundreds of files. Thus they are the only way to go if you want consistency of style, and indeed in my opinion they are the only way to go anyway. Their only disadvantage is that if a page is downloaded and read offline, the user is unlikely to bother to save the style sheet. The main reason for using them is that you can attach the same sheet to many files ensuring consistency and reducing file sizes.
Say you have standard corporate margins in corestyles.css:
BODY {margin-left: 0.5in;
margin-right: 0.3in}
However, you have different color schemes:.
@import url(corestyles.css);
BODY {color: red;
background-color: black}
The @import rule thus allows you to keep some things the same while having others different; and
follows this syntax: @import url(nameoffile.css);
. It must come at the start of the
style sheet, before any rulesets (a ruleset is something like P {color: red}).
Alternatively it can be specified as @import "nameoffile.css";
, as @import
url("nameoffile.css");
or as @import 'nameoffile.css';
. However, Internet Explorer only
supports the url() formats, not the " and ' formats.
There are esssentially three reasons for using @import:
@import url(bidi.css);
That way there is no chance of accidentally changing the bidi settings, and you can control the bidi of many style sheets.
@import url(margins3.css);
@import url(fonts1.css);
@import url(colors2.css);
could be the style sheet, using the fonts sheet 1, the margins sheet 3 and the colors sheet 2.
Embedded style sheets are enclosed in a <STYLE type="text/css">
and
</STYLE>
tag. They go in the header of a page:
<HTML>
<HEAD>
<STYLE type="text/css">
<!--
BODY {color: red}
-->
</STYLE>
</HEAD>
<BODY>
...
</BODY>
</HTML>
Note the <!--
and -->
tags. These are the HTML comment tags, and
are unnecessary for browsers that do support style sheets, but are
essential for those that do not to avoid the unsightly problem of the style sheet appearing on the
screen. This affects about 4% of all browsers.
They have two benefits over linked style sheets:
Any tag except the <HTML>
tag itself can take the style
attribute:
<P style="color: green">
</P>
Inside the style attribute comes style declarations. In this example, the P element is being made green.
The most important difference between inline (tag-level) style and other styles is that by applying the style to an individual element, you have selected the element unambiguously, and therefore inline style does not use selectors. Thus with the example above, there is no need for a selector, whereas if you were making the element green in a LINKed or embedded style sheet, you would need to have P {color: green}.
In my opinion, inline style should be avoided, since it does not indicate why you have changed the style. Thus if you gave the paragraph above a name that indicates its content, you could easily change it. The means of doing this is discussed in the next section.
The bare HTML & CSS document should look like this:
<HTML>
<HEAD>
<LINK rel="stylesheet" type="text/css" href="filename.css">
</HEAD>
<BODY>
...
</BODY>
</HTML>
And the CSS document looks like this:
...
At this stage you should be able to create your own style sheet. Most probably you will do this by adding a <LINK rel="stylesheet" href="nameoffile.css" type="text/css">
tag to the HEAD of your pages. Then you will creating the file 'style.css'. Try creating one now - you should have got the idea how to specify that something should be red or green or whatever - P {color: red} or BODY {font-size: 16px}. When you've played around a bit, the next section deals with how styles are
specified as applying to individual elements, etc.
Copyright © RichInStyle.com 2000; all rights reserved. See copyright document for terms of use. Please visit Bukit Lawang flood appeal.