防止由 firefox 插件创建的 html 元素的样式被站点样式表覆盖
我从我的 firefox 插件中将一些 html + css 注入到每个页面中。但这种样式有时会被样式表覆盖。我想阻止这种行为。
我知道这可以通过一些 css 技巧来解决。例如添加 !important
。但是 Firefox 或 xul 有没有办法可以轻松做到这一点?
I am injecting some html + css into every page from my firefox plugin. But this styles is sometimes getting overridden by the style sheet. I want to stop this behavior.
I know this can be solved by some css tricks. Adding !important
for instance. But is there a way available in firefox or xul to do this easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单独使用
!important
是不够的,网页可以执行相同的操作,并且仍然覆盖您的样式。您可以使用样式表服务来注册用户样式表。如果您随后使用!important
那么网页将无法再覆盖您的样式。请注意,用户样式表始终是全局的,适用于所有网页以及浏览器自己的 XUL 文档。您可以使用 @-moz-document 将它们限制为特定网页。
Using
!important
alone is not enough, the webpage could do the same and still override your styls. You can use the Stylesheet Service to register a user stylesheet. If you then use!important
then web pages will no longer be able to override your styles.Note that user stylesheets are always global and apply to all webpages as well as browser's own XUL documents. You can restrict them to particular webpages using @-moz-document.
不是最好的解决方案,但正确...
Not the best solution but right...
对我来说,在这种情况下您肯定会使用
!important
。也就是说,您可以尝试使用特异性来确保您的样式得到设置。
因此,请确保您的目标元素尽可能精确,以便覆盖那些不那么具体的样式。
例如,如果您有此结构,则
网站的样式可能会针对您的目标,
您应该致力于做到这一点
,这是更具体的,并且会覆盖上述样式。
您可以做的另一件事是在
之前
附加
您的样式,这将使它们出现在级联中的最后,并且,如果它们相等,将被应用。To me, this is one case where you would certainly use
!important
.That said, you can try to use specificity to ensure your styles are being set.
So, make sure that you are targeting the elements are precisely as possible, in order to override those styles that are not as specific.
For instance, if you have this structure
The site's styles may target
You should aim to do this
which is more specific and would override the above styles.
The other thing you might be able to do is
append
your styles just before the</head>
, which would make them appear last in the cascade, and, if they are equal, will be applied.