"Grow Wise, Grow Happy," get organized, simplify, get motivated, increase productivity, accomplish goals, get healthy, raise happy kids, find your authentic self, improve your relationships, achieve success...

 

Using CSS (Cascading Style Sheets)

Updated on July 28, 2007

What is CSS

CSS (Cascading Style Sheets) formats the appearance of your web pages.

Using the Same CSS Code on Multiple Pages

To add style to multiple pages without having to write the code into every page, use an external style sheet. Create a file with just CSS data in it (i.e. my_style_sheet.css), then create a link to the style sheet page by inserting the following code on every page.

<link rel="stylesheet" type="text/css" href="styles/my_style_sheet.css" />

In the link tag, the rel tells the computer what you are linking to (a style sheet). The type tells the computer what kind of style sheet you are using (a text/css style sheet). The href tells the computer where that file is (in this example it's a files called my_style_sheet.css and it's in a directory called styles). You will need to change the href value based on where your web page is in relation to your style sheet.

Using CSS Code on One Page

If you want to add a style to just one web page, you can create an internal style sheet, code directly written into the web page using a style tag.

<style type="text/css">
Selector
{
property1: value1;
property2: value2;
}
</style>

Internal style sheets always override external style sheets.

Using CSS Code for Just One Item in a Web Page

If you want to add a style to just one item on a web page, you can specify the style directly in the tag with an inline style sheet, writing style="" within the tag and inserting the CSS code between the quotes. Notice that you do not need brackets when you define the style in the tag.

<p style="color: green; text-align: right">This text is right aligned and colored green.</p>

Inline styles always override internal style sheets and external style sheet.

Basic Syntax

CSS code has a selector (i.e. the item being modified; e.g. h1), property (i.e. the property being defined; e.g. color), and value (the description of the property being defined; e.g. blue).

Together, they are written as:

Selector {property: value;}
h1 {color: blue;}

Assigning Multiple Properties to a Single Selector

You can list all of a selector's properties and their corresponding values within the brackets, separating them with a semicolon (;). For easier reading, place each property-value set on a separate line.

Selector
{
property1: value1;
property2: value2;
}

Assigning the Same Properties to Multiple Selectors

If you want several selector to all have the same properties and values, you can save space by listing them together, separated by a comma.

Selector1, Selector2, Selector3
{
property1: value1;
property2: value2;
}

Class

Let's say you only want some items to have a specific appearance (i.e. you only want some, not all, <p> tags to have a specific appearance). To tell the computer that we only want certain items to have a specific appearance, we create a class.

A class is written as:

.class1
{
property1: value1;
property2: value2;
}

DO NOT start the name of the class with a number. It won't work in some browsers.

A class without a selector can be used with any element tag.

.rct
{
text-align: center;
color: red;
}

In our web page the html for any item that we wanted to be centered and colored red would look like:

<p class="rct">This text is centered and colored red.</p>
<h1 class="rct">This text is centered and colored red.</p>
<td class="rct">This text is centered and colored red.</p>

If you only want to have the class apply to a specific type of element (e.g. just <p> tags or just <td> tags), place a selector in front of the class. You can assign multiple classes to a single type of selector.

Selector1.class1
{
property1: value1;
property2: value2;
}

p.alpha
{
text-align: right;
color: blue;
}

p.beta
{
text-align: left;
color: green;
}

ID Selectors: Not to Be Confused with Class

You can have many tags on a web page that have the same class, but you can only have a tag with an ID once. The ID is a way to tell the computer, "This, and only this, single tag will have these properties."

This is useful when doing things like positioning and special headings or anything else that you don't want to accidentally have the same. For example, having two paragraphs with the same position means that one paragraph will be on top of the other one, and that will make it difficult to read, so using the ID will ensure that only one paragraph will be in position A and the other paragraph won't.

Instead of using a period we use a pound sign (#).

#id
{
property1: value1;
property2: value2;
}
#cg
{
text-align: center
color: green
}

In our web page the html would look like:

<p id="cg">This text is centered and green.</p>

Like the classes, you can specify that an ID may only be used for a specific type of element by putting a selector in front of the ID.

Selector1#id
{
property1: value1;
property2: value2;
}

p#cg
{
text-align: center
color: green
}

Inserting Comments in Your CSS Code

If you like to write notes in your code, such as "This is the code for the announcements box," you just need to write your comments between /* and */. These comments will not show up on your web page.

/* This is my comment. */

If you liked this page, please share it with others...

| | Tell a friend
Stay Updated! Get updates delivered to your inbox or XML Get Updates via Feed

Scroll down to comment on this article.

Related Items

Similar topics

See other topics in this category.




All content written by Kristen Brooke Beck (a.k.a. Kristen Beck, a.k.a. Kristen Helmer)

© 2002-2008 Kristen Brooke Beck and its affiliates and assigns and licensors.
All rights reserved.

TM Kristen's Guide , "Grow Wise, Grow Happy!", and the Kristen's Guide flower logo are trademarks of Kristen Brooke Beck. Trademarks may be registered in some jurisdictions and may not be used without written permission.
All other trademarks are the property of their respective owners.

DO NOT distribute without explicit written permission. Request Permission to Reprint an Article or Printable

Information on Kristen's Guide: http://www.kristensguide.com is an expression of personal opinion intended for educational and entertainment purposes and is not a substitute for services from a licensed professional. By accessing this website or using the information from this website in any way, you are agreeing to the terms of the User Agreement at http://www.kristensguide.com/user_agreement.asp.

Search

Stay Updated

Get notified when new printables, articles, and tools are available.

Add This!
XML Updates by Feed
Updates by Email

Free Tools Free Printables
Free Calculators
Free Site Widgets
Get Free Advice

Kristen's
Guide to...
Finding Happiness
Helping the World

Getting Healthy
Losing Weight
Looking Beautiful

Managing Your Home
Getting Organized
Food
Emergencies

Family & Parenting
Genealogy
Relationships & Friendships

Etiquette & Courtesy
Holidays & Gift Ideas
Travel

Money
Home Based Business

Computers
Website Design
Writing

Help KG

If you like KG and want to see more free printables, tools, and articles, then please help keep us going.

Link to KG on your blog, website, or social profile. (It's the best thing you can do to help.)


Tell a Friend About Us
Send a Comment
Report a Problem
Start a Discussion

Advertise

Advertise on KG

Site Statistics

Services Get Reprint Rights
Get Reviews of Your Products

About KG Who is Kristen?
What is Kristen's Guide?
Testimonials
Site Map
User Agreement & Privacy Policy
Contact Info
Home Page

Recommended

Kristen's Picks
Planners & Organizers

Favorite Sites

American Cancer Society Logo

UNICEF Logo

National Center for Missing and Exploited Children Banner on Kristen's Guide



Get
Kristen's Guide
Updates

Add This!
XML...by Feed
by Email



Latest
Kristen's Guide
Updates

See All Updates


Get
Kristen's Guide
Updates

Add This!
XML...by Feed
by Email



Get
Kristen's Guide
Updates

Add This!
XML...by Feed
...by Email



Get
Kristen's Guide
Updates

Add This!
XML...by Feed
...by Email


Recent Visitors


Get
Kristen's Guide
Updates

Add This!
XML...by Feed
by Email