CSS syntax
CSS Rules consist of two main parts: Selector and one or more declarations.
selector {declaration1; declaration2; ... declarationN }
A selector is usually an HTML element that you need to change the style.
Each declaration consists of an attribute and a value.
Property is the style attribute you want to set ). Each attribute has a value. The attribute and value are separated by colons.
selector {property: value}
The following code defines the text color in the h1 element as red, and sets the font size to 14 pixels.
In this example, h1 is the selector, color and font-size are attributes, and red and 14px are values.
h1 {color:red; font-size:14px;}
The following shows the structure of the above Code:
Tip:Use curly brackets to enclose the declaration.
Different write methods and units of Values
In addition to the English word red, we can also use the hexadecimal color value # ff0000:
p { color: #ff0000; }
To save bytes, we can use the abbreviated form of CSS:
p { color: #f00; }
We can also use RGB values in two ways:
p { color: rgb(255,0,0); }p { color: rgb(100%,0%,0%); }
Note that when the RGB percentage is used, the percentage symbol must be written even if the value is 0. However, this is not required in other cases. For example, when the size is 0 pixels, px units are not required after 0, because 0 is 0, regardless of the unit.
Remember to write quotation marks
Tip:If the value is a number of words, enclose the value with quotation marks:
p {font-family: "sans serif";}
Multiple declarations:
Tip:To define more than one declaration, separate each declaration with a semicolon. The following example shows how to define the center section of a red text. The last rule does not require extra points, because the semicolon is a separator in English, not an ending symbol. However, most Experienced designers add a semicolon at the end of each statement. The advantage is that when you increase or decrease the statement from the existing rules, will minimize the possibility of errors. Like this:
p {text-align:center; color:red;}
You should describe only one attribute in each row to enhance the readability of the style definition, as shown in the following figure:
p { text-align: center; color: black; font-family: arial;}
Space and case
Most Style Sheets contain more than one rule, while most rules contain more than one declaration. Multiple declarations and spaces make the style sheet easier to edit:
body { color: #000; background: #fff; margin: 0; padding: 0; font-family: Georgia, Palatino, serif; }
Whether or not to contain spaces does not affect the effect of CSS in the browser. Similarly, unlike XHTML, CSS is not case sensitive. However, there is an exception: if it involves working with HTML documents, the class and id names are case sensitive.