Introduction To Cascading Style Sheets (CSS)
HTML is very limited in the way it allows you to format your web pages. You have some
control of where things appear on the page by putting them in tables, but they are very
awkward to use. Frames are some help, but they tend to be very annoying. You can
control the fonts that appear on your pages with the <FONT> tag, but they tend to require
a lot of code. This is because HTML was intended to mark up a page's structure, not it
appearance on the screen. This has changed with the introduction of a complementary
markup system called Cascading Style Sheets (CSS). CSS allows you to define a page's
appearance without affecting its HTML structure.
H2 {font-family: Arial; font-style: italic; color: red; font-weight: bold }
In the above example, the <H2> element is the selector. What followers H2 is the style
definition. Once the above style is applied to an HTML document, every <H2> element will
appear red, italic, bold and Arial plus its inherent HTML characteristics.
A simple way of combining styles to an HTML document is to define the styles in the head
or the body of an HTML document. To place your styles in the head of an HTML document
your selectors and definitions go between the tags:
The comment tags (<!-- -->) are used to prevent older browsers that do not recognize the
<STYLE> code from displaying the style codes on the screen.
Below is a sample HTML page using CSS.
This header is red, centered and Arial
This header is gray, centered and Arial
This header is blue, not centered and Arial
This header is black, left aligned and Arial
This text is black, justified, 12 point and Arial. Now is the time for all good men to come to the aid of their party.
External Style Sheets
Defining styles in the head of an HTML document is helpful if you only want to affect one
Web page. However, if you want to affect several pages or an entire Web site then you
should create an external style sheet and link to it from your HTML documents. For
example, we will create a file called mystyle.css that contains this text:
H3 {font-family: Comic Sans MS; font-weight: bold; color: red }
Next, create a new HTML document named mystyle.html. We will add a <LINK> element
that calls our style sheet (mystyle.css) in the document's head:
The code in the <LINK> tag tells the browser to find an external style sheet that is a css
file and the name of the file is mystyle.css (assuming its in the same directory as the HTML
file.). The title attribute is only used to give the style sheet a name to reference. The file
mystyle.css defines the styles in mystyles.html. This means that every <H3> element you
use in the HTML document will have the red, bold, Comic Sans MS style defined in
mystyle.css.
Another way to link to an external style sheet is with the following code:
The @import statement works similar to the <LINK> element. However, the @import
statement is a little buggy in the 4.0 versions of Navigator and Internet Explorer. So you
might be better off using the <LINK> statement.
H3 {font-family: Arial }
In this example, H3 is the selector (in this case an HTML element), and font-family: Arial
is its defined style. This definition is a combination of a property and its value, seprated
by a colon. Properties are characteristics an element can assume, such as, font type, font
size, and color. Values are specific traits those properties can have, such as, Arial, 18
point, or red. Multiple properties in the same definition are separated with semicolons. For
example:
In the above example, Arial is the preferred type face. Helvetica is the second choice. This
is done in case the user's system does not have Arial the second choice will be used. If
a browser can't match any of the choices, it will use its default font.
Now create an HTML file as shown below and name it style1.html:
If you can, look at the above using Netscape and then using MS Explorer.
You will find that there is some differences in the way the file appears in Netscape and Explorer. At present this is one draw back to using styles. Not all browsers interpret the codes the same.
|