The HTML Learning Corner

Cascading Style Sheets

 Introduction to CSS: Part #1 

Page Index for Part #1
Section #1: Words of Caution
Section #2: What is CSS
Section #3: Style Rule Structure
Section #4: Adding Styles
Section #5: External Style Type
Section #6: Embedded Style Type
Section #7: Inline Style Type

 Section: #1 
 Words of Caution 

[INDEX]

CSS, or cascading style sheets aren't that hard to learn.  What's difficult a lot of times is to get your CSS to implement the same, or nearly the same way in different browsers.  In fact many times it's impossible and you will have to adapt a 'coding down' philosophy when dealing with the Netscape 4.+ browsers and doing things differently for the Opera browser. I find both of these browsers subpar despite claims that Opera supports CSS better than any other browser.  I just have not seen evidence of this and it's support of Javascript stinks!

On the other hand if you are using the Internet Explorer browser you must take care in your coding because the IE browser will let you get away with things that are just not correct, or that work and look differently in all the other browsers.  In other words it will let you get away with bad coding and give you the result you want but that doesn't mean your coding is correct.

Unfortunately the only way to find this out is to check your work in other browsers and many times multiple browsers on the same computer just will not coexist.  If you have a friend that runs a different browser ask them to check it once you have the page(s) uploaded to your web site and to let you know how it looks.

That said let's move on and learn about CSS.



 What is CSS 

 Section: #2 
 CSS = Cascading Style Sheets 

[INDEX]

Here is how the W3C (World Wide Web Consortium) defines CSS.
Cascading Style Sheets (CSS) is a simple mechanism for adding style (e.g. fonts, colors, spacing) to Web documents.

And here is how Eric A. Meyer states it in his CSS Pocket Reference book published by O'Reilly.
Cascading Style Sheets (CSS) is the W3C standard for controlling the visual presentation of web pages.

And here is how I define CSS.
Making stuff on your web page look the way you want it to!

CSS is one of the leading factors in separating the Formating (or structure) of your web page(s) from the Presentation of that web page(s).  Which simply means you are going to use HTML/XHTML to layout your web page(s) and use CSS for what the things on your web page are going to look like!

Layout/formatting = HTML/XHTML
Presentation = CSS

Note: CSS is also one of the things that make up DHTML.  It goes hand in hand with Javascript and HTML/XHTML to make up what's called Dynamic HTML or simply DHTML.



 The Rule Structure in CSS 

 Section: #3 
 CSS Rule Structure 

[INDEX]

Like I said in Section #1 CSS, or cascading style sheets really aren't that hard to learn.

Cascading style sheets are broken down into one or more things called rules that are applied to different page elements.  Sometimes these page elements are a particular HTML tag and sometimes they are what I call a 'catch all' rule, meaning different elements (HTML or XHTML tags) can use the same rule.  These different 'page elements' will determine, or describe how these elements will look and sometimes where they will appear on your web page.

It's okay if you didn't understand the last paragraph just keep reading.

 Selector and Declaration 

The rules I am speaking of in CSS are broken down into two (2) parts.  The selector and the declaration.  The selector is going to be an HTML/XHTML tag, either implied in your coding, or one of those catch all rules I was talking about which are called classes.

 The Property/Value Pairs 

The declaration part of a CSS rule is further broken down into one or more property/value pairs which are applied to the selector.  These property/value pairs are what will give the selector a different look, such as setting a color value, font size, setting the background color, etc.

Confused yet?  Don't be!  A simple graphic will illustrate what I'm talking about.

CSS Rule Structure

In the graphic example I have choosen the paragraph <P> tag as the selector but as you can see you DO NOT use the angle ( < > ) brackets but just the letter P all by itself, (I use lowercase lettering myself but for these examples I have opted to use uppercase for easier readability) .  I have applied to the => P selector a property called color: and a value of #ff0000;, which is the color 'red'.

The color: property pertains to the color of TEXT and nothing else.  So that means everywhere I use the <p> tag in my document the color of any text I use between the opening <p> tag and the closing </p> tag will be in the color red, UNLESS I specify otherwise using another CSS rule.

Inportant Note: Unlike HTML you need to use closing tags or you will end up in trouble.  Like having all your text display in the color red because you left out a closing </p> tag.

 Example 

Here is an inline style example.  I will get to what inline CSS is a little later.

CODE

<p style="color: #ff0000">Learning CSS by Example</p>

RESULT

Learning CSS by Example

 Formatting Your CSS 

Take a look at the image of our CSS rule again.

CSS Rule Structure

Note in the image the formatting for the CSS rule:

