SunQuest
 
       Style Sheets
  Home arrow Style Sheets arrow Page 3 - Customizing styles: User-Controlled Style ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Customizing styles: User-Controlled Style Sheets - Part II
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2005-04-13

    Table of Contents:
  • Customizing styles: User-Controlled Style Sheets - Part II
  • Changing entire style sheets: the first basic approach
  • Listing full source code
  • Summary and further reading

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    Customizing styles: User-Controlled Style Sheets - Part II - Listing full source code


    (Page 3 of 4 )

    As I promised before, if you wish to implement this initial approach in your Web application and add your own improvements, here's the full code for changing style sheets:

    <html>

    <head>

    <title>CHANGING STYLES BY STYLE SHEET</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

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

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

    <script language="javascript">

    // disable accessible style sheet

    var accessibleSheet=document.getElementById('accessible');

    accessibleSheet.disabled=true;

    changeStyle=function(){

         var switcher=document.getElementById('switcher');

         switcher.onclick=function(){

               var defaultSheet=document.getElementById('default');

               if(!defaultSheet){return;}

               // disable default style sheet

               defaultSheet.disabled=true;

               var newSheet=document.getElementById('accessible');

               if(!newSheet){return;}

               // enable accessible style sheet

               newSheet.disabled=false;

               return false;

         }

    }

    // execute function when page is loaded

    window.onload=function(){

         if(document.getElementById){

               changeStyle();

         }

    }

    </script>

    </head>

    <body>

    <h1>Page Name</h1>

    <p><a href="#" title="Change Style" id="switcher">Change style</a></p>

    <div class="content">

    <p>Content for div1 goes here...Content for div1 goes here...Content for div1 goes here...</p>

    </div>

    <div class="content">

    <p>Content for div2 goes here...Content for div2 goes here...Content for div2 goes here...</p>

    </div>

    <div class="content">

    <p>Content for div3 goes here...Content for div3 goes here...Content for div3 goes here...</p>

    </div>

    </body>

    </html>

    Taking a look at the full source code, it's becomes clear that once both style sheets have been defined, the first two lines of JavaScript code deactivate the "accessible" style sheet, making "default.css" the unique active style sheet in the document.

    Then, with a single style sheet to deal with, the "changeStyle()" function is executed after the document has been loaded. Finally, when the user clicks on the switcher link, style sheets are changed gracefully.

    From the previous lines, it's easy to deduce that specifying different rules for the same "content" CSS class applied to the <div> elements, and defined in both style sheets, can achieve a dramatic style change with minimum of effort. With a little bit of JavaScript code, we're switching style sheets on and off. Definitely, we have improved our background about style changes.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · Over this second article, the process to change styles in web documents is taken one...
       · I took the liberty and clean the example up and make it more...
       · Hi Chris,Definitively that was a good addition to the existing changeStyle()...
     

    STYLE SHEETS ARTICLES

    - Creating Gradients for Individual Containers...
    - Creating Gradients for Web Page Headers with...
    - SEO Scrolling Frames Problem Solved
    - Building Cross-Browser Background Effects wi...
    - CSS: Pseudo
    - Using PNG Images to Build Background Effects
    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification
    - CSS: Dimensions
    - CSS: Margins and Padding
    - CSS: Crossing the Border
    - CSS: Text, Fonts, and Tables
    - CSS: Working with Text
    - CSS: Backgrounds
    - CSS for the Newbie







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway