CSS编码规范

174 阅读1分钟

属性书写顺序

书写属性时,应按功能进行分组,并以 Formatting Model(布局方式、位置) > Box Model(尺寸) > Typographic(文本相关) > Visual(视觉效果) 的顺序书写,以提高代码的可读性。

  • Formatting Model 相关属性包括:position / top / right / bottom / left / float / display / overflow
  • Box Model 相关属性包括:border / margin / padding / width / height
  • Typographic 相关属性包括:font / line-height / text-align / word-wrap
  • Visual 相关属性包括:background / color / transition / list-style

若包含 content 属性,应放在最前面

.main {
    /* formatting model: positioning schemes / offsets / z-indexes / display / ...  */
    content: " ";
    position: relative;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    float: left;
    z-index: 1;
    display: flex;
    clear: both;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    
    
    /* box model: sizes / margins / paddings / borders / ... */
    box-sizing: border-box;
    width: 100vw;
    height: 100vh;
    margin: 0;
    padding: 0;
    border: 1px solid #1867BE;

    /* typographic: font / aligns / text styles / ... */
    font-size: 14px;
    line-height: 14px;
    text-align: center;
    word-wrap: break-word;

    /* visual: colors / shadows / gradients / ... */
    background-color: #fff;
    color: #333;
    box-shadow: 0 0 0;
    cursor: pointer;
    transition: color .3s;
    transform: scale(1);
    opacity: 0;
}

参考自:github.com/ecomfe/spec…