Cascading Style Sheets.

Picture this: you are a web designer who just finished creating a company's web site (about 200 documents). You tired and happy enjoying your favorite music and a cup of coffee (glass of beer, etc) when unexpectedly your boss comes in and tells you that according to the last decision of a web committee all headers in the documents should be blue, each page should have a background image with the company's logo, all tables should have 2 pixels border, all links should be blue, and so one. After the speech you are given time till the end of the day to do all of these changes. How does it sound?

Fortunately, you know about CSS (Cascading Style Sheets), don't you? CSS is a way to assign rules how HTML objects should look in web browsers. These rules allow you to ' manipulate with fonts, colors, backgrounds, positions, spaces, sizes, etc of any particular object, some objects, or all of them at once. Every CSS rule has the form:

selector{ property: value; ... }
Where selector defines the object(s) these rules should be applied to, and property and values defines the changes you want to make. For example:
H2{
  font-size: 110%;
  font-family: Verdana;
}

How to insert styles in web documents.

There are four ways to insert such rules in your document:
  • using inline styles for HTML elements;
  • embedding styles in the document;
  • linking style sheets;
  • importing external styles sheets.

    Inline style is probably the easiest form of CSS. To apply particular rules to an object we need to specify parameter style of the object. The value of this parameter is a string with CSS rules separated with semicolon. Here is an example:
    Code Result
      <span style="font-family:monospace; text-transform:capitalize;">
      This text is displayed using CSS.
      </span>
      
    This text is displayed using CSS.
    This method is very simple and good for debugging, but it forfeits the whole idea of CSS. To apply the same style to a group of objects we need to manually edit each of them. This method is useful when you need to apply a particular style to one single object on your page.

    For now we'll concentrate on embedding style sheets. To embed styles in your document inside the header of your documents we need to use tag <style> (requires the closing tag </style>). Inside the style tag we can write our style rules. Here is an example:

    ...
    <head>
     <title>My First Styled Page</title>
     <style>
      body { font-family: Verdana; font-size:90%; }
      h3 {
        background-color: darkblue;
        color: yellow;
      }
     </style>
    </head>
    ...
    
    The rules we described in this style will be applied to the tag body; that is to the whole document, and to all headers of the third level in the document, but to only documents where they are ' embedded into.

    Selector or objects for the rules.

    Now let's talk about selector or how we can define objects for our rules. First of all, any tag name can be a selector:
    H2{
      font-size: 110%;
      font-family: Verdana;
    }
    
    In this example H2 is a selector; that is, these rules should be applied to all second level headers in the current document. We also can apply the same rules to several tags. For example, if we want to change all headers of levels 1 through 3 we can list them separating with commas:
    H1, H2, H2 {
      font-size: 110%;
      font-family: Verdana;
    }
    

    We can also put some conditions on the objects we want to change. For example, if we want to change colors of emphasized text (inside em tag) only if this text is inside tables, then we can do this:

    table em {  color: red; }
    

    For now we know how to redefine all elements created with a specific tag or some of them. However, CSS are much more flexible than that. We can assign the same style to all elements belong to a specific class. The following code creates new look for all elements of class example:

    .example {
      background-color: lightgrey;
      font-family: Courier;
      font-size: 85%
      whitespace: pre;
    }
    
    But how we specify that a particular elements belongs to this new class. To do that we can use new property class, which can be used with any tag. The next code has three elements of this class:
    This is <span class=example>my text</span>. And this is <a href="somewhere">a link</a>. This 
    text is <em class=example>emphasized</em>, isn't it? 
    <table class=example>
    ...
    </table>
    
    Thus elements span, em, end table are of the class example, and element a is not.

    We can assign even more complicated conditions combining in the selector tag names and classes. For example, the following style will be applied only to the tables of class big:

    table.big {
      font-size: 125%;
      border: 3px;
      color: darkblue;
    }
    

    And finally, we can assign style rules to a specific element assigning an ID property of the element and setting rules for this ID:

    #menu {
      visibility: hidden;
      position: absolute; 
    }
    ...
    <div id=menu>This is a pop-up menu</div>
    
    ID is one more parameter that can be applied to any tag, but unlike parameter class value id supposed to be unique for each element you assign that property. Property id also allows to access these IDentified elements via JavaScript and change their styles on-fly.

    Font Properties

    Once we learned how we can apply styles we need know what properties we can change. We start with font properties. We would like to focus on the following five properties:
  • font-family
  • font-style
  • font-variant
  • font-weight
  • font-size

    font-family

    This property allows to change the font of the element. It can be assigned either a generic font family:
  • serif (e.g., Times)
  • sans-serif (e.g., Arial or Helvetica)
  • cursive (e.g., Zapf-Chancery)
  • fantasy (e.g., Western)
  • monospace (e.g., Courier)
    or a specific font name, It also can be assigned a list of values separated with commas. If a specific font assignment is made it should be followed by a generic family name in case the first choice is not present. Here is a short example:
    P { font-family: "Letter Gothic MT", Times, fantasy; }
    

    font-style

    This property allows the font to be displayed in one of three ways:
  • normal
  • italic
  • oblique

    font-variant

    The font-variant property determines if the font is to display in normal or small-caps. Small-caps are displayed when all the letters of the word are in capitals with uppercase characters slightly larger than lowercase. Later versions of CSS may support additional variants such as condensed, expanded, small-caps numerals or other custom variants (IE6 does not support these).

    font-weight

    This property assigns weight of the font. It can take one of the following values:
  • normal
  • bold
  • bolder
  • lighter
  • or any integer number between 100 and 900.
    I believe that no matter what number you assign it would be rounded, so that only values 100, 200, 300, 400, 500, 600, 700, 800, and 900 are actually differ. Since not all fonts have all possible weights some values can produce the same result.

    font-size

    This property can take several different types of values. First of all, it can be assigned an absolute size:
  • xx-small
  • x-small
  • small
  • medium
  • large
  • x-large
  • xx-large
    It also can be assigned a relative size:
  • larger
  • smaller
    It can be assigned absolute length using measurement units like pt or px. For example:
    CodeResult
    .the_font { font-size: 1cm; }
    ...
    <span class=the_font>This is this font</span>
    
    This is this font
    And finally, it can be assigned in percents of the current font. For example:
    CodeResult
    .bigger { font-size: 125%; }
    ...
    This is normal font.
    <span class=bigger>This is this bigger font.</span>
    And this is normal font again.
    
    This is normal font. This is this bigger font. And this is normal font again.

    Actually, there is one more property font, but that property doesn't give us any additional options. It only allows to set all the 5 previously mentioned properties in one statement. The syntax of this property is:

    font: [font-style || font-variant || font-weight ]? font-size [ / line-height ]? font-family
    Here is a short example:
     P { font: italic bold 12pt Times, serif; }
    
    In this example we assigned the following values:
  • font-style = italic
  • font-variant not assigned
  • font-weight = bold
  • font-size = 12pt
  • font-family = Times, serif

    Follow this link and you'll find a tool that allow you to experiment with CSS.

    Note: unlike HTML CSS is case sensitive.