Search
Close this search box.
Search
Close this search box.

Basic CSS Syntax

The syntax for CSS is different than that of (X)HTML markup. 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.
  • 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 seperated with a colon and contained within curly brackets. Multiple
properties are seperated 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.

body {
    background: #eeeeee;
    font-family: "Trebuchet MS", Verdana, Arial, serif;
}

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;}

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.

By default links do not inherit attributes like color.

 

 

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;
}

As you can see in the above code, I have grouped all the header elements into one selector. Each one is seperated by a comma. The final result of the above code sets all headers to green and to the specified font.