HOME

CSS Basic
CSS HOME
CSS Introduction
CSS Syntax
CSS How To
CSS Background
CSS Text
CSS Font
CSS Border
CSS Margin
CSS Padding
CSS List

CSS Advanced
CSS Dimension
CSS Classification
CSS Positioning
CSS Pseudo-class
CSS Pseudo-element
CSS Media Types

Examples/Quiz
CSS Examples
CSS Quiz Test

References
CSS2 Reference
CSS2 Print
CSS2 Aural
CSS Units
CSS Colors
CSS Colorvalues
CSS Colornames
Online Degrees
Please visit our sponsors !

CSS Syntax

Previous Next

Syntax

The CSS syntax is made up of three parts: a selector, a property and a value:

selector {property: value}

The selector is normally the element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon and surrounded by curly braces:

body {color: black}

If  the value is multiple words, put quotes around the value:

p {font-family: "sans serif"}

Note: If you wish to specify more than one property, you should separate each property with a semi-colon. The example below shows how to define a center aligned paragraph, with a red text color:

p {text-align: center; color: red}

To make the style definitions more readable, you can describe one property on each line, like this:

p
{
text-align: center;
color: black;
font-family: arial
}


Grouping

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. Each header element will be green:

h1, h2, h3, h4, h5, h6 
{
color: green
}


The class Attribute

With the class attribute you can define different styles for the same element. Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:

p.right {text-align: right}
p.center {text-align: center}

You have to use the class attribute in your HTML document:

<p class="right">
This paragraph will be right-aligned.
</p> 

<p class="center">
This paragraph will be center-aligned.
</p> 

You can also omit the tag name in the selector to define a style that can be used by many elements:

.center {text-align: center}

In the code below both the h1 element and the p element are classed with "center". This means that both of the elements will follow the rules in the ".center" selector:  

<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p> 


The id Attribute

The id attribute can be defined in two ways. It can be defined to match all elements with a particular id, or to match only one element with a particular id.

<p id="intro">
This paragraph will be right-aligned.
</p>

In this example the id attribute will match all elements with id="intro":

#intro
{
font-size:110%;
font-weight:bold;
color:#0000ff;
background-color:transparent
}

In this example the id attribute will match only p elements with id="intro":

p#intro
{
font-size:110%;
font-weight:bold;
color:#0000ff;
background-color:transparent
}


CSS Comments

You can insert comments in CSS to explain your code, which can help you when you edit the source code at a later date. A comment will be ignored by the browser. A CSS comment begins with "/*", and ends with "*/", like this:

/* This is a comment */
p
{
text-align: center;
/* This is another comment */
color: black;
font-family: arial
}


Previous Next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

Domain Name Registration
Domain Name
Registration & More!



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search