Cascading Style Sheets - info

What is CSS?

CSS stands for Cascading Style Sheets

  • Styles define how to display HTML elements
  • Styles were added to HTML 4.0 to solve a problem
  • External Style Sheets can save a lot of work
  • External Style Sheets are stored in CSS files

CSS Syntax

A CSS rule has two main parts: a selector, and one or more declarations:

The selector is normally the HTML element you want to style.
Each declaration consists of a property and a value.
The property is the style attribute you want to change.
Each property has a value.

CSS declarations always ends with a semicolon ;and declaration groups are surrounded by curly brackets { }

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.
p {color:red;text-align:center;}

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

h1 {font-family: Georgia, sans-serif;}
p {font-family: Tahoma, serif;}

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


CSS Comments

Comments are used to explain your code, and may help you when you edit the source code at a later date. Comments are ignored by browsers.

A CSS comment begins with "/*", and ends with "*/", like this: /* This is a comment */

Styles are normally saved in external .css files. External style sheets enable you to change the appearance and layout of all the pages in a Web site, just by editing one single file!

The id and class Selectors

In addition to setting a style for a HTML element, CSS allows you to specify your own selectors called "id" and "class".

The id Selector

The id selector is used to specify a style for a single, unique element.
The id selector uses the id attribute of the HTML element, and is defined with a "#".
The style rule below will be applied to the element with id="para1":
#para1 {text-align:center;color:red;}
Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
IDs are similar to classes, except once a specific id has been declared it cannot be used again within the same (X)HTML file.
 I generally use IDs to style the layout elements of a page that will only be needed once, whereas I use classes to style text and such that may be declared multiple times.
ID's are unique -
  • Each element can have only one ID
  • Each page can have only one element with that ID

The class Selector

The class selector is used to specify a style for a group of elements
Unlike the id selector, the class selector is most often used on several elements.
Classes are not unique.
  • You can use the same class on multiple elements.
  • You can use multiple classes on the same element.

This allows you to set a particular style for any HTML elements with the same class.
The class selector uses the HTML class attribute, and is defined with a "." In the example below, all HTML elements with class="center" will be center-aligned: .center {text-align:center;}
.greenboldtext{ font-size: small;  color: #008080; font-weight: bold;}
To put it more simply, this <span class="greenboldtext">sentence</span> you are reading is styled in my CSS file by the following.

You can also specify that only specific HTML elements should be affected by a class. In the example below, all p elements with class="center" will be center-aligned:
<Head> {text-align:center;}</Head>
<body> <p class="center"> </body

Multiple Classes

<p class="applylarge applyred">This is an example of multiple classes.</p>

Three Ways to Insert CSS

There are three ways of inserting a style sheet:
  1. External style sheet
  2. Internal style sheet
  3. Inline style

External Style Sheet

An external style sheet is ideal when the style is applied to many pages.
With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag.
The <link> tag goes inside the head section:
<link rel="stylesheet" type="text/css" href="mystyle.css" />
An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension.
An example of a style sheet file is shown below:   mystyle.css codes are:= {color:lightskyblue;font-weight:bold} {color:darkolivegreen;font-weight:bold}
h1 {color:blue;text-align:center;}
p {font-family:"Times New Roman";font-size:20px;}
#textcenred {text-align:center;color:red;}
hr {color:sienna;}
body {background-color:#F0F8FF;}
body {background-repeat:repeat;}
body {background-attachment:fixed;}
body {background-position:center center; }
a:hover {color:red;}

Internal Style Sheet

An internal style sheet should be used when a single document has a unique style.
You define internal styles in the head section of an HTML page, by using the <style> tag, like this:
<style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
body {background-attachment:fixed;}

Inline Styles

An inline style loses many of the advantages of style sheets by mixing content with presentation.
Use this method sparingly! To use inline styles you use the style attribute in the relevant tag.
The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>  

Multiple Style Sheets

If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. 
For example, an external style sheet has these properties for the h3selector:

Multiple Styles Will Cascade into One
Styles can be specified:
  • inside an HTML element

  • inside the head section of an HTML page
  • in an external CSS file
Tip: Even multiple external style sheets can be referenced inside a single HTML document.  

Cascading order

What style will be used when there is more than one style specified for an HTML element? Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority:
  1. Browser default
  2. External style sheet
  3. Internal style sheet (in the head section)
  4. Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).


Background Color

body {background-color:#b0c4de;} The background color can be specified by:
  • name - a color name, like "red"
  • RGB - an RGB value, like "rgb(255,0,0)"
  • Hex - a hex value, like "#ff0000"
  • aqua, black, blue, fuchsia, gray, green, lime, maroon,
     navy, olive, purple, red, silver, teal, white, and yellow


Background Image

The background-image property specifies an image to use as the background of an element.
By default, the image is repeated so it covers the entire element.
The background image for a page can be set like this:
Background Image - Repeat Horizontally or Vertically. By default, the background-image property repeats an image both horizontally and vertically.

repeated only horizontally (repeat-x)
Body {background-image:url('gradient2.png');background-repeat:repeat-x;}

repeated only vertically (repeat-y)
Body {background-image:url('gradient2.png');background-repeat:repeat-y;}
  Background Image - Set position and no-repeatt

When using a background image, use an image that does not disturb the text. Showing the image only once is specified by the background-repeat property:
body{background-image:url('img_tree.png');background-repeat:no-repeat; background-position:right top;}

Brackground does not move

Enter CSS background coding in this order:-
  1. background-color
  2. background-image
  3. background-repeat
  4. background-attachment
  5. background-position


Elements can have both CLASS and ID applied.
eg     <li id="comment-27299" class="item">

The above information was compiled using information from:- and