属性缩写与分拆
- 无继承关系时,使用缩写
不推荐:
body {margin-top: 10px;margin-right: 10px;margin-bottom: 10px;margin-left: 10px;}
推荐:
body {margin: 10px;}
- 存在继承关系时,使用分拆方式
不推荐:
.m-detail {font: bold 12px/1.5 arial, sans-serif;}.m-detail .info {font: normal 14px/1.5 arial, sans-serif;}
要避免错误的覆盖:
.m-detail .info {font: 14px sans;}
如果你只是想改字号和字体,然后写成了上面这样,这是错误的写法,因为
font复合属性里的其他属性将会被重置为 user agent 的默认值,比如font-weight就会被重置为normal。
推荐:
.m-detail {font: bold 12px/1.5 arial, sans-serif;}.m-detail .info {font-weight: normal;font-size: 14px;}
在存在继承关系的情况下,只将需要变更的属性重定义,不进行缩写,避免不需要的重写的属性被覆盖定义
- 根据规则条数选择缩写和拆分
不推荐:
.m-detail {border-width: 1px;border-style: solid;border-color: #000 #000 #f00;}
推荐:
.m-detail {border: 1px solid #000;border-bottom-color: #f00;}
