上个星期,制定的前端开发规范,今天抽查了css规范的执行;发现了很多问题
.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}
.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;}
.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层嵌套,并且还有一个问题是,样式的命名采用了部分驼峰方式。
.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;}