CSS中属性排序

From: http://markdotto.com/2011/11/29/css-property-order/


CSS property order

CSS property order matters, especially on larger teams with several engineers and designers all contributing code. I know it’s been discussed, but frankly there’s always room for improvement and alternate approaches. In a few projects at Twitter, we added the constraint of consistent CSS property order to make onboarding new folks easier, debugging faster, and expectations of what exactly a block of CSS does that much clearer (without viewing it in the browser).

High level overview

At a high level, here’s our breakdown:

  • position
  • display and box model
  • font, leading, color, text
  • background and border
  • CSS3 properties like border-radius and box-shadow
  • and a handful of other purely visual properties

We hadn’t really seen an approach like this in any other projects we’ve worked on. It mixes the common structural and alphabetical approaches into one in an effort to ensure our property order is logical, scannable, and all-encompassing. We wanted an order that mapped to how we mentally visualized DOM elements and—to an extent—how Webkit’s Inspector presents properties.

Show me!

Getting more specific, here are some blocks of CSS might look with this property order in mind.

Here you can see positioning, box model (height and padding), and visual (background-colorborder-bottom and box-shadow) in their proper order. This flows in such a way that you can discern that this is a fixed dark gray bar with a drop shadow–you know exactly what it is you’re dealing with as you read line-by-line.

In this example, we have only box model and visual. Notice that for box model, we have dimensions first with min-height and padding (as that adds to the overall width of an element). For the visual elements,background-color and border come first because those inform you how this element starts to look (e.g., a gray box witha border). border-radius and box-shadow follow that because those are additional visual treatments that add to the base look and feel.

Use what works for your team

This approach seems to work well for us and our teams without adding too much overhead. All you need to do is create a series of rules and stick to them with your team and this because a very successful tactic for organizing and maintaining CSS. No one solution is teh best, so keep that in mind and adjust as you go.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS选择器的优先级排序如下: 1. `!important`:具有`!important`声明的样式具有最高优先级,无论其位置在哪里。 2. 行内样式:通过`style`属性直接应用于元素的样式具有较高的优先级。 3. ID选择器:通过`#`符号和元素的ID来选择元素的样式具有较高的优先级。 4. 类选择器、属性选择器和伪类选择器:通过`.`符号和类名、`[]`符号和属性名、以及`:`符号和伪类名来选择元素的样式具有较高的优先级。 5. 标签选择器:通过元素的标签名来选择元素的样式具有较低的优先级。 6. 通配符选择器:通过`*`符号来选择所有元素的样式具有较低的优先级。 7. 继承:从父元素继承的样式具有较低的优先级。 8. 浏览器默认属性:浏览器默认的样式具有最低的优先级。 以下是一个示例,展示了不同选择器的优先级排序: ```html <!DOCTYPE html> <html> <head> <style> /* ID选择器 */ #myElement { color: red; } /* 类选择器 */ .myClass { color: blue; } /* 标签选择器 */ p { color: green; } </style> </head> <body> <!-- 行内样式 --> <p style="color: orange;">This is a paragraph with inline style.</p> <!-- ID选择器 --> <p id="myElement">This is a paragraph with ID selector.</p> <!-- 类选择器 --> <p class="myClass">This is a paragraph with class selector.</p> <!-- 标签选择器 --> <p>This is a normal paragraph.</p> </body> </html> ``` 在上述示例,`!important`声明具有最高优先级,行内样式具有较高优先级,ID选择器具有较高优先级,类选择器具有较低优先级,标签选择器具有较低优先级。因此,段落元素`<p>`的颜色将按照优先级排序来确定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值