mishe / blog

前端碰上的问题或体会
230 stars 39 forks source link

css 规范的code review #128

Open mishe opened 8 years ago

mishe commented 8 years ago

上个星期,制定的前端开发规范,今天抽查了css规范的执行;发现了很多问题

引入了css 模块化思想,可以接受少量的代码冗余,但不表示可以接受大面积的代码冗余。

表现症状1

.footer .home-icon:before{background-position:1px -166px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .productList-icon:before{background-position:-45px -166px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .me-icon:before{background-position:-89px -166px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .mores-icon:before{background-position:-135px -166px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .homeActive:before{background-position:1px -210px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .productListActive:before{background-position:-45px -210px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .meActive:before{background-position:-89px -210px;width: 42px;height: 40px;margin: 8px 0 2px}
.footer .moresActive:before{background-position:-135px -210px;width: 42px;height: 40px;margin: 8px 0 2px}

上面的代码就范了大面积冗余的问题; 42px;height: 40px;margin: 8px 0 2px 这些代码都是一样的,完全可以把它们定义成一个。

.footer .icons:before{width: 42px;height: 40px;margin: 8px 0 2px}
.footer .home-icon:before{background-position:1px -166px}
.footer .productList-icon:before{background-position:-45px -166px}
.footer .me-icon:before{background-position:-89px -166px}
.footer .mores-icon:before{background-position:-135px -166px}
.footer .homeActive:before{background-position:1px -210px}
.footer .productListActive:before{background-position:-45px -210px}
.footer .meActive:before{background-position:-89px -210px}
.footer .moresActive:before{background-position:-135px -210px}

表现症状2

.property .regular-module1 .append-list{width: 100%;height: 90px;background-color: #FFF;}
.property .regular-module1 .append-list .append-listData{display: inline-block;width: 88%;height: 90px}
.property .regular-module1 .append-list .append-listData .append-word1{font-size: 24px;color: #666;padding-top: 18px;clear: both}
.property .regular-module1 .append-list .append-listData .append-word2{font-size: 20px;color: #999;padding-top: 10px;clear: both}
.property .regular-module1 .append-list .append-listData .append-wordData1{float: left;padding-left: 30px;}

.property .integralDetails-module1 .append-list{width: 100%;height: 90px;background-color: #FFF;}
.property .integralDetails-module1 .append-list .append-listData{display: inline-block;width: 100%;height: 90px}
.property .integralDetails-module1 .append-list .append-listData .append-word1{font-size: 24px;color: #666;padding-top: 18px;clear: both}
.property .integralDetails-module1 .append-list .append-listData .append-word2{font-size: 20px;color: #999;padding-top: 10px;clear: both}
.property .integralDetails-module1 .append-list .append-listData .append-wordData1{float: left;padding-left: 30px;}

上列中,虽然分了模块,但事实上引入了3层的模块嵌套,我们完全没有必要这样做,完全可以简化成下面的内容,不仅css文件减少了,而且代码也更简洁;如果有差异,在增加一个样式嵌套就可以了,所以,一般只要3层嵌套就可以解决大部分的样式问题,模块实在太大,css太多,完全可以分拆成2个模块来实现。

.property .append-list{width: 100%;height: 90px;background-color: #FFF;}
.property .append-listData{display: inline-block;width: 88%;height: 90px}
.property .append-word1{font-size: 24px;color: #666;padding-top: 18px;clear: both}
.property .append-word2{font-size: 20px;color: #999;padding-top: 10px;clear: both}
.property .append-word-data1{float: left;padding-left: 30px;}

有些规范被忽略了

超过3层嵌套

.property .regular-module1 .append-list .append-listData{display: inline-block;width: 88%;height: 90px}
.property .regular-module1 .append-list .append-listData .append-word1{font-size: 24px;color: #666;padding-top: 18px;clear: both}
.property .regular-module1 .append-list .append-listData .append-word2{font-size: 20px;color: #999;padding-top: 10px;clear: both}
.property .regular-module1 .append-list .append-listData .append-wordData1{float: left;padding-left: 30px;}
.property .regular-module1 .append-list .append-listData .append-wordData2{float: right}
.property .regular-module1 .append-list .append-listData .append-wordData3{color: #ff6046}
.property .regular-module1 .append-list .append-listIcon{display: inline-block;margin-bottom: 32px}

规范定义css嵌套尽量在3层以内,上列尽量有5层嵌套,并且还有一个问题是,样式的命名采用了部分驼峰方式。

没有使用-线连接

使用了单词命名css

.more{float: right;margin: 0 20px;}
.more:before{background-position:-116px 3px;width: 30px;height: 30px;margin-bottom: -7px}
.eye,.eyes{float: right;display: inline-block;margin: 0 30px;}
.eye:before{background-position:-240px 14px;width: 32px;height: 24px;}
.eyes:before{background-position:-240px -12px;width: 32px;height: 24px;}
lichking1201 commented 8 years ago

css比较好的模块化思想是BEM

mishe commented 8 years ago

刚查了下BEM的思想,我倒,这是服务端生成的页面骨架,让前端的csser写样式? 我曾经做过很长一段时间这样的csser,对于提高个人的css能力是个很不错的方式。 但这个称之为css模块化就过了吧,现在最好的应该是css module,但需要借助webpack之类的工具才能实现,如果没有工具帮助的话,我的方法还是不错的,可以一试。

lee134134134 commented 8 years ago

I feel more accord with oocss。