CSS syntax

CSS syntax

Let's say we want a nice red color as the background of a webpage:
Using HTML we could have done it like this:

 
 <body bgcolor="#FF0000">
 
 
With CSS the same result can be achieved like this:

 
 body {background-color: #FF0000;}
 
 

Applying CSS to an HTML document

There are three ways you can apply CSS to an HTML document. These methods are all outlined below. We recommend that you focus on the third method i.e. external.

Method 1: In-line (the attribute style)

One way to apply CSS to HTML is by using the HTML attribute style. Building on the above example with the red background color, it can be applied like this:

 <html>
   <head>
  <title>Example</title>
   </head>
   <body style="background-color: #FF0000;">
  <p>This is a red page</p>
   </body>
 </html>
 

Method 2: Internal (the tag style)

Another way is to include the CSS codes using the HTML tag <style>. For example like this:

 <html>
   <head>
  <title>Example</title>
  <style type="text/css">
    body {background-color: #FF0000;}
  </style>
   </head>
   <body>
  <p>This is a red page</p>
   </body>
 </html>
 

Method 3: External (link to a style sheet)

The recommended method is to link to a so-called external style sheet. Throughout this tutorial we will use this method in all our examples.

An external style sheet is simply a text file with the extension .css. Like any other file, you can place the style sheet on your web server or hard disk.
For example, let's say that your style sheet is named style.css and is located in a folder named style. The situation can be illustrated like this:



The trick is to create a link from the HTML document (default.htm) to the style sheet (style.css). Such link can be created with one line of HTML code:

<link rel="stylesheet" type="text/css" href="style/style.css" />
 
Notice how the path to our style sheet is indicated using the attribute href.
The line of code must be inserted in the header section of the HTML code i.e. between the <head> and </head> tags. Like this:
<html>
  <head>
     <title>My document</title>
     <link rel="stylesheet" type="text/css" href="style/style.css" />
  </head>
  <body>
   ...

Comments

Popular posts from this blog

Displaying a flash message after redirect in Codeigniter

What is required for it startup company ?

how to export html data to pdf in angularjs and javascript