The HTML Learning Corner

Cascading Style Sheets

 Introduction to CSS: Part #2 

Page Index for Part #1
Section #1: Element Selectors
Section #2: Contextual Selectors
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 
 Selector Types 

[INDEX]

So far the only selector type I have talked about is the Element type, which uses HTML/XHTML elements (tags) as a selector (see Part #1). This is a fairly easy and straight forward selector type to deal with.

Element selectors can be grouped together and separated with a comma so that all the selectors, (tags in this case), can share the same property/value pairs.

Example

H1, H2, P { color: #000000; }

When used in your HTML or XHTML document all three of the tags (elements) H1, H2, and P will display their text lettering in the color purple which has the Hexadecimal value of: #800080.



 Contextual Selectors 

 Section: #2 
 Contextual Selectors 

[INDEX]

Contextual Selectors are harder to get a handle on and chances are you might never use them. 



 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=" " sttribute 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