(t:CSS Basics:t)

In this tutorial we'll cover some of the basics of CSS.

CSS is an acronym for Cascading Style Sheets. It's a language developed as an extension of HTML for document presentation. For an in-depth look at it's history and development you can check out the Wikipedia entry for CSS.

So let's get to it... Open your text editor and enter this code below:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. </head>
  5. <body>
  6. Hello World!
  7. </body>
  8. </html>
<html> <head> <title>My first CSS web page</title> </head> <body> Hello World! </body> </html>

It's a simple HTML document which should suite our purposes in this tutorial. Save it as "test.html" and open it in your web browser to see the following:

Hello World!


Okay, now we're going to use a CSS method called "inline" to modify the text. Using this method you simply add a "style" property and it's elements to the tag you want modify.

Now add lines #6 and #8 in your code as follows:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. </head>
  5. <body>
  6. <font style="background:red; color:#ffffff;">
  7. Hello World!
  8. </font>
  9. </body>
  10. </html>
<html> <head> <title>My first CSS web page</title> </head> <body> <font style="background:red; color:#ffffff;"> Hello World! </font> </body> </html>

Reload the file in your browser and you should see:

Hello World!


If you look at the "style" property in the <font> tag you'll notice two attributes are set:
"background:red;" and "color:#ffffff;".
This is the simplest method of using style sheets. Again this method is called "inline". There is another method called "internal".

As discussed in the "HTML Basics" tutorial there is a section at the top of the web page called the "header". Within this section you can define styles called "selectors" that will be applied to specific elements in a web page.

Enter ALL of the following code in your editor and save it as "test.html":
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. <style>
  5. body {background:yellow;}
  6. font {background:red; color:white;}
  7. </style>
  8. </head>
  9. <body>
  10. Hello World!<br>
  11. <font>What a lovely day!</font>
  12. </body>
  13. </html>
<html> <head> <title>My first CSS web page</title> <style> body {background:yellow;} font {background:red; color:white;} </style> </head> <body> Hello World!<br> <font>What a lovely day!</font> </body> </html>

Reload the file in your browser and you should see:

Hello World!
What a lovely day!

This is a good way to set default display attributes for your document. But what if you want to utilize other attributes on occasion? One way is to simply use the standard HTML tag properties OR you could define a style "class".

There are a variety of ways you can define a class in a style sheet. We'll be covering the two most commonly used methods in this tutorial.

You can define a class "universally" or as part of an HTML element. Look at the following code which defines both respectively in our style sheet.

Change your code to look like the following:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. <style>
  5. body {background:yellow;}
  6. .red {color:red;}
  7. font.blue {color:blue;}
  8. </style>
  9. </head>
  10. <body>
  11. <center class="red">
  12. Hello World!<br>
  13. <font class="blue">What a lovely day!</font>
  14. </center>
  15. <center class="blue">
  16. This line should be BLACK.<br>
  17. <font class="red">This line should be RED.</font><br>
  18. </center>
  19. </body>
  20. </html>
<html> <head> <title>My first CSS web page</title> <style> body {background:yellow;} .red {color:red;} font.blue {color:blue;} </style> </head> <body> <center class="red"> Hello World!<br> <font class="blue">What a lovely day!</font> </center> <center class="blue"> This line should be BLACK.<br> <font class="red">This line should be RED.</font><br> </center> </body> </html>

Reload the file in your browser and you should see:

Hello World!
What a lovely day!
This line should be BLACK.
This line should be RED.



Inheritance

Styles will inherit attributes from more UNIVERSAL styles unless explicitly defined in there attributes. You can take advantage of this or it can also be a problem.

Add lines #6 and #20 to your code as follows:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. <style>
  5. body {background:yellow;}
  6. font {background:red; color:white;}
  7. .red {color:red;}
  8. font.blue {color:blue;}
  9. </style>
  10. </head>
  11. <body>
  12. <center class="red">
  13. Hello World!<br>
  14. <font class="blue">What a lovely day!</font>
  15. </center>
  16. <center class="blue">
  17. This line should be BLACK.<br>
  18. <font class="red">This line should be RED.</font><br>
  19. <font>This line should be WHITE on RED.</font>
  20. </center>
  21. </body>
  22. </html>
<html> <head> <title>My first CSS web page</title> <style> body {background:yellow;} font {background:red; color:white;} .red {color:red;} font.blue {color:blue;} </style> </head> <body> <center class="red"> Hello World!<br> <font class="blue">What a lovely day!</font> </center> <center class="blue"> This line should be BLACK.<br> <font class="red">This line should be RED.</font><br> <font>This line should be WHITE on RED.</font> </center> </body> </html>

Reload the file in your browser and you should see:

Hello World!
What a lovely day!
This line should be BLACK.
This line should be RED.
This line should be WHITE on RED.


