自从开始做前端开发以来,我发现在开发页面的时候,总是有一个问题十分影响自己的开发效率,这个问题就是css的命名,主要是指css类选择器的命名。这个问题主要体现在:第一,有的内容你压根想不出用什么名字来给它命名,因为一般命名总是考虑语义化,好让其他人看到这个css类的名字就知道它是作用于哪一个内容的,但是由于网页内容的复杂性和多样性,你很难保证每个部分都能起一个合适的名字,即使你最终迫不得已想出了一个名字,也会有这个名字是否是最合适的这种纠结存在,而且最要命的是,这个命名的过程是一项非常辛苦的脑力活动,会耗费掉很多脑细胞,这一件很不值得的事情;第二,由于命名的时候是语义化的命名,这一点可能会阻碍css代码的重用,比如说某一个网页的内容用.title来描述它的样式,这个title包含了2条规则,{font-size: 14px; line-height: 20px},此时网页的另一个内容可能需要跟这个title具有一模一样的样式,但是从另一个内容所处的网页位置来说,可能用.desc来命名才是更合适的选择,这个时候,我相信喜欢语义化命名的人肯定会把那个内容再单独起一个css类desc,然后把title的样式复制过来,结果就导致css文件中会存在两份相同的样式规则,只是命名不同而已,这样代码就重复了。
解决这个问题的方法就是采用面向属性的css命名,把那些我们实在想不出名字的,而且没有必要起名字的,并且可以只用单一的css属性或者组合的单一css属性来描述的部分,通通都用面向属性的css类来控制样式,让你从烦乱的css命名的漩涡中彻底解放出来,除了提高你的工作效率,最重要的是减少你脑细胞的损耗,让你不会那么辛苦。
首先要声明,面向属性的css命名这个思想不是我的原创,它来自于张鑫旭的博客。我是当时比较纠结于css的命名问题,然后找到了他的两篇文章,对我重新认识css的命名以及如何组织全站的css有很多的帮助,这两篇文章分别是:
精简高效的CSS命名准则/方法
我是如何对网站CSS进行架构的
你可以先去通过他的文章了解这个命名思想的起源,再回来看我的一些总结跟应用。
这个方法,简单来说,就是直接以css属性简写作为css的类名,在使用的时候,通过使用一个或组合多个这样的简写形式的css类来达到控制样式的目的。比如说网页中有一个网页内容,是一段居中的文本,大小为12px,行高为20px,段前后间距分别为10px和15px,那么就可以定义一下这些简单的属性类:
<span style="color: #800000">.tc </span>{<span style="color: #ff0000">text-align</span>:<span style="color: #0000ff"> center</span>;}<span style="color: #800000"> .f12 </span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff"> 12px</span>;}<span style="color: #800000"> .lh20 </span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff"> 20px</span>;}<span style="color: #800000"> .mt10 </span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff"> 10px</span>;}<span style="color: #800000"> .mtb15 </span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff"> 15px</span>;}
在页面中使用的时候,直接组合以上这些css属性类即可:
<span style="color: #0000ff"><span style="color: #800000">p </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="tc f12 lh20 mt10 mb15"</span><span style="color: #0000ff">>…</span><span style="color: #800000">p</span><span style="color: #0000ff">></span></span>
这就是这种属性命名方法的具体使用方式。
css中有很多的属性都可以采用这种方法来命名。在张鑫旭的博客中,他把自己的这套方法总结成了一个开源的css库:https://github.com/zhangxinxu/zxx.lib.css/blob/master/zxx.lib.css。我们可以先从他这个库来了解他本人是如何组织这些不同的css属性类的,然后再来讨论这其中的一些问题。
这是他的开源库中,采用面向属性命名的全部css类(以下代码仅是为了阅读本文方便才引用,如果是想在实际工作中使用,最好是关注张鑫旭本人的github或者博客,因为他会不断地优化自己的东西):
<span style="color: #008000">/*</span><span style="color: #008000"> display </span><span style="color: #008000">*/</span><span style="color: #800000"> .dn</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">none</span>;}<span style="color: #800000"> .di</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">inline</span>;}<span style="color: #800000"> .db</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">block</span>;}<span style="color: #800000"> .dib</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">inline-block</span>;}<span style="color: #800000"> div.dib</span>{<span style="color: #ff0000">*display</span>:<span style="color: #0000ff">inline</span>;<span style="color: #ff0000"> *zoom</span>:<span style="color: #0000ff">1</span>;}<span style="color: #008000">/*</span><span style="color: #008000"> other block level tag(eg. p, li, h1~h6), using 'inline_any' instead </span><span style="color: #008000">*/</span> <span style="color: #008000">/*</span><span style="color: #008000"> height </span><span style="color: #008000">*/</span><span style="color: #800000"> .h0</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">0</span>;}<span style="color: #800000"> .h16</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">14px</span>;}<span style="color: #800000"> .h16</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">16px</span>;}<span style="color: #800000"> .h18</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">18px</span>;}<span style="color: #800000"> .h20</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .h22</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">22px</span>;}<span style="color: #800000"> .h24</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">24px</span>;}<span style="color: #800000"> .h30</span>{<span style="color: #ff0000">height</span>:<span style="color: #0000ff">30px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> width </span><span style="color: #008000">*/</span> <span style="color: #008000">/*</span><span style="color: #008000"> fixed width value </span><span style="color: #008000">*/</span><span style="color: #800000"> .w20</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .w50</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">50px</span>;}<span style="color: #800000"> .w70</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">70px</span>;}<span style="color: #800000"> .w100</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">100px</span>;}<span style="color: #800000"> .w120</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">120px</span>;}<span style="color: #800000"> .w140</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">140px</span>;}<span style="color: #800000"> .w160</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">160px</span>;}<span style="color: #800000"> .w180</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">180px</span>;}<span style="color: #800000"> .w200</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">200px</span>;}<span style="color: #800000"> .w220</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">220px</span>;}<span style="color: #800000"> .w250</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">250px</span>;}<span style="color: #800000"> .w280</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">280px</span>;}<span style="color: #800000"> .w300</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">300px</span>;}<span style="color: #800000"> .w320</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">320px</span>;}<span style="color: #800000"> .w360</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">360px</span>;}<span style="color: #800000"> .w400</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">400px</span>;}<span style="color: #800000"> .w460</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">460px</span>;}<span style="color: #800000"> .w500</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">500px</span>;}<span style="color: #800000"> .w600</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">600px</span>;}<span style="color: #800000"> .w640</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">640px</span>;}<span style="color: #800000"> .w700</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">700px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> percent width value </span><span style="color: #008000">*/</span><span style="color: #800000"> .pct10</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">10%</span>;}<span style="color: #800000"> .pct15</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">15%</span>;}<span style="color: #800000"> .pct20</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">20%</span>;}<span style="color: #800000"> .pct25</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">25%</span>;}<span style="color: #800000"> .pct30</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">30%</span>;}<span style="color: #800000"> .pct33</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">33.3%</span>;}<span style="color: #800000"> .pct40</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">40%</span>;}<span style="color: #800000"> .pct50</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">50%</span>;}<span style="color: #800000"> .pct60</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">60%</span>;}<span style="color: #800000"> .pct66</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">66.6%</span>;}<span style="color: #800000"> .pct70</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">70%</span>;}<span style="color: #800000"> .pct75</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">75%</span>;}<span style="color: #800000"> .pct80</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">80%</span>;}<span style="color: #800000"> .pct90</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">90%</span>;}<span style="color: #800000"> .pct100</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">100%</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> line-height </span><span style="color: #008000">*/</span><span style="color: #800000"> .lh0</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">0</span>;}<span style="color: #800000"> .lh16</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">14px</span>;}<span style="color: #800000"> .lh16</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">16px</span>;}<span style="color: #800000"> .lh18</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">18px</span>;}<span style="color: #800000"> .lh20</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .lh22</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">22px</span>;}<span style="color: #800000"> .lh24</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">24px</span>;}<span style="color: #800000"> .lh30</span>{<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">30px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> margin </span><span style="color: #008000">*/</span><span style="color: #800000"> .m0</span>{<span style="color: #ff0000">margin</span>:<span style="color: #0000ff">0</span>;}<span style="color: #800000"> .ml1</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .ml2</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .ml5</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .ml10</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .ml15</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .ml20</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .ml30</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .mr1</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .mr2</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .mr5</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .mr10</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .mr15</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .mr20</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .mr30</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .mt1</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .mt2</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .mt5</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .mt10</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .mt15</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .mt20</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .mt30</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .mb1</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .mb2</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .mb5</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .mb10</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .mb15</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .mb20</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .mb30</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">30px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> margin negative </span><span style="color: #008000">*/</span><span style="color: #800000"> .ml-1</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">-1px</span>;}<span style="color: #800000"> .mr-1</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">-1px</span>;}<span style="color: #800000"> .mt-1</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">-1px</span>;}<span style="color: #800000"> .mb-1</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">-1px</span>;}<span style="color: #800000"> .ml-3</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">-3px</span>;}<span style="color: #800000"> .mr-3</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">-3px</span>;}<span style="color: #800000"> .mt-3</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">-3px</span>;}<span style="color: #800000"> .mb-3</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">-3px</span>;}<span style="color: #800000"> .ml-20</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">-20px</span>;}<span style="color: #800000"> .mr-20</span>{<span style="color: #ff0000">margin-right</span>:<span style="color: #0000ff">-20px</span>;}<span style="color: #800000"> .mt-20</span>{<span style="color: #ff0000">margin-top</span>:<span style="color: #0000ff">-20px</span>;}<span style="color: #800000"> .mb-20</span>{<span style="color: #ff0000">margin-bottom</span>:<span style="color: #0000ff">-20px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> padding </span><span style="color: #008000">*/</span><span style="color: #800000"> .p0</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">0</span>;}<span style="color: #800000"> .p1</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .pl1</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .pt1</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .pr1</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .pb1</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">1px</span>;}<span style="color: #800000"> .p2</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .pl2</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .pt2</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .pr2</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .pb2</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .pl5</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .p5</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .pt5</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .pr5</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .pb5</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">5px</span>;}<span style="color: #800000"> .p10</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .pl10</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .pt10</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .pr10</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .pb10</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">10px</span>;}<span style="color: #800000"> .p15</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .pl15</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .pt15</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .pr15</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .pb15</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">15px</span>;}<span style="color: #800000"> .p20</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .pl20</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .pt20</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .pr20</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .pb20</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .p30</span>{<span style="color: #ff0000">padding</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .pl30</span>{<span style="color: #ff0000">padding-left</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .pt30</span>{<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .pr30</span>{<span style="color: #ff0000">padding-right</span>:<span style="color: #0000ff">30px</span>;}<span style="color: #800000"> .pb30</span>{<span style="color: #ff0000">padding-bottom</span>:<span style="color: #0000ff">30px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> border-color name rule: border(b)-position(l/r/t/b/d)-width(null/2)-style(null/sh)-color(first one letter/first two letter) |-> All colors are safe color</span><span style="color: #008000">*/</span><span style="color: #800000"> .bdc</span>{<span style="color: #ff0000">border</span>:<span style="color: #0000ff">1px solid #ccc</span>;}<span style="color: #800000"> .blc</span>{<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #ccc</span>;}<span style="color: #800000"> .brc</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #ccc</span>;}<span style="color: #800000"> .btc</span>{<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #ccc</span>;}<span style="color: #800000"> .bbc</span>{<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #ccc</span>;}<span style="color: #800000"> .bdd</span>{<span style="color: #ff0000">border</span>:<span style="color: #0000ff">1px solid #ddd</span>;}<span style="color: #800000"> .bld</span>{<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #ddd</span>;}<span style="color: #800000"> .brd</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #ddd</span>;}<span style="color: #800000"> .btd</span>{<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #ddd</span>;}<span style="color: #800000"> .bbd</span>{<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #ddd</span>;}<span style="color: #800000"> .bde</span>{<span style="color: #ff0000">border</span>:<span style="color: #0000ff">1px solid #eee</span>;}<span style="color: #800000"> .ble</span>{<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #eee</span>;}<span style="color: #800000"> .bre</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #eee</span>;}<span style="color: #800000"> .bte</span>{<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #eee</span>;}<span style="color: #800000"> .bbe</span>{<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #eee</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> background-color name rule: bg - (key word/Hex color) |-> All colors are safe color </span><span style="color: #008000">*/</span><span style="color: #800000"> .bgwh</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#fff</span>;}<span style="color: #800000"> .bgfb</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#fbfbfb</span>;}<span style="color: #800000"> .bgf5</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#f5f5f5</span>;}<span style="color: #800000"> .bgf0</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#f0f0f0</span>;}<span style="color: #800000"> .bgeb</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#ebebeb</span>;}<span style="color: #800000"> .bge0</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#e0e0e0</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> safe color </span><span style="color: #008000">*/</span><span style="color: #800000"> .g0</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">#000</span>;}<span style="color: #800000"> .g3</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">#333</span>;}<span style="color: #800000"> .g6</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">#666</span>;}<span style="color: #800000"> .g9</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">#999</span>;}<span style="color: #800000"> .gc</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">#ccc</span>;}<span style="color: #800000"> .wh</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">white</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> font-size </span><span style="color: #008000">*/</span><span style="color: #800000"> .f0</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">0</span>;}<span style="color: #800000"> .f12</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">12px</span>;}<span style="color: #800000"> .f13</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">13px</span>;}<span style="color: #800000"> .f14</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">14px</span>;}<span style="color: #800000"> .f16</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">16px</span>;}<span style="color: #800000"> .f18</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">18px</span>;}<span style="color: #800000"> .f20</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">20px</span>;}<span style="color: #800000"> .f24</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">24px</span>;}<span style="color: #800000"> .f28</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">28px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> font-family </span><span style="color: #008000">*/</span><span style="color: #800000"> .fa</span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff">Arial</span>;}<span style="color: #800000"> .ft</span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff">Tahoma</span>;}<span style="color: #800000"> .fv</span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff">Verdana</span>;}<span style="color: #800000"> .fs</span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff">Simsun</span>;}<span style="color: #800000"> .fl</span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff">'Lucida Console'</span>;}<span style="color: #800000"> .fw</span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff">'Microsoft Yahei'</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> font-style </span><span style="color: #008000">*/</span><span style="color: #800000"> .n</span>{<span style="color: #ff0000">font-weight</span>:<span style="color: #0000ff">normal</span>;<span style="color: #ff0000"> font-style</span>:<span style="color: #0000ff">normal</span>;<span style="color: #ff0000"> white-space</span>:<span style="color: #0000ff"> normal</span>;}<span style="color: #800000"> .b</span>{<span style="color: #ff0000">font-weight</span>:<span style="color: #0000ff">bold</span>;}<span style="color: #800000"> .i</span>{<span style="color: #ff0000">font-style</span>:<span style="color: #0000ff">italic</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> text-align </span><span style="color: #008000">*/</span><span style="color: #800000"> .tc</span>{<span style="color: #ff0000">text-align</span>:<span style="color: #0000ff">center</span>;}<span style="color: #800000"> .tr</span>{<span style="color: #ff0000">text-align</span>:<span style="color: #0000ff">right</span>;}<span style="color: #800000"> .tl</span>{<span style="color: #ff0000">text-align</span>:<span style="color: #0000ff">left</span>;}<span style="color: #800000"> .tj</span>{<span style="color: #ff0000">text-align</span>:<span style="color: #0000ff">justify</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> text-decoration </span><span style="color: #008000">*/</span><span style="color: #800000"> .tdl</span>{<span style="color: #ff0000">text-decoration</span>:<span style="color: #0000ff">underline</span>;}<span style="color: #800000"> .tdn,.tdn:hover,.tdn a:hover,a.tdl:hover</span>{<span style="color: #ff0000">text-decoration</span>:<span style="color: #0000ff">none</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> letter-spacing </span><span style="color: #008000">*/</span><span style="color: #800000"> .lt-1</span>{<span style="color: #ff0000">letter-spacing</span>:<span style="color: #0000ff">-1px</span>;}<span style="color: #800000"> .lt0</span>{<span style="color: #ff0000">letter-spacing</span>:<span style="color: #0000ff">0</span>;}<span style="color: #800000"> .lt1</span>{<span style="color: #ff0000">letter-spacing</span>:<span style="color: #0000ff">1px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> white-space </span><span style="color: #008000">*/</span><span style="color: #800000"> .nowrap</span>{<span style="color: #ff0000">white-space</span>:<span style="color: #0000ff">nowrap</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> word-wrap </span><span style="color: #008000">*/</span><span style="color: #800000"> .bk</span>{<span style="color: #ff0000">word-wrap</span>:<span style="color: #0000ff">break-word</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> vertical-align </span><span style="color: #008000">*/</span><span style="color: #800000"> .vm</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">middle</span>;}<span style="color: #800000"> .vtb</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">text-bottom</span>;}<span style="color: #800000"> .vb</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">bottom</span>;}<span style="color: #800000"> .vt</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">top</span>;}<span style="color: #800000"> .v-1</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">-1px</span>;}<span style="color: #800000"> .v-2</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">-2px</span>;}<span style="color: #800000"> .v-3</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">-3px</span>;}<span style="color: #800000"> .v-4</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">-4px</span>;}<span style="color: #800000"> .v-5</span>{<span style="color: #ff0000">vertical-align</span>:<span style="color: #0000ff">-5px</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> float </span><span style="color: #008000">*/</span><span style="color: #800000"> .l</span>{<span style="color: #ff0000">float</span>:<span style="color: #0000ff">left</span>;}<span style="color: #800000"> .r</span>{<span style="color: #ff0000">float</span>:<span style="color: #0000ff">right</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> clear </span><span style="color: #008000">*/</span><span style="color: #800000"> .cl</span>{<span style="color: #ff0000">clear</span>:<span style="color: #0000ff">both</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> position </span><span style="color: #008000">*/</span><span style="color: #800000"> .rel</span>{<span style="color: #ff0000">position</span>:<span style="color: #0000ff">relative</span>;}<span style="color: #800000"> .abs</span>{<span style="color: #ff0000">position</span>:<span style="color: #0000ff">absolute</span>;} <span style="color: #008000">/*</span><span style="color: #008000">z-index</span><span style="color: #008000">*/</span><span style="color: #800000"> .zx1</span>{<span style="color: #ff0000">z-index</span>:<span style="color: #0000ff">1</span>;}<span style="color: #800000"> .zx2</span>{<span style="color: #ff0000">z-index</span>:<span style="color: #0000ff">2</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> cursor </span><span style="color: #008000">*/</span><span style="color: #800000"> .poi</span>{<span style="color: #ff0000">cursor</span>:<span style="color: #0000ff">pointer</span>;}<span style="color: #800000"> .def</span>{<span style="color: #ff0000">cursor</span>:<span style="color: #0000ff">default</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> overflow </span><span style="color: #008000">*/</span><span style="color: #800000"> .ovh</span>{<span style="color: #ff0000">overflow</span>:<span style="color: #0000ff">hidden</span>;}<span style="color: #800000"> .ova</span>{<span style="color: #ff0000">overflow</span>:<span style="color: #0000ff">auto</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> visibility </span><span style="color: #008000">*/</span><span style="color: #800000"> .vh</span>{<span style="color: #ff0000">visibility</span>:<span style="color: #0000ff">hidden</span>;}<span style="color: #800000"> .vv</span>{<span style="color: #ff0000">visibility</span>:<span style="color: #0000ff">visible</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> opacity </span><span style="color: #008000">*/</span><span style="color: #800000"> .opa0</span>{<span style="color: #ff0000">opacity</span>:<span style="color: #0000ff">0</span>;<span style="color: #ff0000"> filer</span>:<span style="color: #0000ff">alpha(opacity=0)</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> zoom </span><span style="color: #008000">*/</span><span style="color: #800000"> .z</span>{<span style="color: #ff0000">*zoom</span>:<span style="color: #0000ff">1</span>;}
首先他这部分代码里面,包含了我们在网页开发中大部分常用的css属性,如display,height,margin,padding,border,color,font-size等。在属性类的命名上,基本上都是采取属性跟属性值的缩写。其他可说明的是:
1)width除了有固定值的属性类定义外,还包含了百分比的属性类定义,毕竟这个在实际工作中也时有用到;
2)margin,border,padding由于包含上下左右相关的属性,所以他还提供了针对上下左右单边的属性类,方便单独使用。
另外他的代码都有浏览器兼容性方面的考虑,所以要是在自己的工作中用的话,最好是参考着他的来。
这种方法在我使用之前,我比较顾虑的是它的可维护性。因为这些属性类很多都包含属性值,比如.f12{font-size: 12px},所以在html元素的class属性值就肯定会包含f12这样的css属性类名,假如要修改的对应的属性值该怎么办呢?那么就需要修改三个地方才行:首先是属性值定义的地方,第二是属性类名定义的地方,最后就是在html中使用的地方。当时想到这个问题的时候,我觉得这种方法不可行,因为在实际工作中,尼玛UI岗位的同事改设计的情况太多了,那样的话,页面上用到这种属性命名类的地方都要经常改来改去。
但是后来我发现,即使不用这种命名方法,我还是改变不了UI调整设计图的情况,而且只要设计图一改,甚至我的html结果以及我那些采用语义化命名的css类都要改,那个麻烦程度其实比用属性命名类的方法更厉害,所以我最后就开始在项目中尝试这个方法。结果发现,其实特别好用,尤其是做些文本类的排版,垂直布局,分栏布局,以及百分比布局等特别简单高效,前面提到的那个维护的问题也很小。我有两个方法来解决来那个问题:
1)假如原先用f12,后来设计把font-size改成14px,那么我只用再增加一个f14即可,再把原先html中需要把f12替换成f14的地方,换成f14即可。如果f12没有别的地方用到了,我会考虑把f12再删掉。
2)假如原先用f54,由于这种属性类并不具备通用性,所以我可能考虑直接把f54替换成我需要的属性类,比如f52。
在本文本部分的最后,我还会给出自己关于这种使用方法的建议以及对维护问题的处理补充。现在先回来再看看张鑫旭的这些代码,我从个人的角度,结合自己在项目中的实际情况,来说下需要我们改善下的地方。
1)有些采用固定值的属性类有多余的,也有缺少的;比如没有.h28表示height: 28px的,.w100到.w700可能都是用不到的
2)跟颜色相关的可能大部分都不一定符合项目需求,尤其是那种做出来完全没有任何规范的设计图,肉眼看上去相同的颜色,实际上却是不一样的颜色;相同的版本,在不同的页面,有可能也使用了不同的颜色;甚至是那种色系比较丰富的设计。这些属性类包括border,background,color。当然纯黑色和纯白色还是可以统一起来的,毕竟这两个颜色基本上各个网站都会用到。
3)还有些属性类也是多余的,比如font-family,因为我在项目中有用less,font-family是在别的地方定义的,所以这里就不需要了。当然你要是觉得这个还是有用得着的话,可以考虑留下。
4)还可以考虑增加些其它的css属性类,比如border-radius以及flex等。border-radius现在用的已经很普遍了,flex在移动端也有可以用的到的地方。这个就得根据自己的项目实际情况,慢慢增加了。
综合以上这些内容,我对于这种面向属性命名的方法建议如下:
1)首先肯定是得以张鑫旭的这套代码为基础;
2)对于height,line-height,font-size这三个属性,划分属性类的时候,尽量以步长为2的等差分布来定义,如:
.h0 , .h18 , .h20 , .h22 , .h24 , .h26 , .h28 , .h30 , .h32 , .h34 , .h36 , .h38 , .h40;
.lh18 , .lh20 , .lh22 , .lh24 , .lh26 , .lh28 , .lh30 , .lh32 , .lh34 , .lh36 , .lh38 , .lh40 ;
.f12 , .f14 , .f16 , .f18 , .f20 , .f22 , .f24 , .f26 , .f28 , .f30 ;
一来是为了保证这些小范围的的尺寸都能覆盖到;二是为了保证各个尺寸都是偶数,方便布局。
3)对于width,100以内的固定值,可以考虑以10为步长定义一个等差分布序列:
.w0 , .w10 , .w20 , .w30 , .w40 , .w50 , .w60 , .w70 , .w80 , .w90 , .w100
其它的固定值的属性类可以等到实际用到的时候再追加;
对于百分比的width,可以把10%到100%的值都定义出来,然后针对1/3 , 1/4 , 1/5, 1/6 也单独定义出来,因为这些都属于常用的一些布局比例,所以可以考虑提前定义。
4)margin跟padding的属性类有的可能是多余的,也有可能有缺少的,但是不能提前定义太多,可以考虑在实际用的过程中再追加;
5)border,background-color以及color可以考虑留下。由于这几个跟颜色有关,所以可以把设计图中最常用的几种颜色也抽出来分别定义追加进去,假如设计师比较有经验的话,做出来的东西就会比较规范,尤其是在通用的颜色这一块,不会搞出很多的颜色出来。在zxx.lib.css中,已定义的border,background-color还有color都是安全色,如果是设计师的颜色,其实也能定义成属性类,比如#7c7c7c,就可以定义成.c7c,bg7c,bd7c。
6)可以根据项目的实际情况增加border-radius以及flex等属性类。定义方式完全跟其它属性一样,如flex的:
<span style="color: #800000">.df </span>{<span style="color: #ff0000"> display</span>:<span style="color: #0000ff"> flex</span>; }<span style="color: #800000"> .dif </span>{<span style="color: #ff0000"> display</span>:<span style="color: #0000ff"> inline-flex</span>; }<span style="color: #800000"> .fdr </span>{<span style="color: #ff0000"> flex-direction</span>:<span style="color: #0000ff"> row</span>; }<span style="color: #800000"> .fdrr </span>{<span style="color: #ff0000"> flex-direction</span>:<span style="color: #0000ff"> row-reverse</span>; }<span style="color: #800000"> .fdc </span>{<span style="color: #ff0000"> flex-direction</span>:<span style="color: #0000ff"> column</span>; }<span style="color: #800000"> .fdcr </span>{<span style="color: #ff0000"> flex-direction</span>:<span style="color: #0000ff"> column-reverse</span>; }<span style="color: #800000"> .fwn </span>{<span style="color: #ff0000"> flex-wrap</span>:<span style="color: #0000ff"> nowrap</span>; }<span style="color: #800000"> .fww </span>{<span style="color: #ff0000"> flex-wrap</span>:<span style="color: #0000ff"> wrap</span>; }<span style="color: #800000"> .fwr </span>{<span style="color: #ff0000"> flex-wrap</span>:<span style="color: #0000ff"> wrap-reverse</span>; }<span style="color: #800000"> .jcfs </span>{<span style="color: #ff0000"> justify-content</span>:<span style="color: #0000ff"> flex-start</span>; }<span style="color: #800000"> .jcfe </span>{<span style="color: #ff0000"> justify-content</span>:<span style="color: #0000ff"> flex-end</span>; }<span style="color: #800000"> .jcc </span>{<span style="color: #ff0000"> justify-content</span>:<span style="color: #0000ff"> center</span>; }<span style="color: #800000"> .jcsb </span>{<span style="color: #ff0000"> justify-content</span>:<span style="color: #0000ff"> space-between</span>; }<span style="color: #800000"> .jcsa </span>{<span style="color: #ff0000"> justify-content</span>:<span style="color: #0000ff"> space-around</span>; }<span style="color: #800000"> .aifs </span>{<span style="color: #ff0000"> align-items</span>:<span style="color: #0000ff"> flex-start</span>; }<span style="color: #800000"> .aife </span>{<span style="color: #ff0000"> align-items</span>:<span style="color: #0000ff"> flex-end</span>; }<span style="color: #800000"> .aic </span>{<span style="color: #ff0000"> align-items</span>:<span style="color: #0000ff"> center</span>; }<span style="color: #800000"> .aib </span>{<span style="color: #ff0000"> align-items</span>:<span style="color: #0000ff"> baseline</span>; }<span style="color: #800000"> .ais </span>{<span style="color: #ff0000"> align-items</span>:<span style="color: #0000ff"> stretch</span>; }<span style="color: #800000"> .acfs </span>{<span style="color: #ff0000"> align-content</span>:<span style="color: #0000ff"> flex-start</span>; }<span style="color: #800000"> .acfe </span>{<span style="color: #ff0000"> align-content</span>:<span style="color: #0000ff"> flex-end</span>; }<span style="color: #800000"> .acc </span>{<span style="color: #ff0000"> align-content</span>:<span style="color: #0000ff"> center</span>; }<span style="color: #800000"> .acsb </span>{<span style="color: #ff0000"> align-content</span>:<span style="color: #0000ff"> space-between</span>; }<span style="color: #800000"> .acsa </span>{<span style="color: #ff0000"> align-content</span>:<span style="color: #0000ff"> space-around</span>; }<span style="color: #800000"> .acs </span>{<span style="color: #ff0000"> align-content</span>:<span style="color: #0000ff"> stretch</span>; }<span style="color: #800000"> .fxa </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> auto</span>; }<span style="color: #800000"> .fxn </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> none</span>; }<span style="color: #800000"> .fx1 </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> 1</span>; }<span style="color: #800000"> .fx2 </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> 2</span>; }<span style="color: #800000"> .fx3 </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> 3</span>; }<span style="color: #800000"> .fx4 </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> 4</span>; }<span style="color: #800000"> .fx5 </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> 5</span>; }<span style="color: #800000"> .fx6 </span>{<span style="color: #ff0000"> flex</span>:<span style="color: #0000ff"> 6</span>; }
以上这部分flex的属性类需结合auto-prefix这种工具来使用,因为有兼容性问题,需要统一添加前缀。
7)关于这些面向属性的css类组织:
第一,按照前面的这些建议,在张鑫旭的代码的基础上,调整成自己的项目所需之后,就应该把这个代码单独存放起来,作为一个像bootstrap这样的单独的库来使用;
第二,在实际工作过程中,如果要增加新的css属性类,只有在这个属性类有公用的价值,才能添加到第一步的公共属性类库里面去,否则的话,就只能在当前页面的main css里面去写。
8)最后,就是在坚持没有重复的代码前提下,根据实际情况去追加相关的css属性类。有公用性的就加到单独的库里,没公用性的话就追加到页面的main css里。
这些建议,也只是我个人的使用经验总结,有比较多的个人想法,如果觉得不对或者有疑问的话,欢迎私信或者评论交流。本文最重要的目的是分享这种面向属性的命名方法,我相信肯定会有朋友能够看到这个方法的价值的。
最后在张鑫旭的代码中,还有一部分代码,定义了很多简单常用的css类,比如浮动,浮动清除等等:
<span style="color: #008000">/*</span><span style="color: #008000"> 块状元素水平居中 </span><span style="color: #008000">*/</span><span style="color: #800000"> .auto</span>{<span style="color: #ff0000">margin-left</span>:<span style="color: #0000ff">auto</span>;<span style="color: #ff0000"> margin-right</span>:<span style="color: #0000ff">auto</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 清除浮动</span><span style="color: #008000">*/</span><span style="color: #800000"> .fix</span>{<span style="color: #ff0000">*zoom</span>:<span style="color: #0000ff">1</span>;}<span style="color: #800000"> .fix:after</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">table</span>;<span style="color: #ff0000"> content</span>:<span style="color: #0000ff">''</span>;<span style="color: #ff0000"> clear</span>:<span style="color: #0000ff">both</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 基于display:table-cell的自适应布局 </span><span style="color: #008000">*/</span><span style="color: #800000"> .cell</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">table-cell</span>;<span style="color: #ff0000"> *display</span>:<span style="color: #0000ff">inline-block</span>;<span style="color: #ff0000"> width</span>:<span style="color: #0000ff">2000px</span>;<span style="color: #ff0000"> *width</span>:<span style="color: #0000ff">auto</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 双栏自适应cell部分连续英文字符换行 </span><span style="color: #008000">*/</span><span style="color: #800000"> .cell2</span>{<span style="color: #ff0000">overflow</span>:<span style="color: #0000ff">hidden</span>;<span style="color: #ff0000"> _display</span>:<span style="color: #0000ff">inline-block</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 单行文字溢出虚点显 示</span><span style="color: #008000">*/</span><span style="color: #800000"> .ell</span>{<span style="color: #ff0000">text-overflow</span>:<span style="color: #0000ff">ellipsis</span>;<span style="color: #ff0000"> white-space</span>:<span style="color: #0000ff">nowrap</span>;<span style="color: #ff0000"> overflow</span>:<span style="color: #0000ff">hidden</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> css3过渡动画效果 </span><span style="color: #008000">*/</span><span style="color: #800000"> .trans</span>{<span style="color: #ff0000"> -webkit-transition</span>:<span style="color: #0000ff">all .15s</span>;<span style="color: #ff0000"> transition</span>:<span style="color: #0000ff">all .15s</span>; } <span style="color: #008000">/*</span><span style="color: #008000"> 大小不定元素垂直居中 </span><span style="color: #008000">*/</span><span style="color: #800000"> .dib_vm</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">inline-block</span>;<span style="color: #ff0000"> width</span>:<span style="color: #0000ff">0</span>;<span style="color: #ff0000"> height</span>:<span style="color: #0000ff">100%</span>;<span style="color: #ff0000"> vertical-align</span>:<span style="color: #0000ff">middle</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 加载中背景图片 - 如果您使用该CSS小库,务必修改此图片地址 </span><span style="color: #008000">*/</span><span style="color: #800000"> .loading</span>{<span style="color: #ff0000">background</span>:<span style="color: #0000ff">url(about:blank) no-repeat center</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 无框文本框文本域 </span><span style="color: #008000">*/</span><span style="color: #800000"> .bd_none</span>{<span style="color: #ff0000">border</span>:<span style="color: #0000ff">0</span>;<span style="color: #ff0000"> outline</span>:<span style="color: #0000ff">none</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 绝对定位隐藏 </span><span style="color: #008000">*/</span><span style="color: #800000"> .abs_out</span>{<span style="color: #ff0000">position</span>:<span style="color: #0000ff">absolute</span>;<span style="color: #ff0000"> left</span>:<span style="color: #0000ff">-999em</span>;<span style="color: #ff0000"> top</span>:<span style="color: #0000ff">-999em</span>;}<span style="color: #800000"> .abs_clip</span>{<span style="color: #ff0000">position</span>:<span style="color: #0000ff">absolute</span>;<span style="color: #ff0000"> clip</span>:<span style="color: #0000ff">rect(0 0 0 0)</span>;} <span style="color: #008000">/*</span><span style="color: #008000"> 按钮禁用 </span><span style="color: #008000">*/</span><span style="color: #800000"> .disabled</span>{<span style="color: #ff0000">outline</span>:<span style="color: #0000ff">0 none</span>;<span style="color: #ff0000"> cursor</span>:<span style="color: #0000ff">default!important</span>;<span style="color: #ff0000"> opacity</span>:<span style="color: #0000ff">.4</span>;<span style="color: #ff0000"> filer</span>:<span style="color: #0000ff">alpha(opacity=40)</span>;<span style="color: #ff0000"> -ms-pointer-events</span>:<span style="color: #0000ff">none</span>;<span style="color: #ff0000"> pointer-events</span>:<span style="color: #0000ff">none</span>;} <span style="color: #008000">/*</span><span style="color: #008000">inline-block与float等宽列表</span><span style="color: #008000">*/</span><span style="color: #800000"> .inline_box</span>{<span style="color: #ff0000">font-size</span>:<span style="color: #0000ff">1em</span>;<span style="color: #ff0000"> letter-spacing</span>:<span style="color: #0000ff">-.25em</span>;<span style="color: #ff0000"> font-family</span>:<span style="color: #0000ff">Arial</span>;}<span style="color: #800000"> .inline_two, .inline_three, .inline_four, .inline_five, .inline_six, .inline_any</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">inline-block</span>;<span style="color: #ff0000"> *display</span>:<span style="color: #0000ff">inline</span>;<span style="color: #ff0000"> letter-spacing</span>:<span style="color: #0000ff">0</span>;<span style="color: #ff0000"> vertical-align</span>:<span style="color: #0000ff">top</span>;<span style="color: #ff0000"> *zoom</span>:<span style="color: #0000ff">1</span>;}<span style="color: #800000"> .float_two, .float_three, .float_four, .float_five, .float_six</span>{<span style="color: #ff0000">float</span>:<span style="color: #0000ff">left</span>;}<span style="color: #800000"> .inline_two, .float_two</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">50%</span>;<span style="color: #ff0000"> *width</span>:<span style="color: #0000ff">49.9%</span>;}<span style="color: #800000"> .inline_three, .float_three</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">33.33333%</span>;<span style="color: #ff0000"> *width</span>:<span style="color: #0000ff">33.3%</span>;}<span style="color: #800000"> .inline_four, .float_four</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">25%</span>;<span style="color: #ff0000"> *width</span>:<span style="color: #0000ff">24.9%</span>;}<span style="color: #800000"> .inline_five, .float_five</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">20%</span>;<span style="color: #ff0000"> *width</span>:<span style="color: #0000ff">19.9%</span>;}<span style="color: #800000"> .inline_six, .float_six</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">16.66666%</span>;<span style="color: #ff0000"> *width</span>:<span style="color: #0000ff">16.6%</span>;}<span style="color: #800000"> .inline_fix</span>{<span style="color: #ff0000">display</span>:<span style="color: #0000ff">inline-block</span>;<span style="color: #ff0000"> width</span>:<span style="color: #0000ff">100%</span>;<span style="color: #ff0000"> height</span>:<span style="color: #0000ff">0</span>;<span style="color: #ff0000"> overflow</span>:<span style="color: #0000ff">hidden</span>;}
这些代码也可以根据实际情况稍微调整,大部分在实际工作中都有使用到的场景,这里仅作引用介绍,因为使用起来也很简单的。
为了说明这个命名方法的作用,我做了一个demo,以博客园博客列表页来说明如何应用这种命名方法。先来看博客列表页的结构:
可以看到这个列表页其实是用到了很多语义化的命名的css类的,假如要用面向属性的命名方法来定义,就会变成下面这个样子:
实际效果如下:
相关源码可访问以下链接查看:
http://liuyunzhuge.github.io/blog/css_name/html/demo.html
在以上这个实践过程中,并没有所有的样式都使用这种命名方法,主要的原因是单纯的属性命名对无法对伪类或者伪元素进行很好的样式控制,而正好博客园列表页中的每个链接的样式都不一样,所以没办法统一。这也正是面向属性的命名方法的一种限制情形。
虽然本文的目的是在推崇这种面向属性的命名方法,但是你从文中的内容也可以看到,这种方法是有针对性的,前面一开始介绍的时候就说过:
采用面向属性的css命名,把那些我们实在想不出名字的,而且没有必要起名字的,并且可以只用单一的css属性或者组合的单一css属性来描述的部分,通通都用面向属性的css类来控制样式
使用这个方法的最大前提就是没有必要起名字,并且能够用组合的单一属性来描述的内容,就可以这种方法来加速页面布局的工作。像前面应用实践介绍过的那种情形一样,当你需要配合伪类,伪元素或者背景图片的时候,就不太适合用这种方法。
在面向属性的命名方法不能使用的时候,有另外两种css的命名或者说组织方式可以使用:
1)语义化的命名,在整个页面,语义化的css命名还是不可获缺的一部分,尤其是那些划分页面模块的,比如.header .footer .logo等等,抽象公共样式或者公共组件的,如.dropdown,.btn,.tab等等。这些是css模块化,代码重用的比较大的组织单位,如果把它们也拆分了,会使得整个站点的css结构非常的复杂,那样的话还不如直接用style呢;
2)采用层级来命名,而且要多用直接子元素选择器,虽然在张鑫旭的博客中不建议css有层级,但是有的时候如果不想命名,又无法用面向属性的命名方法来解决的设计,可以考虑用层级来解决,在bootstrap的源码中很多的css组件,比如nav,dropdown,tab等等,都是通过层级来控制的,一来是起到命名空间的作用,二来是减少对层数较深的子孙元素产生影响。但是层数也不能太深,最好不要超过3层,否则html结构变化之后,就会影响css代码的结构。
本文主要是传播面向属性的css命名方法这种思想,由于它在我实际工作中帮助我减少了很多不必要的css命名,所以我专门写了这篇文章把它分享出来。这个里面也包含了很多自己在工作中产生的想法,不一定符合你自己的实际需求,要是感兴趣的话,可以去研究下张鑫旭的那2篇文章,相信你自己也能够总结出一些属于自己的东西。感谢阅读:)