Placed in: Home arrow Programming arrow Webdesign arrow Create a better jQuery stylesheet switcher
Create a better jQuery stylesheet switcher

Style Sheet switchers (or "colour theme choosers") are not really that new. Apart from that fact, they still are pretty fun to use and cool to see. I was wondering how jQuery could help me achieve this technique. While searching, I came across several solutions.

There is a problem when using these techniques, which I will explain later. I created a little work-around to create a better jQuery stylesheet switcher. We'll simply change some colours for the user!

jQuery stylesheet switcher

Make sure to check out the demo to view what we're trying to accomplish.

Demo jQuery stylesheet switcher   Download jQuery stylesheet switcher

The cute little monsters used in the demo are created by Fast Icon (Dirceu Veiga). Now, let's take a look at how you can create something like this yourself!

Normal way

First, I'll show how jQuery users normally would change their CSS file.

HTML

This is the trimmed down version of the HTML file. As you can see, there is one style.css placed in the head and there are three links to color changers.

 
<html>
<head>
   <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="colorchanger">
   <a class="colorbox colorblue" href="?theme=blue" title="Blue Theme"></a>
   <a class="colorbox colorgreen" href="?theme=green" title="Green Theme"></a>
   <a class="colorbox colororange" href="?theme=orange" title="Orange Theme"></a>
</div>
</body>
</html>

Nothing really fancy going on here - just the markup that we need for the page.

CSS

