CSS Style Switcher Tutorial  

Post Categories   Post Time 3 years, 7 months ago

CSS Style Switcher Header

Placing multiple colour or font schemes on your blog is often an excellent addition to your website, and it can help to customise your look and feel. After implementing this on my site, I recieved several requests from people wanting to see how it is done.

I have put together this simple tutorial to show how easy it is to add multiple stylesheets to your blog, and ensure that the user’s selection is stored on their computer so the style remains the same on each page. Please click ‘Read More’, or go to the tutorials page in order to view step by step instructions.

Step 1 - Download the styleswitcher javascript file

Step 2 - Link to the javascript file in your blog’s <head> area

  • Go to the area in your index.php source code, just before the </head> tag
  • Enter <script type="text/javascript" src="styleswitcher.js"></script>
  • This links the functions required for switching the styles to the main index.php page


Step 3 - Create the various stylesheets for each desired theme

  • Make a copy of the main stylesheet (wp-layout.css)
  • In the new file (e.g wp-layout-orange.css) make the required changes to alter the colour of various elements
  • Do this as many times as required (you can always add more later)

Step 4 - Link to the required stylesheets in your header

  • For the main, default stylesheet, enter <link rel="stylesheet" type="text/css" href="wp-layout.css" title="default" /> just after the line you added before. This ensures that this is the stylesheet that loads when the page is opened for the first time.
  • For all the other stylesheets you have created, enter <link rel="alternate stylesheet" type="text/css" href="wp-layout-orange.css" title="orange" />
  • Repeat this for all the other stylesheets you have created

Step 5 - Place the links in your page to allow a visitor to change the style

  • For each stylesheet you have created, just enter a link similar to the following:
  • <a href="/" onclick="setActiveStyleSheet(’orange’); return false;">Orange</a>
  • This will cause the selected stylesheet for the page to change to the one clicked on in the link. It is a good idea to enter a link similar to the following, in case a visitor wants to switch back to the default stylesheet:
  • <a href="/" onclick="setActiveStyleSheet(’default’); return false;">Default</a>

Additional, Optional Code (for PHP users)

Courtesy of Rossz

