Rules
The best thing about using a Cascading Style Sheet is that it is really simple
to create. A style sheet is essentially a set of one component :
the Rule.
A rule defines what a specified portion of
HTML, i.e. Heading, should look like and how it should behave in the browser
window. Once you understand how
to write one rule, you can easily write a set of them to create a style
sheet. Style sheets are saved as .css files,
i.e. mainPageGuide.css.
Parts of a Rule
There are three parts to every rule: the selector, property, and value.
Selector: Names the rule
and/or identifies its type.
Property: Style element
being defined. There are a variety of different properties, each one responsible
for an aspect of the page's appearance and behavior. Property categories include
Type, Background, Block or Text Spacing, Box or Borders, List, Positioning,
and Extensions.
Value: Quality or quantity
assigned to a property to define its 'nature'. A value can be a keyword such
as 'yes' or 'no', or a number. The type of value used will depend on the property
being defined.
Together, the property and value
are known as the declaration.
The
diagram for a rule would be selector {declaration}
or more specifically selector {property: value
}.
In the example, h1
{ font-style: bold } : h1 is
the selector, font-style is the property being defined, and bold
is the value or how the property is being defined.
|
Understanding Selectors
---------------------------
Selectors indicate the rule type and what will be affected. There are
three main types of selectors: the HTML selector, class selector, and
ID selector.
HTML Selectors
An HTML selector is the text portion of an HTML tag. For example, H3
is the selector for the <H3> tag . All HTML tags have default properties.
The HTML selector can be used to control the behavior
of its specific HTML tag. To change the HTML tag's
behavior, just change or redefine the selector's properties. A list
of properties available in CSS1 can be found on the Properties page.
Creating a Rule by Redefining a HTML selector:
1. Start with the HTML selector whose properties you want to change and
then add a opening bracket, { , to open your
definitions list.
2. Type in all your property declarations, ending each declaration
with a semicolon, ;.
h3{
font: bold 14pt helvetica, arial, sans-serif;
color: red;
|
3. } Close your declarations list with a closing bracket, } .
h3{
font: bold 14pt helvetica, arial, sans-serif;
color: red;
} |
Once you redefine an HTML selector, all of its
tags throughout the website will be affected automatically.
Class Selectors
Class selectors are selectors that you create from scratch. They
can be applied to any HTML tag, so are considered the most versatile type
of selector.
Creating a Class Selector Rule
1. Type a period and assign a class name. The name can be anything as long
as it is letters and numbers. Then open your declaration with a bracket,
{ .
2. Type in your declarations for the class, ending each declaration with
a semicolon, ;.
.topic_heading
{
font-size:36 pt;
font-style:bold;
color: 000033;
|
3. Type a closing bracket, } , to end your rule.
.topic_heading
{
font-size:36 pt;
font-style:bold;
color: 000033;
} |
Using a Class Selector Rule in an HTML document
Unlike an HTML selector, which works with existing tags within an HTML document,
a class selector does not affect the document automatically. It must
be referenced or called within the HTML document to have any affect.
1. Choose the opening tag of an HTML element which you would like to apply
the class rule to.
<html>
<body bgcolor ="ffffff">
<h3 align="center"> My Summer Vacation</h3>
</body>
</html>
|
2. Insert class to indicate that you will
be applying a class rule, then set it to the name of the class rule
you are referencing, = "classRuleName".
<html>
<body bgcolor ="ffffff">
<h3 class="topic_heading" align="center">
My
Summer Vacation
</h3>
</body>
</html>
|
ID Selectors
ID selectors are very similar to class selectors. They can be applied to
any HTML tag and need to be referenced within the HTML document to
have any affect.
The only real difference is when they should be used. An ID selector
is generally reserved for something that appears once on
a page. For example, if you have a page with a set of several topic
headings, but there is one particular
topic heading that you would like to stand out from the rest, then
you would create an ID selector rule for that specific topic heading.
The ID selector gives it a unique name and identity, so you can change
that
one topic heading without affecting the rest
of the set.
ID selectors are most commonly used with positioning properties since
the page layout of a website may vary from page to page. For each page,
you could create ID selector rules that would adjust specific sections
of the layout to meet your needs.
Creating an ID Selector Rule
1. Type a number sign, #, and assign an ID name. The name can be anything
as long as it is letters and numbers. Then open your declaration with
a bracket,
{ .
2. Type in your declarations for the ID, ending each declaration with
a semicolon, ;.
#special_heading
{
font-size:36 pt;
font-style:bold;
color: 990000;
|
3. Type a closing bracket, } , to end your
rule.
#special_heading
{
font-size:36 pt;
font-style:bold;
color: 990000;
}
|
Using an ID Selector Rule in an HTML document
Similar to a class selector rule, an ID selector rule does not affect the
document automatically. It must be referenced or called within the HTML document
.
1. Choose the opening tag of an HTML element which you would like to apply
the ID rule to.
<html>
<body bgcolor ="ffffff">
<h1 align ="center"> My Summer
Vacation</h1>
..............
<h3 align ="center">Camping</h3>
..............
<h3 align ="center">Movies</h3>
...............
<h3 align ="center"> Paris and the Louvre</h3>
</body>
</html>
|
2. Insert ID to indicate that you will be
applying an ID rule, then set it to the name of the ID rule you are referencing, = "idRuleName".
<html>
<body bgcolor ="ffffff">
<h1 align ="center">My Summer Vacation</h1>
.............
<h3 id="specialHeading" align ="center">
Paris
and the Louvre< /h3>
</body>
</html>
|
Grouping Rules
-----------------
One way to make style sheets shorter and easier to navigate
is by grouping similar rules. Below are some examples:
If we
have rules with identical
declarations, i.e.
h1 { font-weight: bold; }
h2 { font-weight: bold; }
h3 { font-weight: bold; }
we, can group selectors
into a comma-separated list and only list the declaration once, like
this:
h1, h2, h3 { font-style :bold; } |
If we have rules
with the same selector, i. e.
h1 { color:green; }
h1 { text-align: center; }
we, can group declarations
into a semicolon- separated list like this:
h1 {
color:green;
text-align: center;
}
|
Applying Rules to Specific Items
------------------------------------
Style rules are commonly applied to HTML elements through their HTML tags.
If, however, you wanted to apply a style to just one word within a paragraph,
what
do you do? That one word is not considered an HTML element on its own so does
not have an HTML tag associated with it. Although you cannot create individual
HTML tags for such specific items, there are two HTML tags, <div> and <span>,
that will sufficiently allow you to apply the style rules to such items.
The <div> and <span> tags are very similar, except
the <div>
tags add a break above and below the item they enclose. The <span> tags
alone do no have any properties, so you can easily apply any style to the item
they
enclose.
Using <div> and <span> tags
Both the <div> and <span> tags can be used in the same way as any other HTML
tag, i.e. <i> for italics or <b> for bold. To apply a style, the instructions
are the same as for Using a Class Selector Rule or Using
an ID Selector Rule.
An example for
using both the <DIV> and <SPAN> tags are below. The example assumes the "french"
and "revered"
styles
have
already been created.
<html>
<body bgcolor ="ffffff">
<center><h3 id="specialHeading" align="center">
Paris
and the Louvre< /h3></center>
<p>We arrived in Paris on a hot summer day. After
a night of good sleep, we started the morning off shopping along the
Champs Elysses. We then headed to the Louvre to see the:<div class="french">
Mona Lisa </div>
<p>It is really creepy, her eyes do seem to follow
you around. The Mona Lisa was painted by my favorite artist,
<span class ="revered">Leonardo
Da Vinci<span>. His sketches of the
human anatomy are amazing
</body>
</html>
--------------------------------
An example of what the webpage may look
like from the code above.
--------------------------------
Paris and the Louvre
We arrived in Paris on a hot summer day. After
a night of good sleep, we started the morning off shopping along the
Champs Elysses. We then headed to the Louvre to see the:
Mona Lisa
It is really creepy, her eyes do seem to follow
you around.
The Mona Lisa was painted by my favorite artist,
Leonardo da Vinci.His sketches of the
human anatomy are amazing.
|
CSS Comments
-----------------
CSS comments are used in the same way as HTML comments are used in HTML documents.
A comment does not have any affect on the appearance of the webpage. It allows
you to make notes on the code being written. This makes it easier for you
to keep track of what you're doing as well as being a reference guide for other
people who may use your CSS style sheet later.
There are two ways to make CSS comments : single-line and multi-lined
To Write Single-Line Comments
1. Start a comment line by typing two backslashes, //.
2. Type in your comment . There must be no line breaks.
//My
first CSS Document
//font color of the Main Heading is set to Navy
Blue.
.main_heading {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: medium;
color: #000066;
}
//font color of the Topic Headings are set
to Red.
.topic_headings {
font-family: Times New Roman, Times, serif;
font-size: small;
font-weight: bold;
color: #660000;
}
|
To Write Multi-Lined Comments
1. Open your comment with a backslash and star symbol, /*.
2. Type in your comment.
3. Close your comment with a star symbol and backslash, */.
/*
My first CSS Document */
/*This style will be used to set the Main
Heading on my pages to be medium, Navy Blue, and in a sans-serif
font*/
.main_heading {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: medium;
color: #000066;
}
/*This style will be used to set the Topic
Headings on my pages to be small, Red, bold, and in a serif font*/
.topic_headings {
font-family: Times New Roman, Times, serif;
font-size: small;
font-weight: bold;
color: #660000;
}
|
- Created by: Tuyet
Nguyen
- Wellesley College Information Services
- Date Created: September 19, 2003
- Last Modified:
November 14, 2005
- Expires: June 1, 2004