Integrating Alternative Stylesheets in WordPress Themes

Published On July 15, 2008
29 Comments Leave a Comment

I am sure that everyone has come across WordPress themes which use alternative stylesheets (CSS) to switch between different sets of style information. For example, you can have a look at the following themes that have this functionality :-

Here is how these themes do it :-

Default Stylesheet

Ideally, every WordPress theme has a style.css file associated with it that contains all the style information for the theme. This default stylesheet is usually called in the header.php file of your WordPress theme by something like this :-

Rel=”stylesheet” is what defines the active stylesheet as the default one.

Alternative Stylesheets

Now you will need alternative stylesheets that change the look or the layout of your theme. For example, you can change the color of the background, fonts, positioning of the sidebar, header images etc. To define the alternate stylesheets, you should use something like rel=”alternate stylesheet” during the stylesheet call. Here are some examples :-

You can make new stylesheet files called orange.css and blue.css files and maybe change the background colors for the body of your theme (for this example). Use something like :-

body {background-color:#FF6E1F;}

Goes in orange.css for orange background color

body {background-color:#2C85EF;}

Goes in blue.css for blue background color

Now, place these files in your theme folder (same path as your style.css).

Style Switcher

The styles in the above alternate stylesheets will be included into your theme but will not be used until it is specified in some way. To do this, we can take help of Javascript code which will serve as a style switcher. You can download the Javascript file here :-

Download styleswitcher.js

Please the styleswitcher.js file into your theme folder and include it into your theme by placing the following code above the tag in your header.php file :-

Now, you need a control scheme that will allow you to switch between the alternative stylesheets. Place the following code anywhere in your theme body :-

Orange
Blue

The above code will make new hyperlinks in your theme called Orange and Blue. When you click on these links, your theme’s background color will change to the specified color.

Conclusion

So you can now experiment and tweak different things in your theme using the alternate stylesheets. You can make any number of such stylesheets but make sure that you call them through the Javascript code. I hope you benefit from this article 🙂

29 replies on “Integrating Alternative Stylesheets in WordPress Themes”

stosun Reply

Hi,
I did what you said above, but still it doesn’t work.
at left top, I put two links to switch styles, but it doesn’t matter.
Could you help me, please?
Regards

conservatories Reply

I would definitely try this. I have just created a fresh blog using wordpress and I am looking out for some new styles and themes. I liked your tutorial and I will give it a shot. Keep up the good job, and keep sharing more tips and advice on Word Press.

Brooke Reply

I spent 6 hours searching and tweaking and thanks to your tutorial i finally got it to work! The only thing though is I cannot get the default style sheet to stick around. It shows up for a few seconds than automatically changes to one of the other styles. This is how I am calling it:

You can see the site at yobeat.server288.com

If you have any suggestions for fixing this, i would be greatly appreciative! Thanks for the great tutorial.

webdesign Reply

I studied the css tutorial and I really learned a lot. I think my next homepage will be a lot better than my first one.

Blay Reply

Thanks for this. Question though, I want to make it automatically switch everytime so one comes to the page. I have 4 different pictures in the header with colour changes in the text. I have done everything you said. Its just how would I go to rotate the css /headers automatically instead of clicking on a button?

Jai Reply

To do the change automatically, you will have to use some PHP code to randomize it whenever someone visits your page. I hope this code can give you some idea :-



Ness Reply

Hi ! Actually, I managed to do it, and it works really fine. I used a js file to get the user’s computer time and to return a stylesheet accordingly. The user can also choose to keep a specific stylesheet (I used cookies), and to toss the cookie to return to the night / day alternance.
Also, for people who don’t have js enabled, I did a php function which does basically the same, but based on the server’s time.
Quite a mess mixing both languages, but now it works ^^ I might put the website url here if somebody’s interested (for the moment, the website is still in development).

Richard Reply

I’ve seen this idea before a few times, usually incorporatd with a cookie that allows you to set a style sheet and when you revisit the site it picks up the cookie data and keeps your selection from last time. Nice touch … if you think you can do it?

Ness Reply

Thanks for this !
I have a question, though : I noticed that even when I don’t choose a specific stylesheet, the cookie “style” is created. I’d like to avoid that (I’m trying to design a two-mode style switcher : as long as the user hasn’t choosen a style, styles are random), but I’m not that much into javascript and I don’t know how to have the cookie created ONLY if the user has chosen a stylesheet.
Any advise ?

John Young | We Have Contact Reply

Very interesting article, and something that I’ve been curious to learn about recently!

One question I would have is: how useful has the community here found alternating stylesheets to be? Do your readers prefer them, or are they more of a novelty?

Nice instructional post!

noobatron Reply

I have been looking for this, since a while but did not know what to search for!

Thanks, I think I have found it:D

Leave a Reply

Your email address will not be published. Required fields are marked *