重要声明

tech2022-12-26  66

During the importance and origin calculation in step two of the cascade resolution process, the !important statement can be used to add weight to a declaration. A declaration appended with the ! operator followed immediately by the keyword important, is said to be an important declaration, rather than a normal declaration. An important declaration in an author style sheet has more weight than a normal declaration in an author style sheet, but an important declaration in a user style sheet will trump them both.

在级联解析过程的第二步的重要性和原点计算过程中,!important语句可用于为声明增加权重。 带有!的声明。 运算符紧随其后的是重要的关键字,据说是重要的声明 ,而不是普通的声明。 作者样式表中的重要声明比作者样式表中的常规声明更重要,但是用户样式表中的重要声明将两者都胜过。

In CSS1, important declarations in an author style sheet had greater weight than important declarations in a user style sheet. In order to improve the accessibility of documents that use CSS for presentation, CSS2 reversed the ranking and gave important declarations in user style sheets the highest weight. This facility allows users with special needs to specify desired settings like a larger font size, and to be confident that those settings will be applied.

在CSS1中,作者样式表中的重要声明比用户样式表中的重要声明具有更大的权重。 为了提高使用CSS进行演示的文档的可访问性,CSS2颠倒了排名,并在用户样式表中赋予了重要的声明最高的权重。 此功能允许有特殊需要的用户指定所需的设置,例如较大的字体,并确信将应用这些设置。

Here’s an example of an important declaration—in this case, a font-size declaration:

这是一个重要声明的示例,在这种情况下,是一个font-size声明:

p { font-size: 1em !important; }

If important declarations have the same origin, the normal rules of specificity and order specified apply. If !important is used on a declaration with a shorthand property, it’s equivalent to adding !important to all of the individual subproperties.

如果重要的声明具有相同的来源,则应遵循特定的一般性规则和指定的顺序。 如果将!important用于具有简写属性的声明,则等效于将!important添加到所有单个子属性中。

重要的位置 (Placement of !important)

Make sure the !important statement is placed at the end of the declaration, just before the semicolon, and after the value. It will be invalid if it’s located anywhere else. Note also that when a shorthand property is used, the statement must still appear at the end of the list of values for that property, not against each individual value. Here’s an example:

确保!important语句放在声明的末尾,分号之前和值之后。 如果位于其他任何地方,它将无效。 还要注意,当使用速记属性时,该语句仍必须出现在该属性的值列表的末尾,而不是针对每个单独的值。 这是一个例子:

.example { margin: 10px 12px 9px 8px !important; }

The above rule would make all the margin values (top, right, bottom, and left margins) for elements within a class of “example” !important.

上面的规则将使“ example”类中的元素的所有页边距值(上,右,下和左页边距)都很重要。

重要和维护 (!important and Maintenance)

Introducing even one uncommented important declaration into an author style sheet has a huge negative impact on the style sheet’s maintainability, so it should be used with care. The only way to overwrite an important declaration is by using even more important declarations—an approach that soon becomes unmanageable. A style sheet that’s littered with important declarations often signals that an author hasn’t thought clearly enough about the structure of the CSS.

在作者样式表中甚至引入一个没有注释的重要声明都会对样式表的可维护性产生巨大的负面影响,因此应谨慎使用。 覆盖重要声明的唯一方法是使用更重要的声明,这种方法很快将变得难以管理。 充满重要声明的样式表通常表示作者对CSS的结构不够清楚。

翻译自: https://www.sitepoint.com/important-declarations/

最新回复(0)