覆盖 CSS 类选择器而不使用 !important
我只是为网络应用程序编写一个 javascript UI 对话框。问题是用户可以为网络应用程序创建自己的主题,其中可能包括元素 css 选择器(h1 {...}、div {...}
等),它们会覆盖我的 css 格式用于 UI 对话框。该对话框是一个 div 元素,通过类选择器进行格式化(id 不起作用,因为该对话框可能会出现多次)。有没有办法阻止用户模板样式的元素选择器影响 UI 对话框,而不必使用巨大的 css 重置样式并为 UI 对话框的每种样式使用 !important
? jQuery UI 等 UI 库如何设置对话框样式?
例如,用户主题包括:
input {color:nuts; height:bananas; width:crazy; background:morenuts; }
UI 的 css:
.ui_modal_wrap input {color:red; border:1px solid black;}
UI 的 html:
<div class="ui_modal_wrap>
<input type="text" name="#" />
</div>
问题仍然是我必须为 .ui_modal_wrap 使用巨大的 css 其余部分,因为您无法真正编写具有用户可以应用的所有属性的 css(在本例中height 和 width 元素仍然会破坏样式,因为 UI 不包括高度和宽度)。
I'm just writing a javascript UI dialog for a web app. The problem is that users can create there own themes for the web app, which may include element css selectors (h1 {...}, div {...}
etc.) which overwrite my css formatting for the UI dialog. The dialog is a div element which is formatted over a class selector (id dose not work because this dialog may appear multiple times). Is there a way to stop the element selector of the user template style affecting the UI dialog, without having to use a huge css reset style and using !important
for every style of the UI dialog? How do UI libraries like jQuery UI style there dialog boxes?
For example the user theme includes:
input {color:nuts; height:bananas; width:crazy; background:morenuts; }
The UI's css:
.ui_modal_wrap input {color:red; border:1px solid black;}
The UI's html:
<div class="ui_modal_wrap>
<input type="text" name="#" />
</div>
Problem is still that I have to use a huge css rest for .ui_modal_wrap because you can't really write css with all attributes a user could apply (in this example the height and width element would still break the styling because the UI's dose not include height and width).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,您应该知道规则是通过
特异性规则
应用的(如果两者都在外部样式表中)。一般来说,您可以考虑一个分数,然后将应用适用于分数最高的元素的规则。
在选择器链中,每个元素类型值 1,类值 10,id 值 100。
body div.wrapper == 12 点
body div.wrapper div span == 14 点
body #quote == 101 点
所以一般只需制作页面特定的规则(页面其余部分的样式)不太具体,并让 UI CSS 接管。或者,您始终可以将 UI 标记放在 HTML 的“超级”位中,例如:
然后通过在该样式表上的每个规则之前放置
#super #super2
来“命名空间”UI 的 CSS。In general you should know that rules are applied (if both are in external stylesheets) with
specificity rules
.Generally there is a score you can think about, and the rule applying to the element that has the highest score will be applied.
In a selector chain, each element type is worth one, classes are worth 10, and an id is worth 100 points.
body div.wrapper == 12 points
body div.wrapper div span == 14 points
body #quote == 101 points
So generally just make the page-specific rules (that style the rest of your page) less specific, and let the UI CSS take over. Alternately you could always put the UI markup inside a "super" bit of HTML, like:
And then "namespace" the CSS for the UI by putting
#super #super2
before each rule on that style sheet.根本没有办法确保这一点,除非:
阅读 CSS 特异性
There no way to ensure this at all unless:
read css specificity
您将需要有第二组样式,它使用更具体的选择器,它将覆盖用户样式设置的元素。使用
!important
只是实现此目的的一种方法。查看CSS 特异性以获取更多信息。HTML
CSS:
jQuery UI 通过在您使用主题滚轮时为您提供指定 CSS“范围”的选项来实现此目的。您可以将范围设置为
.system-scope
,然后在 HTML 中将所有 UI 元素包装在具有系统范围类的元素中。You will need to have a second set of styles which use more specific selectors, which will override the elements set by the user's styles. Using
!important
is just one way of doing this. Look up CSS Specificity for more information.HTML
CSS:
jQuery UI implement this by providing you with the option to specify a CSS 'scope' when you use the Theme Roller. You could set the scope to
.system-scope
and then in your HTML wrap all UI elements in an element with a class of system-scope.