[PHP]
< ?
$d = dir("style/");
$styles=array();
while (false !== ($entry = $d->read())) {
if ((strpos($entry,’.') !== 0) && ($entry !== “default”)) print “<link rel=’alternate stylesheet’ type=’text/css’ media=’screen’ href=’” . get_settings(’siteurl’) . “/style/$entry/style.css’ />”;
}
$d->close();
?>
[/PHP]
This code (or similar, it may need alteration) would generate a link to each CSS file you had in your stylesheet directory automatically, saving you the hassle of entering each of them by hand. It is very useful if you have numerous stylesheets. You can download a text copy here. It assumes all stylesheets are called ’style.css’ and reside in /baseurl/style/style_sheet_name’. It shouldn’t be too hard to put together some code that generates the activation link for each style, too.

If you have any problems implementing this script, please feel free to contact me. Please note that I take no credit for creating the styleswitcher.js file, it is simply a collection of functions collated from across the Internet. If there are several queries about one particular aspect of the tutorial, I will update it or create a Frequently Asked Questions section.

Databases have always been the backbones of the business whether it comes to using data mining techniques to run your email marketing campaigns or just storing the data. Increasing trend of Distributed Databases caused by broadband internet services is encouraging professionals to take advanced DB courses. Now more of Oracle professionals are attempting 70-431 exams to get certified while MSSQL followers are going for 1z0-040 exam. Most of the web hosting companies like startlogic provide MySQL support, anhosting even provides unlimited MySQL databases. Yet it has failed to challenge the giants like Oracle and Microsoft which are leading the corporate DB market.

Post Tags , ,



21 Responses to “CSS Style Switcher Tutorial”

  1. Bobd Says:

    Ah… for me, for some reason, if i am in a page other than my main site is it sorta screws up.. what it does is it goes back to the main page and it doesnt change the style. Also if go to a page off the main one after the style has been changed it doesnt stay the same… it’s probably something I did but dont know.

  2. Bobd Says:

    Edit: Ok, auctually it stays the same until you click a comment thing

  3. Bobd Says:

    Ok, having one problem.. the theme will not change while viewing a comment page or anything besides the main page. If you have heard of that problem and know how to fix it, please inform me of how.

  4. ilmol = sunset » Blog Archive » Multiple Themes Says:

    […] ctive how users can choose their own taste If you need any further information you can go here . Thanks DavidAppleyard. enjoy clicking squares . . .
    Posted by ilmol in […]

  5. The Life of Me » Theme Changer Thing Says:

    […] tever, click on a link/color and it should change the color of the site Red Blue Default This is how I did it. It’s not working as perfect as I would li […]

  6. XPDude::Illusion » Schemer! LOL Says:

    […] es area. Try clicking on the blue. *Woah* Haha. I did it thanks to David Appleyard’s site. Woot! It rocks. This even saves your settings into a cookie… so, if you like blue, you&#821 […]

  7. John A. Bilicki III Says:

    The cookie on my site (Home / Themes) is not being accepted by all the folders even though the path is set to /. Any suggestions?

  8. oeulfswe Says:

    oeluoploogkew

  9. Belinda R Says:

    Just…….gah THANK YOU. I tried so many different methods to get that stuff to work and only yours did.

    Consider yourself hugged! :)

  10. Born Lippy Says:

    […] 20th 2005, 11:58 am
    Filed under: Cool websites

    I just wanted to say thank you to David Appleyard for explaining how to install the style switcher into […]

  11. hilary Says:

    Thank you so much!

  12. Tomber Says:

    I did a workaround for a problem that occured for me in Opera (maybe all browsers?) while loading the page. He always displays the default theme first (for some ms) and then switchs to the style saved in the cookie.
    Add folowing line in all styles you have in the #wrapper object:
    visibility: hidden;

    and in the styleswitcher.js right after if(a.getAttribute(”title”) == title) a.disabled = false; add:
    if(a.getAttribute(”title”) == “activator”) a.disabled = false;

    create a new .css file, let’s take as an exaple style-activator.css, content:
    #wrapper {visibility: visible;}

    and in the header.php template file add right after the other /style-activator.css” title=”activator” />

    if you are using the php script to display all styles you need to exclude the activator.. you can figure that out on your own :)
    Hope somebody is interested in this small workaround..

    Nice Script David and thanks for your great work!

  13. Tomber Says:

    oh and added the domain to the js cookie creation, if you use www.domain.com and domain.com this could be handy:
    document.cookie = name+”=”+value+expires+”; path=/”;
    to
    document.cookie = name+”=”+value+expires+”; path=/; domain=milchbar.nl;”;

  14. BuyOurWebservices.net » CSS Style Switcher Tutorial Says:

    […] ders that may not “love the warm golden tones”…since I’ve already met one. http://ppleyard.org.uk/index.php?p=69#comments David Appleyard - CSS Style Switcher Tutorial […]

  15. konrad Says:

    Hi,

    I’ve just used your style switcher on www.fruttacotta.co.uk/sparkedit and it works great apart from one thing.

    The first style for every page I open is not the deafult one! It always pickes the second style (larger text).

    Any idea what this could be?

    Thanks,

    Konrad

  16. konrad Says:

    Just figured out what I did wrong - all my stylesheets were alternate sheets, rhater than the default one being just a stylesheet.

    Unfortunately the cookie doesn’t seem to work: when switching to a larger font the browser doesn’t remember this when clicking through to the next page and displays in the default font again.

    Any ideas?

    Ta,

    Konrad

  17. John Says:

    The cookie doesnt seem to work when I choose a different page.. any suggestions?

  18. raptrex Says:

    does this work for wordpress 2.0 and above?

  19. hello piople!!! Says:

    hello piople!!!piople was burn here!!! i am hope.

  20. Abi Says:

    Awesome, man

  21. CSS style switcher « Bryan’s comp4040 blog Says:

    […] ials of using style shee switcher just like Alex has done in his site. Here’s a link http://ppleyard.org.uk/index.php?p=69 Explore posts in the […]

Leave a Reply