views:

69

answers:

3

We are developing a Html widget using Css as the styling mechanism. The widget needs to be self-contained such that it doesn’t interfere with any other component on the host webpage.

We have a single Css file with a large number of style definitions in it. In order to contextualise them, every single one starts with the name of the main widget container...

#MainWidgetContainer .class1.....
{...}

#MainWidgetContainer .class2.....
{...}

Is there a way I can apply the MainWidgetContainer context in a more global way?

Note – Possibly being picky. I can live with it the way it is but I thought it was worth an ask.

+1  A: 

Here's the documentation on using selectors. You could use child of mainwidget container. It's not the most intuitive thing to pick up, but it's a real timesaver for stuff like this.

Sneakyness
I'm already selecting the "Descendent" of the #MainWidgetContainer. Would using the "direct child" selector eliminate the need to use #MainWidgetContainer in every single selector I write?
Andy McCluggage
There's no way to find out other than to try it :) It's only CSS. Save a backup copy, then start messing around.
Sneakyness
+3  A: 

Take a look at LESS: http://lesscss.org/

Lloyd
I never knew about this. Constant variables would be great just on its own.
DanDan
It's not really a css. It's a DSL to produce CSS.
Lloyd
Hopefully tools like this will help leverage such features into the standard.
DanDan
Me neither. This project has been pretty Css heavy and this could have helped us. Too big a step at this stage but maybe for the next version!
Andy McCluggage
Although it is pretty good, LESS will not help you in this situation. It really just makes the process of writing css quicker.
Sam Murray-Sutton
A: 

In this instance, I think your only real option is to use the #MainWidgetContainer id in all your selectors. Anything more generic than that risks clashing with the host stylesheets or won't be picked up by your styles. As mentioned above, you can use LESS, or SASS to reduce the tedium of writing in this way, but your styles still have to use that selector.

Sam Murray-Sutton
As I suspected, unfortunately. I guess that's why products like LESS have been made. Thanks all.
Andy McCluggage