w3reference home
CSS Tutorial


Bookmark and Share

How to use CSS

Till now, we have learnt what is CSS and how it is used. Placing a CSS in a document is equally important. A CSS has to be placed according to it's use. There are three ways of inserting a style sheet. They are as follows:
  • Inline Styles
  • Internal Style Sheets
  • External Style Sheets

Inline Styles

This is the most basic way of inserting a style sheet. It is used when the style is to be applied to only one occurrence of the HTML element. Let's see this with the help of an example.
<HTML>
<HEAD>
</HEAD>

<BODY>
<b style="font-family:arial; color:blue">This is a bold text with arial font and blue color</b>
<p style="font-family:arial; color:blue">This is a paragraph with arial font and blue color</p>
</BODY>
</HTML>
The output of the above code is given below:
This is a bold text with arial font and blue color

This is a paragraph with arial font and blue color

The inline styles loses most of its advantages of style sheets because it requires styles to be defined for each HTML element every time it is used. If you need to make a change in a certain style, it would require making changes to each and every occurrence of the HTML element. Inline style should be used only when the specific style is to be used only once in the entire document.

Internal Style Sheets

Internal style sheets are used to define a style for an HTML element for a single document. This style can be used anywhere in the entire document. The internal styles are defined in the head section by using the <style> tag. Look at the example given below:
<HTML>
<HEAD>
<style type="text/css">
B {font-family:arial; color:blue}
</style>
</HEAD>

<BODY>
<b>This is a bold text with arial font and blue color</b>
</BODY>
</HTML>
The output of the above code will be:
This is a bold text with arial font and blue color
If you want to change the style so that the text is underlined as well, you only need to edit the style at one place in the document. The changes in the entire document will show automatically.

If you are using an old browser that does not support CSS, the <style> tag will be ignored but the content of the <style> tag will be displayed on the page. To prevent this, it's better to place the style content within HTML comments as follows:

<HTML>
<HEAD>
<style type="text/css">
<!--
B {font-family:arial; color:blue}
-->
</style>
</HEAD>

<BODY>
<b>This is a bold text with arial font and blue color</b>
</BODY>
</HTML>

External Style Sheets

Suppose you need to define a style that is to be used in your entire site, external style sheet is the solution. Following are the steps for using external style sheets.
  • Create a CSS file that has extention .css.
  • This file contains the style definition and each document will be linked to this style sheet by using the tag.
  • The CSS file contains only the style definition and no other tags.
  • The tag has to be mentioned in the head section.
Below is a css file which we will later use in an HTML document. The name of this css file is example.css. Remember the CSS file should have the extention .css.
body {font-size: 36pt}
b.blue {color: blue}
Now, we will use example.css in an HTML document.
<html>
<head>
<link rel="stylesheet" type="text/css"
href="example.css"/>
</head>

<body>
<b class="blue">This text is bold, blue colored & 36pt</b>
</body>
</html>
The output of the above code will be:
This text is bold, blue colored & 36pt
Here, we have referenced the CSS file, example.css. The entire text in the document will have the attributes defined for the tag apart from the other attributes specified for individual elements.
Code Validator
Learn FTP
Color finder
Link Checker
Free web designs
Coming soon!
Interview Questions...
'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.