css - 为什么用户代理样式表是最后一个要应用的样式表?

标签 css

我看到了answer 讨论了样式表在网页中的应用顺序。

我正在学习认证,我遇到了这个问题:

Question:
You are creating an application by using HTML5 and CSS3. The styles for the pages are derived from five style sheets. The styles are not being applied correctly to the pages in the application. You need to determine the order in which the style sheets will be applied by the browser. In which order will the five style sheets be applied? To answer, move the style sheet types from the list of style sheet types to the answer area and arrange them in the order that they will be applied.

user agent style sheets
user normal style sheets
author normal style sheets
author important style sheets
user important style sheets

Answer:

user normal style sheets
author normal style sheets
author important style sheets
user important style sheets
user agent style sheets

为什么“用户代理样式表”是最后一个应用的?第一个不是因为它是浏览器默认样式表吗?

最佳答案

@GoldShip,答案在这里:

http://www.w3.org/TR/2011/REC-CSS2-20110607/cascade.html#cascade

引用上述链接:

6.4.1 Cascading order

To find the value for an element/property combination, user agents must apply the following sorting order:

  1. Find all declarations that apply to the element and property in question, for the target media type. Declarations apply if the associated selector matches the element in question and the target medium matches the media list on all @media rules containing the declaration and on all links on the path through which the style sheet was reached.

  2. Sort according to importance (normal or important) and origin (author, user, or user agent). In ascending order of precedence:

    1. user agent declarations
    2. user normal declarations
    3. author normal declarations
    4. author important declarations
    5. user important declarations
  3. Sort rules with the same importance and origin by specificity of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively.
  4. Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins. Declarations in imported style sheets are considered to be before any declarations in the style sheet itself.

关于css - 为什么用户代理样式表是最后一个要应用的样式表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543083/

相关文章:

html - 无法将 css 样式表链接到 html

html - div 和 anchor 标记错误之间的异常间隙

jquery animate() 不使用 css 属性

html - WebVTT 样式未应用于简单示例

css - 如果 <li> 具有特定 ID,则无法更改背景颜色

javascript - 如何将文本放在图像上

javascript - 我需要一个非常具体的 jquery 选择器

css - Iphone 上奇怪的背景图像和边框错误

html - 我应该响应网页到多少像素?

css - 跨桌面屏幕尺寸很好地显示