范围 CSS 重置
我有一个 jQuery 插件,它将动态地向页面呈现大量的 HTML。我希望该插件的使用者能够按原样使用它,并且无论他们使用什么 CSS 样式,它看起来都一样。
因此,我需要一个“范围内”的 CSS 重置 - 即在我的插件 div 中说出任何内容的能力不应受到所有者 CSS 的影响。
现在显然我可以重置在此范围内使用的所有 HTML 元素,但它看起来不太优雅。其他人有什么办法处理这个问题吗?
I have a jQuery plugin that is going to dynamically render a decent amount of HTML to the page. I want consumers of the plugin to be able to use it as is and for it to look the same no matter what CSS styles they have in place.
Hence I need a "scoped" CSS reset - i.e. the ability to say anything within my plugins div should not be affected by the owners CSS.
Now obviously I can reset all the HTML elements that I use within this scope but it doesn't seem very elegant. Anyone else go any ways they handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经过一番谷歌搜索后,我发现了我之前偶然发现的这个项目。这是一个范围内的 CSS 重置,完全依赖于 !important 规则。 https://github.com/premasagar/cleanslate
After a fair bit of googling, I found this project that I stumbled upon earlier. It's a scoped CSS reset that relies entirely upon !important rules. https://github.com/premasagar/cleanslate
您可以将所有样式内联到通过插件注入 DOM 的 HTML 中。这仍然不能保证,因为如果用户也愿意的话,他们可以用自己的 JS 覆盖它。
如果更方便的话,那么您可以将所有 HTML 包装在一个 ID 中,然后重置其中的所有内容:
同样,不保证任何内容。但默认情况下应该可以工作。
You could put all the styles inline in the HTML you inject into the DOM via your plugin. That's still no guarantee, as users could over-ride it with their own JS if they desired too.
If it's more for a convenience, then you could wrap all of your HTML in an ID, then reset everything within:
Again, no guarantee of anything. But should work by default.
没有什么比真正的 CSS 重置更重要的了。 CSS 样式规则将级联到它所应用的任何元素,阻止这种情况的唯一方法是用另一个规则覆盖它。
重置样式表的工作原理是覆盖浏览器中默认应用的重要规则。如果您想在某个范围内执行类似的操作,则它不会限于浏览器默认设置的内容,您将必须覆盖页面样式表中可能设置的所有可能的样式。即使如此,您也不能制定如此具体的规则,以至于它们会否决所有可能的选择器。
所以,你所要求的是不可能的。然而,您可以通过覆盖范围最常见和最重要的样式来做出合理的妥协。
There is no thing as a real CSS reset. A CSS style rule will cascade to any element that it applies to, and the only way to stop that is to override it with another rule.
A reset stylesheet works by overriding the important rules that apply by default in the browsers. If you would to do something similar in a scope, it would not be limited to what the browsers set by default, you would have to override every possible style that could ever be set in the style sheet of the page. Even then, you can't make rules that would be so specific that they would overrule every possible selector.
So, what you are asking for is impossible. You can however make a reasonable compromise by overriding the most common and most important styles for the scope.
我建议结合以下几点:
插件检查的plugin-active
这将允许更高级的开发人员使用他们自己的样式表,或者扩展样式表以形成所需的格式,如果开发人员做了像
div a {float:左;}
。您可以在默认样式表中非常具体,为每个元素指定每个规则,但这往往完全没有必要,因为它带来的麻烦大于其价值。
避免制作限制开发的插件,而是尝试制作简化开发的插件。
作为可能受支持也可能不受支持的 HTML5 解决方案(尚不知道哪些浏览器支持此功能),您可以通过
style
元素,带有scoped
属性(看图,范围样式)。它可能包含一个简化的 HTML 重置,但是当您想要覆盖某些内容并且必须陷入特殊性之争时,整个系统更有可能在以后限制您。I would suggest a combination of a few things:
plugin-checked plugin-active
This will allow more advanced developers to use their own stylesheet, or expand on your stylesheet to make the desired format, it will also give developers hooks to override their default styles if they've done something silly like
div a {float:left;}
.You can be very specific in your default stylesheet, specifying every rule for every element, but that tends to be entirely unnecessary as it's more trouble than it's worth.
Avoid making a plugin that restricts development, try instead to make a plugin that simplifies development.
As an HTML5 solution that may or may not be supported (no idea what browsers support this yet), you could add a set of styles to a container via a
style
element with thescoped
attribute (go figure, scoped styles). It could contain a simplified HTML reset, but this whole system is more likely to restrict you later on when you want to override something and have to get into a specificity fight.