范围 CSS 重置

发布于 2024-10-22 01:40:28 字数 219 浏览 10 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

も让我眼熟你 2024-10-29 01:40:28

经过一番谷歌搜索后,我发现了我之前偶然发现的这个项目。这是一个范围内的 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

天煞孤星 2024-10-29 01:40:28

您可以将所有样式内联到通过插件注入 DOM 的 HTML 中。这仍然不能保证,因为如果用户也愿意的话,他们可以用自己的 JS 覆盖它。

如果更方便的话,那么您可以将所有 HTML 包装在一个 ID 中,然后重置其中的所有内容:

#myPluginHTML p, #myPluginHTML li, #myPluginHTML div, etc {
   margin: 0px;
   etc: 0;
}

同样,不保证任何内容。但默认情况下应该可以工作。

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:

#myPluginHTML p, #myPluginHTML li, #myPluginHTML div, etc {
   margin: 0px;
   etc: 0;
}

Again, no guarantee of anything. But should work by default.

羁绊已千年 2024-10-29 01:40:28

没有什么比真正的 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.

木落 2024-10-29 01:40:28

我建议结合以下几点:

  1. 使用大量命名空间/前缀类,即插件检查的plugin-active
  2. 创建一个默认样式表,该样式表将在时为正确的类提供正确的样式没有其他样式应用于该页面。
  3. 对始终相同的 ui 动画使用内联样式(这里没有太多选择,如果是动画,内联样式会发生变化)。
  4. 提供有关哪些类应用于哪些元素以及何时应用于哪些元素的良好文档。

这将允许更高级的开发人员使用他们自己的样式表,或者扩展样式表以形成所需的格式,如果开发人员做了像 div a {float:左;}

您可以在默认样式表中非常具体,为每个元素指定每个规则,但这往往完全没有必要,因为它带来的麻烦大于其价值。

避免制作限制开发的插件,而是尝试制作简化开发的插件。

作为可能受支持也可能不受支持的 HTML5 解决方案(尚不知道哪些浏览器支持此功能),您可以通过 style 元素,带有 scoped 属性(看图,范围样式)。它可能包含一个简化的 HTML 重置,但是当您想要覆盖某些内容并且必须陷入特殊性之争时,整个系统更有可能在以后限制您。

I would suggest a combination of a few things:

  1. use lots of namespaced/prefixed classes, i.e. plugin-checked plugin-active
  2. create a default stylesheet that will give the correct styles to the correct classes when no other styles are applied to the page.
  3. Use inline-styles for ui-animations that are always the same (not a lot of choice in this, if it's animating, inline styles be a-changing).
  4. Provide good documentation on what classes are applied to what elements and when.

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 the scoped 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文