Add line #9 and remove the "class" property in line #18 in your code as follows:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. <style>
  5. body {background:yellow;}
  6. font {background:red; color:white;}
  7. .red {color:red;}
  8. font.blue {color:blue;}
  9. font.red {background:white; color:red;}
  10. </style>
  11. </head>
  12. <body>
  13. <center class="red">
  14. Hello World!<br>
  15. <font class="blue">What a lovely day!</font>
  16. </center>
  17. <center>
  18. This line should be BLACK.<br>
  19. <font class="red">This line should be RED.</font><br>
  20. <font>This line should be WHITE on RED.</font>
  21. </center>
  22. </body>
  23. </html>
<html> <head> <title>My first CSS web page</title> <style> body {background:yellow;} font {background:red; color:white;} .red {color:red;} font.blue {color:blue;} font.red {background:white; color:red;} </style> </head> <body> <center class="red"> Hello World!<br> <font class="blue">What a lovely day!</font> </center> <center> This line should be BLACK.<br> <font class="red">This line should be RED.</font><br> <font>This line should be WHITE on RED.</font> </center> </body> </html>

Reload the file in your browser and you should see:

Hello World!
What a lovely day!
This line should be BLACK.
This line should be RED.
This line should be WHITE on RED.



Line #20 uses the class "red" but also utilizes the HTML <font> tag. Since there is a new class specifically assigned to the font selector in line #9, which sets the background to "white", this is the class that will be used as opposed to the universal class in line #7.


External Style Sheets

Suppose you are writing a number of web pages that implement the same styles as defined between the <style> and </style> tags in our header?

You will need to add this code to each page you create. And every time you change the styles in one page, you have to change them in all the pages. Unless you take advantage of external style sheets.

External style sheets are separate files that contain style definitions. You implement them in your web pages using the HTML tag <link> in your header.

Create a new file in your text editor and call it "styles01.css". In the code above, copy the style elements between the <style> and </style> tags (lines #5 through #9) and paste the content into the new file and save it.

In your web page delete the style sheet in your header (lines #4 through #10) and add the <link> tag with it's corrosponding properties to your header like so:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. <link rel="stylesheet" href="styles01.css" type="text/css">
  5. </head>
  6. <body>
  7. <center class="red">
  8. Hello World!<br>
  9. <font class="blue">What a lovely day!</font>
  10. </center>
  11. <center>
  12. This line should be BLACK.<br>
  13. <font class="red">This line should be RED.</font><br>
  14. <font>This line should be WHITE on RED.</font>
  15. </center>
  16. </body>
  17. </html>
<html> <head> <title>My first CSS web page</title> <link rel="stylesheet" href="styles01.css" type="text/css"> </head> <body> <center class="red"> Hello World!<br> <font class="blue">What a lovely day!</font> </center> <center> This line should be BLACK.<br> <font class="red">This line should be RED.</font><br> <font>This line should be WHITE on RED.</font> </center> </body> </html>

Reload the file in your browser and you should see:

Hello World!
What a lovely day!
This line should be BLACK.
This line should be RED.
This line should be WHITE on RED.



More on Inheritance

As discussed earlier, styles and style sheets inherit the attributes of other styles and style sheets. This applies to inline, internal and external style sheets. Hence the term Cascading Style Sheets (or CSS).

Add lines #5 through #7 and modify line #10 in your code like so:
  1. <html>
  2. <head>
  3. <title>My first CSS web page</title>
  4. <link rel="stylesheet" href="styles.css" type="text/css">
  5. <style>
  6. center {background:lightgrey;}
  7. </style>
  8. </head>
  9. <body>
  10. <center class="red" style="border:2px solid black;">
  11. Hello World!<br>
  12. <font class="blue">What a lovely day!</font>
  13. </center>
  14. <center>
  15. This line should be BLACK.<br>
  16. <font class="red">This line should be RED.</font><br>
  17. <font>This line should be WHITE on RED.</font>
  18. </center>
  19. </body>
  20. </html>
<html> <head> <title>My first CSS web page</title> <link rel="stylesheet" href="styles01.css" type="text/css"> <style> center {background:lightgrey;} </style> </head> <body> <center class="red" style="border:2px solid black;"> Hello World!<br> <font class="blue">What a lovely day!</font> </center> <center> This line should be BLACK.<br> <font class="red">This line should be RED.</font><br> <font>This line should be WHITE on RED.</font> </center> </body> </html>

Reload the file in your browser and you should see:

Hello World!
What a lovely day!
This line should be BLACK.
This line should be RED.
This line should be WHITE on RED.



This covers some basic concepts of CSS. There a quite a large number of attributes and class types you can use to add and modify content. A complete list of these is beyond the scope of this tutorial.

You can find a more comprehensive reference for CSS attributes at w3schools CSS Reference.
You can find a more comprehensive reference for CSS classes at W3C Selectors.
A number of CSS examples are also available at w3schools CSS Examples.