CSS implementation tutorial Free Web Design Tutorials
Home   HTML CSS JavaScript PHP MySQL Usability Glossary
CSSBasicsImplementing CSSImplementing CSSCSS SyntaxPseudo Classes/ElementsCSS ClassesCSS PropertiesFontColor and BackgroundTextBorderMargins and PaddingSize and Position

Home > CSS > Implementing CSSprinter version

Implementing CSS

CSS can be implemented in three ways that have slightly different effects.

  1. Inline - This is where a style definition is applied directly to a single HTML tag as an attribute.
  2. Using the <style> element - This is where a style declaration is placed within the head of a document.
  3. Using the <link> element - This is where the style declaration is placed within an external style document and is linked to from an HTML document.


Using the Inline Style Attribute

This way of setting style is easy to use but requires that each tag be specified individually using a style attribute. This removes the advantage of being able to set style universally across multiple documents leaving you little better off than if you had used <font>.

More than one style property may be specified within the attribute as shown below with a paragraph element.

Inline Style Example
<p style="color: #ff0000; font-size: 15px;">This paragraph is red</p>


Using the <style> Element

This is a style declaration within the head of a document using the <style> element.

This means that any change to the style declaration will only affect this page and similarly removes the advantage of being able to set style universally across multiple documents - however maintenance is easier than with inline style.

<style> Element Example
<html>
<head>
<style type="text/css">
  p {
    color: #ff0000;
    font-size: 15px;
  }
</style>
</head>
<body>
    <p>This paragraph is red</p>
</body>
</html>


Using the <link> Element

This uses a link within the head section of an HTML document to an external style document. Since the link can be placed on multiple pages then a change to the style document will affect all pages that contain the link.

<link> Element Example
<html>
<head>
    <link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
    <p>This paragraph is red</p>
</body>
</html>


Example Style Sheet "mystyle.css" Linked to by the Above
p {
    color: #ff0000;
    font-size: 15px;
}


The style sheet "mystyle.css" is a plain text file saved with a .css extension instead of .txt and only contains the information that would be placed between the opening and closing <style> tags.

What Cascading Means

This can be quite a complicated issue since multiple style declarations can be made that apply to the same elements - and this causes conflicts.

But assuming that you don't implement multiple external linked style sheets then it can be simplified to that below.

  1. If there are no style declarations then the browser implements its default style.
  2. If there is a style definition then this overrides the browser and becomes the default.
  3. If there is an external linked stylesheet and a <style> element within the head of a document and a conflict occurs then the <style> element becomes the default.
  4. If there is an external linked stylesheet, a <style> element within the head of a document and an inline style declaration and a conflict occurs then the inline style declaration becomes the default.


To summarise: Inline has priority over the <style> element which has priority over the external linked stylesheet which has priority over the browser.

Previous - CSS Basics Previous - CSS Basics     Next - CSS Syntax Next - CSS Syntax


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2009 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2009
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Tips and TricksMenu ButtonsSpecial Effects
Recommended Reading
CSS - the missing manual

The CSS anthology

Beginning CSS web development