The problem
I need to redesign CSS structure of an existing web application. It supports "branding" — it's got 5 different look-and-feels. Each user has one assigned brand, based on the company they work for.
Currently we have a bunch of complicated CSS files that have long since broken out of control. A typical web page includes 4 style sheets, templating system decides which ones. This means a page reload is needed to switch brands.
A new CSS system should:
- Be based on CSS scripting, preferably LESS, or SaSS.
- Use only one style sheet in the target environment.
- Allow brands to be e̲a̲s̲i̲l̲y̲ switched without page reloading.
My idea
With the help of CSS scripting, define general and brand-based rules:
p {
/* general settings */
}
#brand1 p {
/* include/redefine general settings, add some for brand1 */
}
#brand2 p {
/* include/redefine general settings, add some for brand2 */
}
Create an outer <div>
for the whole body and switch its id
with JavaScript to brand1
, brand2
, etc. This way I don't need to script CSS in any way, just switch the "context" of all elements with one line of JavaScript.
I'm a CSS beginner, so I'd like to avoid going for something totally wrong. Please comment.
Define layout independently, then define all the stuff that has same, let's say shape and overall UX... and then for the final touch use deeper selectors, like this:
Each brand's page's styling should have one more outer selector, for instance id that is connected to outer wrapper of the page, so for starting thing outer wrapper (or the body, altoho I do not recommend this) should be id="default"... default should have no reference in css whatsoever, then every other brand should be selected in css for body to have id="brand1" or "brand2" etc.
On Interaction whic changes the brand you just do this:
What happends is - css rerenders the page accommodating other selectors that are deeper due added one more outer DOM container and thus more important then default ones.
Changing selectors this way gives you the ability to fine tune your page how ever you like, assuming that css3 and any DOM manipulation js engine is in your full expertise :D
I would do it this way for one element:
for more elements (demo):
Create a single, unbranded stylesheet, that defines the general layout of the page, then define brand-specific rules that vary depending on the
class
of the<body>
element, for example:...so you don't need to redefine anything.
Then with a simple script client change the
class
attribute of<body>
to "brandA
" or "brandB
" as appropriate.I advise against using the
id
attribute because as the identity attribute it should be static and unchanging in the document.I do this way:
HTML
CSS
Theming
Now our work would be identifying the themable components. Here, with the base layout, we can theme only the colours and list styles of the unordered list. Lets get those styles alone first. Being a beginner's tutorial, lets concentrate only on the foreground and background colours and not layouts.
Lets name the first class as
.light
and the CSS for the same would be:JavaScript
And now for the code to change, we need to add three links or buttons, which handle the theme change. So, in the HTML, let's add these three links:
HTML
CSS
jQuery
Demo
You can check out the working demo in jsBin.
you can also load the css dynamically (if you want to generate it based on some parameters, for example). do it with this line of code:
another advantage is that the user doesn't need to download all the style sheets of all the brands he doesn't want