CSS 样式表优先顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13638038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:55:01  来源:igfitidea点击:

Style sheets priority order

cssstylesheet

提问by Chin

If I have the following style sheets:

如果我有以下样式表:

user important declarations
user normal declarations
author normal declarations
user agent declarations
author important declarations

and need to apply them from highest priority to lowest priority, in which order should I use them?

并且需要从最高优先级到最低优先级应用它们,我应该按什么顺序使用它们?

回答by Niet the Dark Absol

The correct order of precedence is, from least important to most important:

正确的优先顺序是,从最不重要到最重要:

  1. User Agent
  2. User Normal
  3. Author Normal
  4. Author Important
  5. User Important
  1. 用户代理
  2. 普通用户
  3. 作者正常
  4. 作者重要
  5. 用户重要

As defined in the specification

如在定义规范

回答by Mehdi Souregi

CSS declarations are applied in this order (from lowest to highest priority):

CSS 声明按以下顺序应用(从最低到最高优先级):

  1. user agent declarations (the default styles your browser applies to elements)
  2. user normal declarations (a user's own stylesheet if they're using one)
  3. author normal declarations (this is your normal stylesheet)
  4. author important declarations (anything your mark important)
  5. user important declarations (any important styles from the user's stylesheet)
  1. 用户代理声明(浏览器应用于元素的默认样式)
  2. 用户普通声明(如果他们使用的是用户自己的样式表)
  3. 作者普通声明(这是您的普通样式表)
  4. 作者重要声明(任何你的标记重要的东西)
  5. 用户重要声明(用户样式表中的任何重要样式)

回答by Aamir Shahzad

Below content is taken from here.

以下内容摘自这里

Style property can appear in any number of style sheets, and several times inside a single style sheet. Therefore, order of applying the rules is very important. This is called the "cascade" order. According to CSS2 spec, the cascade order is (from low to high):

Style 属性可以出现在任意数量的样式表中,并且可以多次出现在单个样式表中。因此,应用规则的顺序非常重要。这称为“级联”顺序。根据 CSS2 规范,级联顺序是(从低到高):

  1. Browser declarations
  2. User normal declarations
  3. Author normal declarations
  4. Author important declarations
  5. User important declarations
  1. 浏览器声明
  2. 用户正常声明
  3. 编写普通声明
  4. 作者重要声明
  5. 用户重要声明

a) The browser declarations are least important;
b) User overrides the author only if the declaration was marked as important.
c) Declarations with the same order will be sorted by specificityand then the order they are specified.
d) The HTML visual attributes are translated to matching CSS declarations.
e) They are treated as author rules with low priority.

a) 浏览器声明最不重要;
b) 仅当声明被标记为重要时,用户才会覆盖作者。
c) 具有相同顺序的声明将按特殊性排序,然后是它们指定的顺序。
d) HTML 视觉属性被转换为匹配的 CSS 声明。
e) 它们被视为具有低优先级的作者规则。