Please visit our sponsors !
CSS Syntax
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:
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
}
|
Jump to: Top of Page
or HOME or
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
|