views:

51

answers:

5

I write a greasemonkey script that adds sticky notes to websites. Because there sometimes are pretty strange style rules used in some websites the sticky notes sometimes turn up messed up (or at least not looking like I want them to look).

Is there a way to say "under this element do not apply any generic stylerules"? So that rules associated with tag names are not applied, but rules associated with certain classes and ids still are. Or does anyone have a better idea on how to ensure that only my styles are applied to the sticky notes?

+1  A: 

AFAIK, that's not really possible.

In stead, you have to make sure you define every style you want manually. Don't rely on defaults. If you select your elements by I'd, they get high priority.

Ikke
Well, that way I'd need to reimplement all HTML elements from scratch, because some pages redefine the basic style even for elements like list items.
panzi
Yes indeed, you will have too. That's the only way you ate sure elements are styled as they should. Iirc, components like lightbox use that same strategy too.
Ikke
A: 

Have you tryed the "!important" keyword?

it overrides the css inheritance hierarchy.

Michele
That can only be applied to styles I explicitly use. I want the defaults to be reset so I don't have to redefine them manually. E.g. on google.com even list items are styled (no bullets, no indentation). And on stackoverflow list items seem to be centered. Basically I'd need to recreate all HTML elements from scratch.
panzi
A: 

This sounds like simply a css specificity issue to me, is it not? It seems like you should just make your css rules more specific, and resilient (don't relay on existing rules, expect defaults to be changed and write the rules that you need).

Here are some links to read up on css specificity:

Erik Vold
A: 

It is indeed a specificity issue. I imagine that you have your sticky notes in a single div#stickyContainer element, in which case you should apply overriding CSS properties to elements by the fact they belong within that element, and ensure that you reset everything.

That is to say, within div#stickContainer div.sticky { ... } make sure that you set every CSS property to what you would like it to be, even if you're just setting them to a typical browser default, and include the !important keyword following each directive.

icio
A: 

You can programmaticly delete generic style rules, but that would effect the whole page.

You might be able to programatically sniff the "generic" rules and then modify the sticky notes' style to counter. If you do that, please post the code. ;-)

But the best approach is probably just to put the sticky notes in iframes. Inside, the styles will be reset to browser defaults. You can then add custom CSS with no side-effects.

Brock Adams
Good idea, except that iframes are the slowest things there are in browsers (especially in firefox). And I'm not sure if it's possible to fill an iframe with content using JavaScript. You cannot do that with windows you opened per JavaScript.
panzi
I don't think your users will notice any speed problems, especially since these iframes will not be loading any outside web pages.You can fill them programmatically; See, perhaps, http://www.dyn-web.com/tutorials/iframes/refs.php ,to start.
Brock Adams