views:

348

answers:

6

I am in charge of providing a theme functionality for a site using a big CSS file (thousands of elements) I've just inherited. Basically we want to allow the user to be able to change the colors on the screen.

Every CSS element, besides color definition also have lots of other attributes - size, font, float, etc... As well a specific color appears in various CSS elements.

If I use the Theme functionality of ASP.NET to have a different CSS file per theme, I have to duplicate my CSS file across all the themes, and it becomes a maintenance nightmare.

Optimally I would like to have a single CSS file (for maintenance) and be able to change the color attributes only.

What are the options here?

+4  A: 

You only have to duplicate the color attributes so if you have

a:hover
{
   text-decoration:none;
   color:Black;
   display:block    
}

in your css file in your theme you only need:

a:hover
{
    color:Red;
}

Now in your page you want to make sure you still reference the original css file, and the browser will merge all the styles.

JoshBerke
+1  A: 

If the differences are minor a possible approach would be CSS cascade rules, e.g:

html:

<body class="dark">
    <a ...>some text</a>
</body>

css:

/* default */
body { color:white }
    a { color:blue }

/* dark theme */
body.dark { color:black }
    .dark a { color:white }
Cristian Libardo
As you said - this is good for minor differences, which unfortunately it is not the current case
LeJeune
+1  A: 

I think keeping one CSS file might be a bad idea, especially since the purpose of themes is to have multiple ones. I suggest having two CSS files used, the base one (what you already use) and the current theme, which get's included/link'd after the base one and overrides what you want different from the base.

TravisO
A: 

Remember: elements can have multiple style classes. Since the layout remains static and will be separate from the coloring, you want to define classes for layout and color separately, in completely different files.

So what you want to do for themes is define a few colors in a separate css file like this:

.DefaultBackgroundColor { background-color: white; }

.PrimaryColor { background-color: #123456; }
.PrimaryAsForeground  { color: #123456; }

.AccentColor { background-color: #654321; }
.AccentAsForeground { color: #654321; }

.ComplementColor { background-color: #333333; }
.ComplementAsForeground { color: #333333; }

.DefaultTextColor { color:black; }
.HighlightTextColor { color:black; font-style:bold; }
.ComplementTextColor { color:white; }

You can add more colors, but you get the idea. Then, in your HTML you add those classes to elements in addition to layout classes. For example, using these classes you might style the Nav buttons for StackOverflow like this:

<div class="nav ComplementTextColor">
    <ul class="primarynav">
         <li class="PrimaryColor"><a href="/questions">Questions</a></li> <!-- selected -->
         <li class="ComplementColor"><a href="/tags">Tags</a></li>
         <li class="ComplementColor"><a href="/users">Users</a></li> 
         <li class="ComplementColor"><a href="/badges">Badges</a></li>
         <li class="ComplementColor"><a href="/unanswered">Unanswered</a></li>
    </ul>
</div>

Obviously those colors are made up and won't work together, but that's the beauty of this set up. This makes it real easy to play with and tweak your colors, since you don't have to define the same color all over the place. You set it up once and then only need to make a very small number of changes to completely re-color the site.

The downside is you are limited. Using the html I posted, for example, you couldn't then go and make the buttons here a different color from everything else just by changing the css file, because we didn't set up 'hook' for it. But you could change that ugly orange color they user here across the board with one simple edit.

Joel Coehoorn
+1  A: 

Like TravisO, I'd suggest using two CSS files... but I would not have the base one be what you already use. I would extract all of the color data from your base one and add it into a new "default" theme.

This way, your positioning details are all in one location, and your themes only have color information. Saves the browser time from downloading and interpreting the default colors in addition to the theme colors.

Illandril
A: 

I'm going to have to go with TravisO's idea on this one.

In this case, staying with one file will actually create a larger maintenance overhead than creating separate css files. What you need is a file for basic css in some location that applies across all themes (root/styles folder for me usually) and then a file in each theme for the coloring information (or any other theme specific css).

knight0323