The syntax for CSS is different than that of (X)HTML markup. Though it is not too confusing, once you take a look at it. It consists of only 3 parts.
selector { property: value }
The selector is the (X)HTML element that you want to style. The property is the actual property title, and the value is the style you apply to that property.
Each selector can have multiple properties, and each property within that selector can have independent values. The property and value are separated with a colon and contained within curly brackets. Multiple properties are separated by a semi colon. Multiple values within a property are sperated by commas, and if an individual value contains more than one word you surround it with quotation marks. As shown below.
body {
background: #eeeeee;
font-family: “Trebuchet MS”, Verdana, Arial, serif;
}
background: #eeeeee;
font-family: “Trebuchet MS”, Verdana, Arial, serif;
}
As you can see in the above code I have separated the color from the font-family with a semi-colon, separated the various fonts with commas and contained the “Trebuchet MS” within quotations marks. The final result sets the body color to light grey, and sets the font to ones that most users will have installed on there computer.
I have changed the way I layout my code, but you can arrange it in one line if you choose. I find that it is more readable if I spread each property to a separate line, with a 2 space indention.
Inheritance
When you nest one element inside another, the nested element will inherit the properties assigned to the containing element. Unless you modify the inner elements values independently.
For example, a font declared in the body will be inherited by all text in the file no matter the containing element, unless you declare another font for a specific nested element.
body {font-family: Verdana, serif;}
Now all text within the (X)HTML file will be set to Verdana.
If you wanted to style certain text with another font, like an h1 or a paragraph then you could do the following.
h1 {font-family: Georgia, sans-serif;}
p {font-family: Tahoma, serif;}
p {font-family: Tahoma, serif;}
Now all <h1> tags within the file will be set to Georgia and all <p> tags are set to Tahoma, leaving text within other elements unchanged from the body declaration of Verdana.
There are instances where nested elements do not inherit the containing elements properties.
For example, if the body margin is set to 20 pixels, the other elements within the file will not inherit the body margin by default.
body {margin: 20px;}
Combining Selectors
You can combine elements within one selector in the following fashion.
h1, h2, h3, h4, h5, h6 {
color: #009900;
font-family: Georgia, sans-serif;
}
color: #009900;
font-family: Georgia, sans-serif;
}
As you can see in the above code, I have grouped all the header elements into one selector. Each one is separated by a comma. The final result of the above code sets all headers to green and to the specified font. If the user does not have the first font I declared it will go to another sans-serif font the user has installed on their computer.
Comment tags
Comments can be used to explain why you added certain selectors within your css file. So as to help others who may see your file, or to help you remember what you we’re thinking at a later date. You can add comments that will be ignored by browsers in the following manner.
/* This is a comment */
You will note that it begins with a / (forward slash) and than an * (asterisks) then the comment, then the closing tag which is just backward from the opening tag * (asterisks) then the / (forward slash).