css - 为什么这个 css 成为先例?

标签 css

我有一个文本框,我添加了一个类 (ui-state-error) 来添加边框颜色:#CD0A0A 但它不起作用。我意识到其他一些 css 胜过它:

textarea, input[type="text"], select {
    border: 1px solid #B5B8C8;
}

.ui-state-error, .ui-widget-content .ui-state-error, .ui-widget-header .ui-state-error {
     background: url("images/ui-bg_glass_95_fef1ec_1x400.png") repeat-x scroll 50% 50% #FEF1EC;
   border: 1px solid #CD0A0A;
   color: #CD0A0A;
}

无论如何我可以向文本框添加一个类“ui-state-error”并让该边框 css 覆盖 input[type="text"] 边框 css。

我想因为它在我的 css 下方,所以它会胜过上方?

最佳答案

CSS 规则的后续排序“胜过”(且仅当)选择器具有相同 Selector Specificity .

A selector's specificity is calculated as follows:

  1. count the number of ID selectors in the selector (= a)
  2. count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
  3. count the number of type selectors and pseudo-elements in the selector (= c)
  4. ignore the universal selector

在这种情况下,特异性是

input[type="text"]   -> a=0 b=1 c=1 -> 11  --winner, order doesn't matter
.ui-state-error      -> a=0 b=1 c=0 -> 10

现在,如果后面的选择器规则也包含类型选择器

input[type="text"]   -> a=0 b=1 c=1 -> 11 
input.ui-state-error -> a=0 b=1 c=1 -> 11  --winner, by ordering override

关于css - 为什么这个 css 成为先例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22523190/

相关文章:

html - 仅CSS的砌体布局

css - 如何将 “line break” 添加到 Flexbox,而不导致双倍大小的间隙?

javascript - 当我尝试从表单更新时出现白色错误屏幕

.net - 如果javascript被禁用,如何在服务器上检查?

html - 我创建的 Material UI 表有额外的 div 标签,这会破坏我的 CSS

javascript - jQuery .animate() - 不工作

css - 使用 Material Design 卡片 div 元素,但卡片中的内容不会居中

css - 如何使用 TabView 在 Tab 之间添加空格?

javascript - JQuery:$(this).data ('pinned' ) 实际上做了什么?

html - CSS3 : Flex display and overflow