How to prevent thirty-party css code from damaging my own page style?
My Case condition:
Page’s style can be customized by third-party css code
Page has its own private section which doesn’t allow other code modify its style.
For example:
Page may like this:
<div class=“page”><div class=“other”></div><div class=“.psm”><h2></h2></div></div>
Thirty party code may like this:
.page{ color: red;}
.page h2{color: blue; font-size: 100px;} /* danger: it will influence the .psm h2 which is a private section */
So, how to provide a way to satisfy above two condition? which means letting
thirty-party css code customize all page content but the private section?
You could try counteracting the styles or using inline styles though of course it depends on what actually; you can and cannot edit. I assume you already know about CSS classes.
Hi,
As long as the page div is not an ID and it is left as a class (as you show it) you should be ok by styling your psm div and it’s children.
If your psm div is just found in one spot on each page then you can make it an ID and it will carry more weight than a class.
If all else fails you can apply the !important declaration to your styles, only as a last resort though.
EDIT:
Of course it depends on how the cascade is set up if they are all left as classes. Your stylesheet would need to be last in the cascade of your linked sheets in the head element.
But it doesn’t make sense to style all the PSM. For example, as the following psm html:
<div class=“.psm”>
<div class=“header”><h2>module title</h2></div>
<div class=“content”>
…<!–complex html tags to express the module contents–>
</div>
</div>
So, I can’t explicitly style all the dom inside the psm. for h2, it has its default browser style.
Style psm with ids or !important doesn’t seem to be a good solution.
It can’t be really be done in total because a user stylesheet that use !important will override anything you put in place of the same weight.
You can of course set up your own important rules and hope that by setting ids that your rules will carry more weight and then they will win out. It would be hard for a user to guess what ids you were using but is not foolproof of course.
However unless you set all properties for every element any style you missed would still cascade into your pageif the user had set it in their user stylesheet using !important.
!important was specifically changed in css2.1 to allow user stylesheets to make sure that they get what they want and not what you want I’m afraid.
Yes, I was explicitly talking about “user stylesheets” which will always win out but on third party code you should always be able to win out assuming you can counteract what they have applied.
Third party code (if well written) should normally be self contained and only apply directly to their own code and avoid all global references.
Due to the very nature cascading nature of CSS it is very difficult to stop code cascading into other sections. It would be nice to have an isolate (or protected) feature but I’m sure that would produce as many reverse issues where code that needs to be affected isn’t