How to Specify CSS for a web page

Three ways to specify CSS.

  1. Inline styling:

    You can define one or several CSS declarations and place them directly on an HTML element via the style attribute. These style declarations applies only to that specific html element they are located on.
    <p style="color: sienna; margin-left: 20px;">
      This is a paragraph
    </p>
    This styling says:
    Let the words "This is a paragraph" be in a Sienna color and have a margin of 20 pixels on the left side.
  2. Internal style sheet:

    This is a collection of CSS style rules that is placed inside the html head element of a document via the html style element. These style rules apply only to the page in which they are placed.
    <html>
     <head>
      <style type="text/css">
       h1 { font-size:16pt; color:blue;}
       /* This is my h1 element style */
      </style>
     </head>
     <body>
      <h1>this headline is blue and 16 pt.</h1>
     </body>
    </html>
    With this CSS rule in effect, any HTML code containing an H1 element on the web page is automatically rendered in 16-point type and colored blue.
  3. External style sheet:

    This is a collection of CSS style rules that is placed in a file of its own with a .css file extension, and then linked to web pages via the html link element placed inside the html head element of each page using it. You can define one or more external style sheet files and include those in you HTML-file.
    CSS file:
    h1 { font-size:16pt; color:blue;}
    p {font-variant: small-caps;}
    Html file:
    <html>
     <head>
      <title>Link Element</title>
       <link rel="stylesheet" type="text/css" href="sheet1.css" />
     </head>
     <body>
      <h1>The link element:</h1>
      <p>Links to an external style sheet (or other document type).
      Used in the head element section of the document.
      </p>
     <h1>The type attribute:</h1>
      <p>The Internet content type. (Always "text/css" for a CSS style sheet.)
      </p>
      </body>
    </html>

    With this CSS rules in effect, any HTML code containing an H1 element is automatically rendered in 16-point type and colored blue. Alle P elements are written in small caps character type.
    Instead of using:
    <link rel="stylesheet" type="text/css" href="sheet1.css" />
    you could use the CSS @import rule like this:
    <style type="text/css">
     @import url("sheet1.css");
    </style>

    Note: In CSS 2.1, any @import rules must precede all other rules.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.