A blog by InetSolution about programming, security, design and marketing for banks, credit unions and e-commerce.

CSS Style Switcher: A quick and dirty how-to

Before you begin to start switching CSS styles on your web site, it is very important that you have already developed it using structural XHTML. It is also necessary that you have created at least 1 style sheet, and for the purposes of this tutorial, 1 alternate style sheet.

Let's begin.

There are many ways to attach the style sheet to your web page, but the method we’re going to use here is the linking method. Begin by first linking your default style sheet in-between the <head> tags of your web page:

<head>
<title>Your Web Page Title</title>
<link rel=”stylesheet” type=”text/css” href=”yourdefaultstyle.css” title=”default” />
</head>

Now, you wouldn't be able to switch your style if there was nothing to switch to, so we must specify an alternate style sheet. The process is the same as above, except we change the rel to “alternate stylesheet”:

<head>
<title>Your Web Page Title</title>
<link rel=”stylesheet” type=”text/css” href=”yourdefaultstyle.css” title=”default” />
<link rel=”alternate stylesheet” type=”text/css” href=”youralternatestyle.css” title=”alternate” />
</head>

Making it go.

If everyone used a standards compliant, style sheet aware browser, such as FireFox, we'd be done. Your users would be able to go to View » Page Styles and select the style they liked most. Unfortunately all browsers are not there yet and we must also add support for Internet Explorer, Safari, Netscape, Opera, and others.

The InetSolution web site CSS style switcher uses a script available in an article by Paul Snowden titled Alternative Style: Working With Alternative Style Sheets. Download styleswitcher.js. We recommend that you download the file into a scripts folder within your web site's folder.

Include this script in the head of your web page as well:

<head>
<title>Your Web Page Title</title>
<link rel=”stylesheet” type=”text/css” href=”yourdefaultstyle.css” title=”default” />
<link rel=”alternate stylesheet” type=”text/css” href=”youralternatestyle.css” title=”alternate” />
<script type="text/javascript" src="/scripts/styleswitcher.js"></script>
</head>

The moment you've been waiting for.

Now that we have everything setup, we can finally add our links that make everything happen. Somewhere in the body of your web page, place the following code:

<body>
<a href=”#” onclick=”setActiveStyleSheet(‘default’); return false;”>Change style to default</a>
<a href=”#” onclick=”setActiveStyleSheet(‘alternate’); return false;”>Change style to alternate</a>

</body>

Once the user selects the alternate style, a cookie will be saved on his computer so that the next time he visits your web site he will see the alternate style. Likewise, if he had the alternate style set and choose default, the cookie would be saved with instructions to load the default style sheet.

Taking it further.

To learn more about CSS switching, I recommend the following articles posted on A List Apart:

Download this article in PDF format

CSS Style Switcher: A quick and dirty how-to

Share |

Comments

3/25/2010 6:52:18 AM #

<head>
<title>Your Web Page Title</title>
<link rel=”stylesheet” type=”text/css” href=”yourdefaultstyle.css” title=”default” />
<link rel=”alternate stylesheet” type=”text/css” ref=”youralternatestyle.css” title=”alternate” />
</head>

you forgot an h for the href on line 4, took me a while to figure out why it wasnt working for me.

Is there a chance this might work for 3 css aswell btw?

Roel

Roel

3/26/2010 9:39:41 AM #

Thanks for catching that Roel.

This should work with as many CSS files as you like. You would just add as many links to switch the stylesheet as you needed:

<body>
<a href=”#” onclick=”setActiveStyleSheet(‘default’); return false;”>Change style to default</a>
<a href=”#” onclick=”setActiveStyleSheet(‘alternate’); return false;”>Change style to alternate</a>
<a href=”#” onclick=”setActiveStyleSheet(‘thirdsheet’); return false;”>Change style to 3rd One</a>
</body>

You can see how we used 3 on our website: http://www.inetsolution.com/css-style-switcher.asp

Donovan

Donovan Myes

5/5/2010 2:10:42 AM #

Pingback from endyear2012.com

car insurance cheap quote «  End Year 2012

endyear2012.com

8/13/2010 4:47:42 AM #

Pingback from handycss.com

4 Techniques to Create CSS Style Switcher | Handy CSS

handycss.com

Add comment




biuquote
  • Comment
  • Preview
Loading



Feed Icon Subscribe to our feed


Our Services

Web Design/Development
We are experts in website design and web application development for banks, credit unions and e-commerce businesses.
Secure Hosting for Banks and Credit Unions
InetSolution is known throughout the financial services industry for our secure managed website and application hosting services for banks, credit unions, e-commerce and other security-aware enterprises.
E-mail Marketing for Banks and Credit Unions
We provide a complete email marketing solution for e-commerce sites, banks and credit unions that includes exceptional HTML email design, landing page design and e-mail delivery & reporting software.
MemberProtect® Website Security Component
The strongest and most versatile user authentication, data security and access control component available to .net developers in the banking and finance industry.

Who is InetSolution?

Donovan - Creative Director
Justin - Lead Architect & Developer
Mac - Business Development & Production Chief
Mosh - Programmer
Paul - Technical Sales Architect
Jay - Network Support
Karen - Sales & Client Care
Erin - Business Manager
Jason - Project Director