Tuesday, 25 October 2011

Introduction To Cascading Style Sheet




1. 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


Ø       Styles Solved a Big Problem

HTML was never intended to contain tags for formatting a document.

HTML was intended to define the content of a document, like:

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers. Development of large web sites, where fonts and color information were added to every single page, became a long and expensive process.

To solve this problem, the World Wide Web Consortium (W3C) created CSS.

In HTML 4.0, all formatting could be removed from the HTML document, and stored in a separate CSS file.

     All browsers support CSS today.

Ø       CSS Saves a Lot of Work!

CSS defines HOW HTML elements are to be displayed.

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!

Syntax

The CSS syntax is made up of three parts: a selector, a property and a value:

selector
{
                    property:value;
}

The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon, and surrounded by curly braces.

Example


<html>
<head>
          <style type=”text/css”>
                   body
                   {
                             color:blue;
                   }
          </style>
</head>
<body>
         Hello World
</body>
</html>


Note: If you want to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a blue text color:

P {text-align:center;color:blue;}

To make the style definitions more readable, you can describe one property on each line, like this:

P
{
      text-align:center;
      color:black;
      font-family:arial;
}

Ø       Grouping

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text color:

Example

<html>
<head>
          <style type=”text/css”>
                   h1,h3
                   {
                             color:green;
                   }
          </style>
</head>
<body>
          <h1>Hello World</h1>
          <h2>Hello World</h2>
          <h3>Hello World</h3>
</body>
</html>






No comments:

Post a Comment