Home arrow Style Sheets arrow Page 3 - Customizing styles: User-Controlled Style Sheets - Part II
STYLE SHEETS

Customizing styles: User-Controlled Style Sheets - Part II


In this second article of a three-part series, you will learn how to allow visitors to your website to view an entirely different, more visually accessible style sheet from the "default" one. Normally, providing more than one style sheet causes browser conflict issues -- but with a little bit of JavaScript, we will solve this problem.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
April 13, 2005
TABLE OF CONTENTS:
  1. · Customizing styles: User-Controlled Style Sheets - Part II
  2. · Changing entire style sheets: the first basic approach
  3. · Listing full source code
  4. · Summary and further reading

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Padding Overview
- CSS: Margins Overview
- Top CSS3 Button Tutorials
- More Top CSS3 Tutorials for Animation and Im...
- CSS Mega Menus: Adding Extra Sections
- CSS Mega Menus
- CSS3 Accordian Menu: Horizontally Display Hy...
- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials