Understanding CSS Syntax
Understanding CSS Syntax
Syntax
In this tutorial you will learn how to define CSS rules in your stylesheet.
A CSS rule have two main parts, a selector and one or more declarations:
The selector specifies which element or elements in the HTML page the CSS rule
applies to.
Whereas, the declarations within the block determines how the elements are
formatted on a webpage. Each declaration consists of a property and a value
separated by a colon (:) and ending with a semicolon (;), and the declaration
groups are surrounded by curly braces {}.
The property is the style attribute you want to change; they could be font, color,
background, etc. Each property has a value, for example color property can have
value either blue or #0000FF etc.
h1 {color:blue; text-align:center;}
To make the CSS more readable, you can put one declaration on each line, like
this:
Example
Try this code »
h1 {
color: blue;
text-align: center;
}
Note: A CSS declaration always ends with a semicolon ";", and the declaration
groups are always surrounded by the curly brackets "{}".
Example
Try this code »
/* This is a CSS comment */
h1 {
color: blue;
text-align: center;
}
/* This is a multi-line CSS comment
that spans across more than one line */
p {
font-size: 18px;
text-transform: uppercase;
}
You can also comment out part of your CSS code for debugging purpose, as
shown here:
Example
Try this code »
h1 {
color: blue;
text-align: center;
}
/*
p {
font-size: 18px;
text-transform: uppercase;
}
*/
Therefore, to be on safer side, you should assume that all components of CSS
rules are case-sensitive.
You will learn about the various types of CSS selectors in the next chapter.