Butterfly link to home page
EDT 555 Advanced Web Design: Tips & Tricks

Cascading Style Sheets Topics 

   

Using the class selector with HTML elements

CSS classes can be added to almost any HTML element. Define a class by giving the HTML tag name an extension (any name you want to use) preceded by a period and adding the standard style definition of properties and values inside curly brackets. Then specify this extension in your HTML. The benefit of this is that you can have the same HTML element, but present it differently depending on its class.

Example #1: Let's say you want two special types of paragraphs in your document. You want one to have blue, italic text. Let's give it the class name "blue." You want the other to have red, bold text. Let's give it the class name "red."

1. First define the styles:


     p.blue {color: blue; font-style: italic }
     p.red {color: red; font-weight: bold }


2. Then specify the class attribute in your HTML document:


     <p class="blue"> This paragraph will have blue, italic text. </p>
     <p class="red"> This paragraph will have red, bold text. </p>
 

3. The result is:

This paragraph will have blue, italic text.

This paragraph will have red, bold text.


Example #2: Let's say you want a special table in your Web pages that you can use as a callout. You want it to be aligned right, have a blue background, a top and bottom margin, a prescribed width, and larger white text: (Lets give it the class name "callout.")

1. First define the styles:


     table.callout { width: 220px; margin-right: 10px; background-color:
     #99ccff; border-top: 2px solid #000; border-bottom: 2px solid #000;
     font-family: Verdana; font-size: 115%; color: white; }


2. Then place the table in a division in your HTML document and specify the class attribute:


     <div align="right">
     <table class="callout">
     <tr>
     <td>The early bird may get the worm, but the second mouse gets
      the cheese.</td>
     </tr>
     </table>
     </div>
 

3. The result is:

The early bird may get the worm, but the second mouse gets the cheese.

Example #3: Let's say you want a special type of link with background and font colors that change when the mouse cursor moves over the link, padding, and a border around each link.  Let's give it the class name "mylink."

1. First define the styles. They should appear in the order specified below. If you rearrange
    them your hover effects may stop working, as they will be overridden:


     a.mylink {background-color: #99ccff; font-family: Arial; font-size: 95%;
          color: white; text-decoration: none; padding: 2px 6px 2px 6px;
          border: 1px solid black}
     a.mylink:visited {background-color: #99ccff; font-family: Arial;
          font-size: 95%; color: white; text-decoration: none;
          padding: 2px 6px 2px 6px; border: 1px solid black}
     a.mylink:hover {background-color: #ffcccc; font-family: Arial;
          font-size: 95%; color: blue; text-decoration: none;
          padding: 2px 6px 2px 6px; border: 1px solid black}
     a.mylink:active {background-color: #99ccff; font-family: Arial;
          font-size: 95%; color: white; text-decoration: none;
          padding: 2px 6px 2px 6px; border: 1px solid black}

2. Then specify the class attribute in your HTML document:


     <a class="mylink" href="../edt555tips.htm">HOME</a>
     <a class="mylink" href="js.htm">JAVASCRIPT</a>
     <a class="mylink" href="dw.htm">DREAMWEAVER</a>
 

3. The result is:  HOME JAVASCRIPT DREAMWEAVER

Note: These links, because a class is specified, will not change the properties of
          ordinary links.

New! Visit the Layout Reservoir, a Web site demonstrating multi-column and menu layouts using CSS.

Sample Web pages using a fixed banner and menu: fixedcss.zip | VMenu.zip

Using style tags for mouseover link effects

In the following example, the link and active link are not underlined with the color set to blue. When the mouse is placed over the link, the text color will change to red and the underline appears. Place this code in the <head> section of the html page:

<style type="text/css">
<!--
a:link { color:#0000ff; text-decoration: none; }
a:active { color:#0000ff; text-decoration: none; }
a:hover { color:#ff0000; text-decoration: underline; }
-->
</style>

Demo: U.S. Department of Education

Link an external style sheet to a web page:   basic.css

An HTML page is linked to an external style sheet using the following command in the HEAD section of the page ("mystyle.css" is the name of the external style sheet):

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

Note: Manual HTML formatting overrides formatting applied with CSS styles. For CSS styles to control the formatting of a paragraph, you must remove all manual HTML formatting.

CSS Tricks

Demonstration: Framing photos without using tables

Demonstration: Creating a 3-d link button effect

 

News  HTML  Dreamweaver  FrontPage  Photoshop Elements  Style sheets  JavaScript  Powerpoints  Misc