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  
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 - What's your name? Changing styles by tag name


    (Page 3 of 5 )

    As I get older, I make fewer efforts to change things. In fact, I'm a little lazy about that now. So, the same principle should be applied to changing styles for page elements. If I really need to affect multiple objects with different styles, accessing elements by their tag name is one of the quickest ways to do it. Accordingly, I've set up a new JavaScript function to perform the style change in a very simple manner. Here's the rewritten version of the "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++){

    var d=divs[i];

    d.style.backgroundColor='#000';

    d.style.color='#ff0';

    d.style.fontSize='300%';

    }

    return false;

    }

    }

    window.onload=function(){

    if(document.getElementById){

    changeStyle();

    }

    }

    </script>

    Finally, here's the list for the HTML code:

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

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

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

    So far, this version of the script isn't very different from our earlier ID-based approach. However, let's see the logic behind the function to understand what it does. First, the script catches the style switcher element, in a similar way to the first ID-based example. Next, it loops over all of the <div> elements present in the document, using the powerful "getElementsByTagName()" method. This way, a collection of <div> objects is generated, to be easily manipulated. With such as useful structure, changing styles is truly simple. When the switcher link is clicked, the event fires up a function that iterates over all of the <div> elements and changes their styles accordingly, specifying background color, font size and font color, respectively. Lastly, as usual, the whole function is executed when the page finishes loading.

    Please notice that the markup is defined only with two <div> containers, which are wrapping up simple paragraphs. However, if I had change my mind about adding many divs to the original HTML, the style change would be immediately applied to them, as soon as the link was clicked again. Here, the power of this technique is clearly evident, since style changes are performed on numerous page elements at the same time.

    At this point, I've played around by changing styles that are assigned inside the JavaScript function itself, allowing little variations to play with, unless the code is specifically modified. Surely, the guys at the maintenance department are complaining loudly. So, it's time to make more room in this place, by adding a few CSS rules to the previous example and making it more flexible and maintainable.

    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

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - 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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek