Saturday 7 December 2013

CSS » How To

How To

There is three ways to insert CSS.
  • External style sheet
  • Internal style sheet
  • 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:

» EXAMPLE:


<head>
<link rel="stylesheet" href="style.css" style="text/css" />
</head>

It won't be problem if You type this:

<head>
<link href="style.css" style="text/css" rel="stylesheet" />
</head>

Note: Your file with extension .css mustn't contain any HTML tags. It can be written in any text editor.

» EXAMPLE OF STYLE SHEET :


p {color: red;}
div {width: 200px; height: 60px; background: blue;}
h1 {font-size: 30px;}

Note: Do not add a space between the property value and the unit (such as font-size:20 px). The correct way is: font-size:20px



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:

» EXAMPLE:


<head>
<style>
p {color: red;}
div {width: 200px; height: 60px; background: blue;}
h1 {font-size: 30px;}
</style>
</head>


Inline Styles


To use inline styles You use the style attribute in the relevant tag. The style attribute can contain any CSS property. See example:

» EXAMPLE:


<h1 style="font-size: 20px; color: red;"></h1>



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 p selector:


An external style sheet has these properties for the p selector:

p {
color: yellow; 
font-size: 20px; 
background: blue;
}

An internal style sheet has these properties for the p selector:

p {
color: yellow; 
font-size: 20px; 
background: blue;
}

If the page with the internal style sheet also links to the external style sheet the properties for p will be:

color: yellow; 
font-size: 20px; 
background: blue;

The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet.

No comments:

Post a Comment