P { color: #ff0000; }

This is the formatting you will be using in both the embedded and external style sheet types, which I will get to in a minute.

Now look at what is referred to as an inline style definition.

<p style="color: #ff0000">Learning CSS by Example</p>

As you can see this is called an inline style definition because the style=" " attribute is used, and because it goes right in the line of code along with the <p> tag in your document.

Note: These CSS style rules can, and are, also referred to as style definitons, or style redefinitions, since they are defining, or redefining a web page element, or elements.



 Adding Styles to Your Documents 

 Section: #4 
 Types of CSS 

[INDEX]

There are three (3) types of CSS styles you can use in your HTML/XHTML documents.  In other words three ways you can add CSS to your HTML or XHTML documents.  I have already mentioned all three but will now break them down for you and give you their order of precedence (meaning which will be acted upon and when).

Style Types
  • External
  • Embedded
  • Inline

Note: Your HTML or XHTML document(s) can use inline, embedded, and/or external style types or any combination of all three of these different types of CSS styles.  In the case of the external style sheet your document can call multiple external style sheets if needed.



 The External Style Sheet 

 Section: #5 
 External Style Sheets 

[INDEX]

I will start with the external style sheet.  It has the least amount of precedence of the three types and what this means is that the other two (2) types (embedded and inline) will be acted upon BEFORE these external style sheet definitions depending on what tag is being used, and what property/value pairs are being used.

An external style sheet is just that.  It is a separate file that your HTML or XHTML document will call (or execute) in order to use the style definitions from this external file.

 Rules for External Style Sheets 
  • You do NOT use the <style type="text/css"></style> tags with an external style sheet.  You merely write out your style definitions using the rule structure outlined in Section #3 above.

  • You then Save this external style sheet using the .css extension.  Example: externalstyles1.css.  The actual filename is up to you as long as you use the .css extension.

  • Then use the following line of code to call (or add/execute) the external style sheet from within your HTML or XHTML document.  This line of code would go in the <head> </head> section of your HTML/XHTML document.

    <link rel="stylesheet" type="text/css" href="name_of_external_style_sheet.css">

  • To call multiple external style sheets simply use the same line of code above changing the name of the extenal style sheet for as many external style sheets as you are going to call (use).

    <head>
    <link rel="stylesheet" type="text/css" href="exstyles1.css">
    <link rel="stylesheet" type="text/css" href="exstyles2.css">
    </head>

  • If you code in XHTML make sure you add the closing tag ( space/> ) to the end of the link line.

    <link rel="stylesheet" type="text/css" href="exstyles1.css" />

  • But careful when using multiple external style sheets that you don't have some tag defined more than once.  Your best bet is to keep a print out handy of all the external style sheets you plan to use in your HTM/XHTML documents so you can refer to them if you need to.


 Embedded Styles 

 Section: #6 
 Rules for Embedded Styles 

[INDEX]

The embedded type of CSS style takes precedence over the external style type(s) but takes a back seat to the inline CSS styles (normally, and depending at times on what tag it is used with, and what property/value pair(s) are being used).

Embedded Style Rules
  • Embedded styles go in the <head> </head> section of your HTML/XHTML document.

  • Embedded styles are used with the following template which can/should be inserted into your HTML/XHTML document:

    <head>
    <style type="text/css">
    <!--

    /* Style Definitions go here! */

    -->
    </style>
    </head>


  • Note the use of the /* Comment */ for writting comments both inside the embedded style sheet definitions and when writing a comment in an external style sheet.  DO NOT use the HTML/XHTML commenting convention of:

    <!-- Comment -->

    with either embedded or external style sheets.


 Inline Styles 

 Section: #7 
 Rules for Inline Styles 

[INDEX]

Inline styles normally will have the greatest precedence of the three (3) style types, meaning that the style you use to define a tag 'inline' will be the one that gets used when all is said and none, depending on the tag being used and the name/value pairs.  I have already given you one example of an inline style in Section #3 above but I will give you another here.  But first a couple of rules.

Inline Style Rules
  • The inline styles are placed inside the style=" " attribute of the particular tag you are using in your HTML/XHTML document.

  • They are added right along with the tag (element) using them.

Example:

CODE

<h1 style="font-family: 'bookman old style' serif; font-size: 20pt; color: #ff0000">Learning CSS by Example</h1>

RESULT

Learning CSS by Example

Note the single quotes around the font name 'bookman old style'.  When using a multiple name font you should always enclose the font name with single quotes.

That's going to do it for Part #1 of the CSS tutorial.  Hope you learned something?

Error processing SSI file

LAST            NEXT

Error processing SSI file
Error processing SSI file