CSS属性有特定的顺序吗? [英] Is there a specific order for CSS properties?
问题描述
例如,如果我写:
background-color: black;
color: white;
或
color: white;
background-color: black;
这两者之间有什么区别,我应该关心订单吗?
Is there any difference between the two and should I care about order?
推荐答案
否。你可以按任何顺序写它们。在同一个选择器中有重复的情况下,最后一条规则获胜。
No. You can write them in any order. In the case of duplicates within the same selector, the last rule wins.
p {
color:blue;
color:red;
color:green; /* Green wins */
}
在处理多个选择器时,
When dealing with multiple selectors, rules that come later will override earlier rules, unless the earlier selector is more specific.
例如,此代码将所有段落变为绿色:
For example, this code will turn all your paragraphs green:
p { color:red; }
/* ... */
p { color:green; }
虽然此代码会将您的所有段落变为红色,
While this code will turn all your paragraphs red, because the first selector is more specific.
body p { color:red; }
/* ... */
p { color:green; }
虽然选择器中的规则顺序可能不重要,一个可预测的顺序(例如在着色规则之前调整规则之前的位置规则),简单地使您的CSS保持一致和略微更愉快的维护。
While the order of rules within a selector may not matter, I would take care to write them in a predictable order (position rules before sizing rules before colouring rules, for example) simply to make your CSS consistent and slightly more pleasant to maintain.
这篇关于CSS属性有特定的顺序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!