Style Sheets
  Home arrow Style Sheets arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2005-04-06

    Table of Contents:
  • Customizing Styles: User-Controlled Style Sheets, part 1
  • There's an ID out there: changing styles by ID
  • What's your name? Changing styles by tag name
  • Flexible style changes: keeping it maintainable with CSS
  • The real style switcher

  • 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


    Customizing Styles: User-Controlled Style Sheets, part 1 - Flexible style changes: keeping it maintainable with CSS


    (Page 4 of 5 )

    Based on the above practical example, it's highly desirable and even recommended to add some CSS declarations, in order to take a more realistic approach and be stricter about the needs that eventually might arise for visitors with impaired sight. According to this concept, I'm going to declare two basic classes: "default" and "accessible" respectively. The first one will be applied to the previous <div> elements when the document is loaded, and the second will be assigned to the page elements, when the user clicks on the switcher link. Now, we're getting a bit more realistic. First, let's present the new "changeStyle()" function:

    <script language="javascript">

    changeStyle=function(){

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

    if(!switcher){return;}

    var divs=document.getElementsByTagName('div');

    if(!divs){return;}

    switcher.onclick=function(){

    for(var i=0;i<divs.length;i++){

    divs[i].className='accessible';

    }

    return false;

    }

    }

    window.onload=function(){

    if(document.getElementById){

    changeStyle();

    }

    }

    </script>

    Second, as I stated above, let's declare the CSS classes. They're really simple:

    <style type="text/css">

    .default {

    font: normal 12px "Arial", Helvetica, sans-serif;

    color: #000;

    background: #fff;

    }

    .accessible {

    font: 300% "Arial", Helvetica, sans-serif;

    color: #ff0;

    background: #000;

    }

    </style>

    Finally, let's show the corresponding HTML markup, which is nearly identical to the previous cases:

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

    <div class="default"><p>Content for div 1 goes here...Content for div 1 goes here...</p></div>

    <div class="default"><p>Content for div 2 goes here...Content for div 2 goes here...</p></div>

    <div class="default"><p>Content for div 3 goes here...Content for div 3 goes here...</p></div>

     

    Fine, let's take a deep breath and explain the above code. The "changeStyle()" function closely resembles the original version, with some subtle differences. Since I've declared two CSS classes, once the page is loaded, the script retrieves the already familiar switcher link. When the link is clicked, the function loops over all of the <div> elements in the page, assigning them the new "accessible" class. This changes their styles in a fast and straightforward manner, enlarging the text, and varying the background color and the font size. As you can see, this approach is significantly more flexible, since, by introducing minor modifications in the CSS classes, the style change can become really dramatic. If you think about it, this is one of the biggest advantages of the CSS. Pretty simple, right?

    If you feel that the above example is not complete, you're correct. So I'm going to fix that mistake by listing the full code:

    <html>

    <head>

    <title>CHANGING STYLES BY TAG NAME</title>

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

    <script language="javascript">

    changeStyle=function(){

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

    if(!switcher){return;}

    var divs=document.getElementsByTagName('div');

    if(!divs){return;}

    switcher.onclick=function(){

    for(var i=0;i<divs.length;i++){

    divs[i].className='accessible';

    }

    return false;

    }

    }

    window.onload=function(){

    if(document.getElementById){

    changeStyle();

    }

    }

    </script>

    <style type="text/css">

    .default {

    font: normal 12px "Arial", Helvetica, sans-serif;

    color: #000;

    background: #fff;

    }

    .accessible {

    font: 300% "Arial", Helvetica, sans-serif;

    color: #ff0;

    background: #000;

    }

    </style>

    </head>

    <body>

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

    <div class="default"><p>Content for div 1 goes here...Content for div 1 goes here...</p></div>

    <div class="default"><p>Content for div 2 goes here...Content for div 2 goes here...</p></div>

    <div class="default"><p>Content for div 3 goes here...Content for div 3 goes here...</p></div>

    </body>

    </html>

    Just copy and paste the code into your favorite editor. Save it and load it into your browser and see what happens. It's not going to change the way you see the world, but hopefully it will demonstrate how easy multi-element style changes can be achieved. But, wait a minute! What's the sense of changing the style of page elements if we cannot turn them back to their original state? Let's make one last effort and add that functionality to the JavaScript function.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · For novice or intermediate-level developers, this article introduces in a very...
       · when you can simply change the id of the body and make your CSS dependent on...
       · I agree at least partially with you. While your point of view might be correct on...
       · It is simply bad practise to use it in an obtrusive way. While JavaScript can...
       · Yes, now we're getting to agree. Indeed, the link could be added via the DOM. This...
       · "Yes it makes sense to stop at red lights, but all we wanted to explain people is...
       · Deninitively, the fact in adding a link without using the DOM, is not the same that...
       ·  :Deninitively, the fact in adding a link without using the DOM, is not the same...
       · Well, perhaps a JavaScript dependent link mixes the structure of a document with its...
       · How can you know that when you don't know the articles :-)
       · At least, it's my best hope.Thanks
     

    STYLE SHEETS ARTICLES

    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...
    - Pulling Web Page Elements with the Blueprint...
    - Pushing Web Page Columns with the Blueprint ...
    - Controlling Column Padding with the Blueprin...
    - Prepending Classes in the Blueprint CSS Fram...
    - Appending Grid Units with the Blueprint CSS ...
    - Changing Grid Units in the Blueprint CSS Fra...
    - The Blueprint CSS Framework
    - Building a Liquid Design with Evened Margins...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT