Precedence of multiple classes defining color property being set by declaration order rather than specification order

时间秒杀一切 提交于 2019-12-03 01:25:55

The order of the classes as you specify them on the elements is irrelevant. It's the order that you define them in your style declarations that matters. The quote you posted means in the style declarations, not the order the classes are listed on the elements.

Funslinger

This seems to limit options. If I want to use two classes on two different paragraphs but have the precedence be different, I'd need to define a third class that is the same as the first defined class but with a slightly different name.

e.g

.xxxxx {margin-top: 1em; margin-left: 0; text-align: left; ... [some other attribute definitions]}<br/>

.yyyyy {margin-top: 3em; margin-left: 1em; text-align: justify; ... [some different attribute definitions]}

<p class="yyyyy xxxxx">...</p> 

will always use a top margin of 3em with a left margin of 1em and a text alignment of justify plus the other attributes in xxxxx and yyyyy. However, there might be a situation in which I want to override the margin-top, margin-left, and text-align settings while keeping all of the other attributes in xxxxx and yyyyy. If precedence was determined by order in the class attribute then I would have that option by doing this:

<p class="xxxxx yyyyy">...</p>

Since it is not, I have to create another definition below the two in question, as so:

.zzzzz {margin-top: 1em; margin-left 0; text-align: left; ... [some other attribute definitions]}

and use

<p class="yyyyy zzzzz">...</p>

This seems like a non-optimal solution to the problem whereas class attribute order precedence allows more flexibility without mucking up the CSS.

BTW, the Nook Glowlight does it the way it should be done by allowing me to set precedence by the order in the class attribute. And thus my css is a bit more compact.

If there is anything that I've learned in my decades of programming, it's that precedence is best placed as close as possible to where all the work gets done. Placing precedence in a linked static stylesheet reduces flexibility.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!