本文介绍了CSS样式优先级无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

/specificity.keegan.st/rel =nofollow> https://specificity.keegan.st/ ,你会发现非常方便和有用,直到你完全熟悉的特异性计算。



为简单起见,下面是如何计算特异性:




  • 计算作为完全选择器一部分的id选择器的总数。将此作为 a

  • 计算作为完整选择器一部分的类,属性和伪类选择器的总数。以 b 为例。

  • 计算作为完整选择器一部分的元素类型和伪元素选择器的总数。以 c 为例。



最后的特殊性是 a + b + c

I have two styles, each declared in its separate 3rd party control that don't play well together and I'm not even sure why that is - and ultimately - how to make them work as I want.

HTML:

<table>
  <tr class="myrow">
    <td>normal cell</td>
    <td class="ui-state-error">error cell</td>
  </tr>
</table>

CSS:

.myrow>td {  /* declared in the grid component */
  background-color: #88f;
}
.ui-state-error {  /* declared in jQuery UI */
  background-color: #f00;
}

Both HTML & CSS simplified to show the exact error. Here's the fiddle.

The end result here is both cells displaying blue background (FF 46.0).

However, I would expect the .ui-state-error to be more specific as it's applied directly to the cell in question vs. the .myrow>td rule which seems more general at first glance.

Regardless, the browser decides to apply .myrow>td CSS and I want to make it apply the .ui-state-error.

How can I do that considering the fact that CSS styles themselves are not under my control?

解决方案

Both your rules are applied directly to the cell - one is via the class and other is via the element type. Since the selector with element type has another class selector attached to it, it has more specificity and hence wins.

The specificity of .ui-state-error selector is 010 because it has only one class selector as part of the complex selector whereas the .myrow > td selector's specificity is 011 as it has one class and one element type selector as part of the complex selector.

You can change the selector like given below to make the error td have red background.

.myrow > .ui-state-error {
  background-color: #f00;
}

Changing the selector like above will not override all other styles. It will override only selectors that are lower in specificity than 010 and apply only for element which have class='ui-state-error when their ancestor has class='myrow'.

If the myrow is a theme specific class and you want the .ui-state-error elements to be red in color irrespective of the theme then you could change the selector like below:

tr > .ui-state-error {
  background-color: #f00;
}

This also has specificity of 011 and would override the .myrow > td.

There is a specificity calculator available at https://specificity.keegan.st/ which you'd find very handy and useful till such time you get completely familiar with the specificity calculations.

To put it simple terms, below is how specificity is calculated:

  • Calculate the total number of id selectors that are part of the full selector. Take this as a
  • Calculate the total number of class, attribute and pseudo-class selectors that are part of the full selector. Take this as b.
  • Calculate the total number of element type and pseudo-element selectors that are part of the full selector. Take this as c.

The final specificity is a + b + c (concatenation and not sum).

这篇关于CSS样式优先级无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:39