views:

273

answers:

4

hi

I have multiple themes on my website and users can just switch between multiple themes by clicking on a javascript link. I have 5 CSS files to handle the layout of all of the themes. 1 is for structure, 3 are for colors etc for different themes, and 1 common for some other stuff.

my css filenames read like this .. main.css, red.css, green.css, black.css, others.css With red, green and black css files are defined as alternate stylesheets.

I have installed YSLOW! and get "This page has 5 external stylesheets. Try combining them into one."

I was just wondering if it is possible to combine most of them into a fewer numbers of CSS files. I know that we can define `

@media screen
  {
  //screen
  }
@media print
  {
  //print 
  }

` sections in a single CSS file. Can something like this be done for multiple CSS files as well?

Thanks for your help.

+3  A: 

You could, instead of swapping stylesheets, combine the stylesheets into one and change the colours with a class on the body instead. This is easier to describe with an example:

Say you currently have

red.css

body { color:red; }

green.css

body { color:green; }

and you swap between them with JavaScript.

Why not change that to:

colors.css

body.red { color:red; }
body.green { color:green; }

and swap the class on the body with JavaScript. Then you only have one CSS file and the JavaScript is much simpler too.

Phil
But I am not just changing text colors .. its about the layout, and color size of other elements as well. For example, main.css .box has rounded corners thing and same box when appyling red.css shows simple box without background images. and so on.
Wbdvlpr
@Wbdvlpr: All your colour styles would be defined using `body.red .style1 { ... }` and `body.green .style1 { ... }` so that changing the body style changed most of the other styles, too. Might bet a bit messy, though.
DisgruntledGoat
DisgruntledGoat is right, I just put a very simple example there.I agree with David Dorward too though, YSlow is just a tool and isn't the law. NickFitz sums it up nicely in saying that that is what alternate stylesheets are for too. My method will allow you to swap entire layouts with just a change of body class, but it has its downsides too.
Phil
+2  A: 

Your main and other can be combined, then rather than attaching the other three, can you not jsut load them with javascript when necessary?

Something like this:

<link rel="stylesheet" href="style1.css" id="stylesheet">

<script type="text/javascript">

function changeStyle() {
    document.getElementById('stylesheet').href = 'style2.css';
}

</script>
ck
how do I do that? you meant server side controlling? page reload?
Wbdvlpr
@wbdvlpr - answer updated with example
ck
I vote for this answer. Don't include all three stylesheets on the page because you're introducing unnecessary overhead for 80% of users who probably won't change the colours.
DisgruntledGoat
+5  A: 

Combine the two common stylesheets, and ignore YSlow for the alternative ones. YSlow is a tool, not the enforcer of the law.

David Dorward
I am not really concerned about the Yslow. I was wondering if the process can be simplified as well.
Wbdvlpr
The "alternate stylesheet" technique was designed precisely for the purpose for which you are using it. I'd say, stick with what you've got - it's the best way :-)
NickFitz
A: 

You could add a class attribute to the body tag and change this by JavaScript. Then you could combine all stylesheet into one.

An interessting tool for writing stylesheets is HSS it helps you to create nested structures.

Hippo