Now straight to the important part of the CSS file: The colorchanger. We'll turn the links into block elements.

 
/* COLOR CHANGER */
#colorchanger { float:right; }
.colorbox { width:20px; height:20px; border:1px solid #050505; float:left; margin:5px; cursor:pointer; display:block; }
.colorblue { background-color:#bfe1f1; }
.colorblue:hover { background-color:#90bcd0; }
.colororange { background-color:#F69C3A; }
.colororange:hover { background-color:#FF5C01; }
.colorgreen { background-color:#78A848; }
.colorgreen:hover { background-color:#189048; }

As you can see, the boxes each have their own color and hover effect. We'll now use jQuery to actually change the stylesheet when the user clicks on one of these links.

jQuery

After loading jQuery, we can now use it's power to change the link-element in the HTML-head (the place where we defined the first CSS sheet: style.css).

 
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
   // Color changer
   $(".colorblue").click(function(){
      $("link").attr("href", "blue.css");
      return false;
   });
   
   $(".colorgreen").click(function(){
      $("link").attr("href", "green.css");
      return false;
   });
   
   $(".colororange").click(function(){
      $("link").attr("href", "orange.css");
      return false;
   });
 
});

This works great! Every time the user clicks on one link, the stylesheet gets replaced. Now the only thing we have to do, is define how the CSS should look like when switching colours.

The problem

There are two problems when looking at this solution: One small one, and a bigger one. The small one, is that the whole style.css gets replaced with the new CSS file (for example: blue.css). Since this new CSS file needs to be downloaded and parsed by the browser, the user will see a little flickering; A split second of the "naked" HTML file (no CSS).

But there is a bigger problem when using this solution. The whole style.css gets replaced, which means that all your CSS definitions will get replaced too. Everything you already set for body, #wrapper, #sidebar (like fonts, floats and sizes) in style.css will be gone as well.

"But you can copy the whole style.css content to the other style sheet files and change the colours, right?" That's absolutely true. But, what will happen if one style sheet would change? You would have to change all other CSS files which will cause loads of work. Also, you're not really saving bandwith with almost duplicate CSS.

The solution

I found a solution for this problem. The key lies in the fact that there are two ways of adding CSS to an HTML page. The normal way, is by using link rel='stylesheet'. The other way is using the style element.

We're going to use this difference to our advantage. We'll create one base CSS file (style.css) where we're going to place all CSS definitions except for the colours. Each colour gets his own CSS file that we're going to switch using jQuery.

HTML

We'll change the HTML a little bit to make this technique work.

 
<html>
<head>
   <style type="text/css">@import url("style.css");</style>
   <link href="green.css" rel="stylesheet" type="text/css" />
</head>
<body>
   <!-- Body contents here -->
</body>
</html>

Now, when we use the color changer, only the green.css file gets replaced by the other CSS file. This is how they can look like:

style.css (sizes, fonts etc.)

 
h2 a { display: block; margin: 0 0 30px 0; font: italic 45px Georgia, Times, Serif;
        text-align: center; }
#wrapper { width:940px; margin:40px auto; background-repeat:no-repeat; }

green.css (colours only)

 
h2 a { color: #78A848; }
h2 a:hover { color: #189048; }
#wrapper { background-image:url("images/monster_green.png"); }

That's all! Now, when the user changes the CSS files, only the colours get changed. Also, you can easily change the style.css file without having to make the same change in all other CSS files.

Conclusion and Download

Now we also need to set a cookie to save any colour changes the user made, so the next time the proper CSS gets loaded right from the start. Other than this, I think this is a pretty nifty way of changing CSS files

Demo jQuery stylesheet switcher   Download jQuery stylesheet switcher

What do you think? Would you do it any other way, or are you going to use it in your next project?


Tags:  css stylesheet switcher jquery webdevelopment

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
Kyle Kinnaman - add an ID rather than use @import   2009-09-28 20:31:30
Gravatar image You can also add an ID or class to the style to be switched and forgo using @import to load the initial css.

header:
Quote:
link href='style.css' rel='stylesheet' type='text/css'
link id="css_color" href='green.css' rel='stylesheet' type='text/css'


js:
Quote:
// Color changer

$(".colorblue " ).click(function(){

$("#css_color " ).attr("href", "blue.css " );

return false;

});

It works in Firefox - I didn't test other browsers.
Marco - Whoah - thanks!   2009-09-28 20:33:12
Gravatar image Thanks Kyle! I tested that before, but for some reason, it didn't work for me. That's why I created the work-around with @import.

I must have done something wrong, thank you for checking it out! Also, I formatted your comment a bit.

Big thanks for helping me (and the community)!
Jack - Perfect!   2009-09-30 11:44:14
Gravatar image Ah, this is ideal and perfectly timed! Literally just working on a site that I was about to add a colour style switcher to and happened to skim through my feeds and there this post was!

Unfortunately I am using a CMS which requires a lot of different css files for different things so this initially just blanked the page - however, Kyle Kinnaman fix there works perfectly so this is absolutely ideal.

Thanks for the guide, files and solutions!
Janko   2009-09-28 21:38:54
Gravatar image Great example, Marco! I really like the demo :)
webmasterdubai - webmasterdubai   2009-09-28 21:47:23
Gravatar image nice work and good way to use style sheet switcher.
Bogdan Pop   2009-09-29 08:52:21
Gravatar image Wouldn't it be simpler to add the @import url("style.css " ); into the color css files?

Load by default green.css which has @import url("style.css " ); on the first line. Then using jQuery you switch the style to blue.css which also contains @import url("style.css " ); on the first line, or whatever. Therefor, you make all changes into style.css and keep the colors in their specific css file.
Marco - Would work   2009-09-29 10:19:13
Gravatar image That would work indeed, but there is a downside to this solution. Since the browser would fully have to parse the "blue.css", the user would also see a slight "flickering" of the page (CSS-less page).
Dan - Why is this happening   2009-10-10 08:18:36
Gravatar image When I tested this on my site, the following line is being added to my HEAD (by doing a simple inspection with Firebug):



This seems to be overwriting by other style sheet references, rewriting all hrefs of all style sheets.

Example Before:



Example After:




Any ideas?
Dan - whoops - redo   2009-10-10 08:24:32
Gravatar image script.js adding line:
link rel="stylesheet" type="text/css" href="css/green.css" media="all"

Before:
link href="css/mystyle.css" type="text/css" rel="stylesheet"
link href="css/green.css" type="text/css" rel="stylesheet"

After:
link href="css/green.css" type="text/css" rel="stylesheet"
link href="css/green.css" type="text/css" rel="stylesheet"
link rel="stylesheet" type="text/css" href="css/green.css" media="all"
Dan - Sorry   2009-10-10 08:29:07
Gravatar image Sorry. just saw Kyle Kinnaman's firs post. doh!
Anthony Fryer - sorted one of my problems, can you help with the n   2009-10-21 15:15:50
Gravatar image The idea to get rid of the flicker of unstyled content works a treat for a site I am currently working on. So you may also know the answer to my second problem?

I have a color changer and a font re-sizer. If they choose a different font then the font increases or decreases, if they choose a color background it changes background and foreground color, however it loses any font size preference?

How could I get it to work so I can choose both a different background color and a different font size from different stylesheets at the same time?

Any clues?
Jehu - not persistent   2009-10-21 16:39:04
Gravatar image First: Thank you for this tutorial.

You say no word about persistence. On demo-page the links have href="#", so a click has no impact. But if you make a page reload, the default style is shown again.

The choosen style should be saved in cookie by javascript [1] to make it work on real sites.

Regards
Jehu

[1] see http://www.quirksmode.org/js/cookies.html
Marco - Below   2009-10-21 20:02:13
Gravatar image Hi Jehu! Thanks a lot for your comment.

I did mention setting the cookie - right below "conlusion and download" ;) . This was just to show how to make a stylesheet switcher, but it's not fully implemented to make it usable. I hope you understand :) .
Kris Brooks - Help with Cookies   2012-01-13 01:04:05
Gravatar image I am new to using and setting cookies could u provide set based on ur code ?
Rick   2009-10-28 15:21:01
Gravatar image This is a pretty niffy idea. One thing that bugs me is when you change the style it has to load a different background image there is some lag. A way to get rid of this is to creat a span with that img and change the position of the image for the different styles. B)
Ramil - Don't use @import   2009-11-05 09:27:59
Gravatar image I'm looking for a good jQuery style switcher, found yours but seems there's a problem:
1.) non-persistent - no cookie
2.) don't use @import - http://stevesouders.com/blog/2009/04/09/dont-use-import/
wespai   2009-12-25 10:30:13
Gravatar image thx collect it to ajax.wespai.com
Dennis   2010-01-20 09:07:50
Gravatar image Very nice!

I am pulling css specifics from a DB and want to open my page with the css values from that entry. (logo position, font,color).

Have you seen any examples ?

Thanks
Ege - why not use classes?   2010-01-20 16:13:19
Gravatar image you can define some rules simply according to your body tag's class:

(ie )

Code:
.blue a:link {
color:blue;
}


and

Code:
.orange a:link {
color: orange;
}


and

Code:
$(".switcher";).click(function() {
$("body";).toggleclass("blue orange";);
});


any downsides of this?
Andy Freeman - Almost there with DB solution   2010-02-04 12:18:07
Gravatar image Ok am pretty much there with my working example of storing the colour.css in a DB then loading the page with the value from the DB.

One bit that is really stumping me is this

When i run it works fine

Code:
    
$(document).ready(function() {

$("#css_colour";).attr("href", 'CSS/Themes/red.css');

});


When I run it via a variable it does not work

$(document).ready(function() {

var MyClientSideVariable = 'CSS/Themes/red.css';
$("#css_colour";).attr("href", MyClientSideVariable);

});

I am very new to jQuery/JScript so would really appreciate if anyone can assis me with fixing this.

If i can resolve this I will be delighted, as i know i can set the MyClientSideVariable = thestoredDBvalue

Many Thanks
Andy
Curtis - Slight Edit   2010-03-22 12:45:40
Gravatar image I found this quite useful so thought I would post my little edit to it.

First, I add in the default theme with an ID. (as per the first post by Kyle so I don't get my normal style sheets removed)
Code:




Then I add my Theme switch area with IDs for each theme.
Code:


Theme 1
Theme 2
Theme 3



And then rather than add a separate script for each theme, I just have one which grabs the id of whatever is clicked and inserts it into the link.
Code:

$("ul#themes li a";).click(function(){
var theme = $(this).attr("id";);
$("link#themeswitch";).attr("href", "" + theme + ".css";);
return false;
});
Curtis - re: Slight Edit   2010-03-22 12:48:14
Gravatar image Well, that didn't work. Even with the pre and code tags, it still stripped it all out.
Aman - Cool Plugin   2010-08-18 16:47:03
Gravatar image Hi

I like this plugin.I have Also 10+ Jquery Style Switchers plugins please see:-

http://jquery13.blogspot.com/2010/08/10-jquery-style-switchers.html


Thanks
Aman
jagadish - thanks   2010-10-03 11:59:54
Gravatar image Hi

Thanks for the code.

It work perfect.
Lucas - Expand?   2010-10-06 23:32:07
Gravatar image Ok I can get your way to work, but I needed to attach more than one CSS (CMS as well) and so I tried Kyle's workaround and I cant get it to work. Is there an example page you could point me to? What I'm trying is this
Head
Code:



CSS Style Sheet Switcher







body
Code:






and JS
Code:
google.load("jquery", "1.3.1";);
google.setOnLoadCallback(function()
{
// Apply some CSS3 to keep the CSS file CSS 2.1 valid
$("h1";).css("text-shadow", "0px 2px 6px #000";);
$("h2 a";).css("text-shadow", "0px 2px 6px #000";);
// Color changer

$(".colorblue";).click(function(){

$("#css_color";).attr("href","blue.css";);

return false;

});


$(".colorgreen";).click(function(){

$("#css_color";).attr("href","green.css";);

return false;

});


$(".colororange";).click(function(){

$("#css_color";).attr("href", "orange.css";);

return false;

});




Did I miss something?? Please help!
Lucas   2010-10-06 23:35:32
Gravatar image Apparently the code brackets don't work? Anyway,

Head
Quote:

CSS Style Sheet Switcher







Body
Quote:






And JS
Quote:
google.load("jquery", "1.3.1";);
google.setOnLoadCallback(function()
{
// Apply some CSS3 to keep the CSS file CSS 2.1 valid
$("h1";).css("text-shadow", "0px 2px 6px #000";);
$("h2 a";).css("text-shadow", "0px 2px 6px #000";);
// Color changer

$(".colorblue";).click(function(){

$("#css_color";).attr("href","blue.css";);

return false;

});


$(".colorgreen";).click(function(){

$("#css_color";).attr("href","green.css";);

return false;

});




$(".colororange";).click(function(){

$("#css_color";).attr("href", "orange.css";);

return false;

});

online diabetes pills guide - online diabetes pills guide   2011-06-15 05:50:03
Gravatar image The venues really seem great!!! The official rooms, dining, tents and international accommodations are amazing. I have bookmarked this webpage, it is really worthy.
diabetes pills hub - diabetes pills hub   2011-06-15 05:50:30
Gravatar image The posts are pretty good; it is useful to every browser. Thanks for making!!!
online anti smoking pills - online anti smoking pills   2011-06-15 05:52:06
Gravatar image The article is inspiring; it is made possible when the words come true. Guys please post comments in this web page. This web page is really worthy!!!
online anti smoking pills   2011-06-15 05:52:22
Gravatar image It was very great for me to go through this blog as this site was very informative and I had really liked it very much.
online pharmacy listing clinic - online pharmacy listing clinic   2011-06-15 05:53:43
Gravatar image I had found this site to be very newsworthy. I really appreciate the work which was involved in this blog. Thanks a lot for the informative site.
retail drug store guide - retail drug store guide   2011-06-15 05:55:26
Gravatar image I was searching for this kind of information and I had got it in this blog. Thanks a lot for the useful information which you had provided in this blog.
clinic pharmacy info - clinic pharmacy info   2011-06-15 05:55:57
Gravatar image Generally, I only read this kind of information which will be very useful for me. This site was really a nice site which had a lot of stuff in it.
online secure weightloss deals - online secure weightloss deals   2011-06-15 05:56:32
Gravatar image I had learned a knowledgeable aspect in this site. Really I thank the website which had made me to know some interesting topic. Thanks a lot!
online birth control pills gui - online birth control pills guide   2011-06-15 05:57:04
Gravatar image This was indeed a great blog and I had really liked it very much. I could also found it to be very interesting thanks a lot for the informative blog.
prescription birth control pil - prescription birth control pills tips   2011-06-15 05:57:38
Gravatar image This blog was quite interesting and I liked it very much. The information in this blog was really very nice and it was very useful for me. Thanks a lot!
prescription birth control pil - generic pharmacy guide   2011-06-15 05:58:24
Gravatar image It is great to see the informative stuff in this blog. I had really liked it very much. Thanks a lot for the lovely site. I found it very interesting.
drugs pharmacy journal - drugs pharmacy journal   2011-06-15 05:59:06
Gravatar image This site was really very newsy and I found it to be very cordial. Thanks a lot for the information which you had provided in this blog. I liked it!
cheap safe online pharmacy - cheap safe online pharmacy   2011-06-15 05:59:31
Gravatar image The stuff in this blog was really very nice and I had liked it very much. Thanks for the informative stuff and it was very useful for me.
weight loss sideeffects guide - weight loss sideeffects guide   2011-06-15 06:00:01
Gravatar image The information in this blog was very interesting and it was very useful for me. I had liked the stuff very much. Thanks a lot!
anti smoking pills information - anti smoking pills information   2011-06-15 06:00:37
Gravatar image The website has simple and easy way of explain the people through articles which are so easy to understand, I was so pleased and suggest others also to visit the site once and get useful material from them.
prescription birth control pil - prescription birth control pills guide   2011-06-15 06:01:11
Gravatar image Once go through this site, which is very interesting and helpful. I assure you people it is one of the best site I ever had gone through that has good information about what you are looking for.
hirmani80 - WebHostingPad   2011-08-02 07:45:58
Gravatar image Nice impression by providing these articles in this blog. This website is providing the some different info in this blog. Thanks a lot for using the great technology in this blog. I had really like it very much for providing the lot info
Jay   2011-08-18 09:48:50
Gravatar image :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry: :angry:
Kris brooks - questoin   2012-01-13 00:01:11
Gravatar image I used this and i love it but i am unsure of how to set cookies for it to work correctly could anyone help me
Anonymous   2012-03-13 17:59:19
Gravatar image Very good
Trail   2012-04-24 22:42:20
Gravatar image [color=blue]Thanks for this information. Very informative.
Thanks for the alternate suggestion as well Kyle.

I have this switcher working the way I want, but I too am unable to get a cookie to work.

Obviously, the cookie should keep the selected preferences across the entire website. I have worked on this and tried several methods.

I would be grateful if someone would provide the code how to do this, perhaps even an article (hope that's not asking for too much). I've looked at PPK's site and several others, but the truth is I just don't understand it. I've read about cookies in a couple of books, but I am missing something. I've spent a lot of time on this, but I'm unable to make it work.

The left side of brain seems to be in hibernation.

Any assistance would be appreciated.[/color]
Curtis   2012-04-25 08:20:15
Gravatar image Hey Trail, just use the jquery cookie plugin. It's rather easy to use.
https://github.com/carhartl/jquery-cookie

On change of stylesheet, set the value of your cookie accordingly. (ie: 'blue.css')
Then on pageload, check that cookie exists and if so, grab the value and put it into a var. Then use that var to set the stylesheet.

ie:
Code:

// this will grab the value from the stylesheet cookie ('blue.css' for example)
var style = $.cookie('stylesheet');

// then use that var to set the stylesheet
$("link";).attr("href", style);
Trail - Still not working   2012-04-29 15:42:01
Gravatar image :(

Curtis, thank you for taking the time to respond to my question.

A couple more questions:

I have downloaded the cookie plugin and set a link to the plugin.

1 - do I create another .js file and add your code above? or does it go in the (what I am calling a switch.js file which contains the code from this article)?

2 - question on the specific code
var style = $.cookie ('stylesheet')
where style is the file name? aka blue.css

var style = $.cookie ('blue.css') or as written

var style = $.cookie ('stylesheet')

3 - $("link";).attr("href", style);

4 - This is what I have for the cookie. I have it in a separate file named cookie.js.

//theme cookie name
//base is a base css file to start
//expires in 14 days
// '/' for the entire domain
//then I have the other files similar to this article: blue.css, //green.css, red.css

// Creating cookie
$.cookie('theme', 'base', {expires: 14, path: '/'});

// Get a cookie
$.cookie('theme');

// Delete a cookie
$.cookie('theme', null);

I'm just not able to get a jQuery cookie set to make this work across a domain. I think I understand the basics, but I guess I need more specifics in order to understand the process.

Thanks for you help!
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio