An Introduction To Cascading Styling Sheets
by K. Yue, copyright 2000
Revised: September 18, 2000

Introduction to CSS

Defining Styles

Example 1: Using CSS (cssiontro1.htm)

<HTML>
<HEAD>
<STYLE type="text/css">
<!--
   h2 { font-family: Arial; font-weight: bold; color: blue }
-->
</STYLE>
</HEAD>
<BODY>
<H2>My Hobbies</H2>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
</UL>
<H2>My Skills</H2>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
   <LI>Pretending to work
</UL>
</BODY>
</HTML>

Without using CSS, the corresponding HTML file looks like this (nocssintro1.htm):

<HTML>
<HEAD>
<!--
A simple example of not using cascading style sheets.
-->
</HEAD>
<BODY>
<B><FONT face="Arial" color="blue"><H2>My Hobbies</H2></FONT></B>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
</UL>
<B><FONT face="Arial" color="blue"><H2>My Skills</H2></FONT></B>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
   <LI>Pretending to work
</UL>
</BODY>
</HTML>

Example 2:

Define the file h2.css:

h2 { font-family: Arial; font-style: bold; color: blue }

Link the file h2.css when needed (cssintro1usinglink.htm)

<HTML>
<HEAD>
<!--
A simple example of using cascading style sheets and the link tag.
-->
<LINK rel="stylesheet" type="text/css" href="h2.css" title="h2">
</HEAD>
<BODY>
<H2>My Hobbies</H2>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
</UL>
<H2>My Skills</H2>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
   <LI>Pretending to work
</UL>
</BODY>
</HTML>

Alternatively, it is possible to use the import command with the <style> tag.

<STYLE TYPE="text/css">
<!--
@import url(h2.css);
... remaining styles.
-->
</STYLE>

Selector and Properties

Example 3:

h2 { font-family: Arial, Helvetica, sans-serif }

In this case, the browser will try to set the property font-family to Arial, Helvetica and sans-serif in order.

Example 4: classandid.htm

<HTML>
<HEAD>
<!--
A simple example of using cascading style sheets.
-->

<STYLE type="text/css">
<!--
.weirdcolor { color: #ff3333; font-family: sans-serif, Arial, Helvetica}
#weirdo { color: #33ff33 }
h2 { font-family: Arial; font-weight: bold; color:blue }
-->
</STYLE>
</HEAD>
<BODY>
<H2>My Hobbies</H2>
<UL>
   <LI><STRONG CLASS="weirdcolor">Sleeping</STRONG>
   <LI><EM ID="weirdo">Eating</EM>
   <LI>Watching Television
</UL>
<H2>My Skills</H2>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
   <LI>Pretending to work
</UL>
</BODY>
</HTML>

Example 5: selector1.htm

<HTML>
<HEAD>
<!--
A simple example of using cascading style sheets illustrating selector options.
-->

<STYLE type="text/css">
<!--
strong h2 { font-family: Arial; font-weight: bold; color: red }
h3, li, td { color: blue
}
h3 { font-family: sans-serif;
font-style: italic }
-->
</STYLE>
</HEAD>
<BODY>
<H2>My Hobbies</H2>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
</UL>
<STRONG><H2>My Skills</H2></STRONG>
<UL>
   <LI>Sleeping
   <LI>Eating
   <LI>Watching Television
   <LI>Pretending to work
</UL>
<h3>The End</h3>
</BODY>
</HTML>

Note:

  1. The style definition for h3 only applies when it is embedded within a strong tag.
  2. The style definition of h3, li and td are the same. Later, h3 is defined again to add additional properties.

Example 6:

div.code {font-family:Arial}

Example 7:

<h4 style="font-family: Helvetica; color=blue">Hello</h4>

Example 8:

The style used in preparing this lecture is:

<!--
.mod { font-family: Arial, Helvetica, sans-serif; font-size: 16pt;
       font-weight: bold; color: #000099; text-align: center }
.section { font-family: Arial, Helvetica, sans-serif; font-size: 14pt;
           color: #000099; font-weight: bold }
body { font-family: "Times New Roman", Times, serif;
       background-color: #FFFFCC;
       list-style-image: url(../../images/blueball.gif);
       width: auto; color: #000000; list-style-type: none }
.subsection { font-family: Arial, Helvetica, sans-serif;
              font-size: 12pt;
              color: #000099; font-weight: bold }
.subtitle { font-family: "Times New Roman", Times, serif;
            font-size: x-small; color: #000099;
            font-weight: lighter; text-align: center }
.pcode { font-family: "Courier New", Courier, mono; font-size: 10pt;
         color: #660000; border: 1px #000000 solid;
         border-color: #003333 #000000 #000000;
         padding-top: 5px; padding-right: 10px;
         padding-bottom: 5px; padding-left: 10px;
         border-width: 1px 1px 1px 1px; height: auto; width: 600px }
.exhead { font-family: "Times New Roman", Times, serif;
          font-style: italic; color: #006633 }
.Exercisehead { font-family: Arial, Helvetica, sans-serif;
                font-size: 11pt; color: #660066 }
.ExerciseBody { font-family: "Times New Roman", Times, serif;
                font-size: 11pt; color: #660066 }
-->