ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

编写更好的CSS(2/3)

来源:网络整理     时间:2015-12-16     关键词:box-shadow,选择符,html代码

本篇文章主要介绍了"编写更好的CSS",主要涉及到box-shadow,选择符,html代码方面的内容,对于HTMLjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 编写好的CSS代码能提升页面的渲染速度。本质上,一条规则都没有引擎解析的最快。MDN上将CSS选择符归拆分成四个主要类别,如下所示,性能依次降低。ID 规则Cl...

// 糟糕
.someclass {
 padding-top: 20px;
 padding-bottom: 20px;
 padding-left: 10px;
 padding-right: 10px;
 background: #000;
 background-image: url(../imgs/carrot.png);
 background-position: bottom;
 background-repeat: repeat-x;
}

// 好的
.someclass {
 padding: 20px 10px 20px 10px;
 background: #000 url(../imgs/carrot.png) repeat-x bottom;
}

编写更好的CSS

避免不必要的命名空间

// 糟糕
.someclass table tr.otherclass td.somerule {..}

//好的
.someclass .otherclass td.somerule {..}

避免不必要的重复

尽可能组合重复的规则。

编写更好的CSS

// 糟糕
.someclass {
 color: red;
 background: blue;
 font-size: 15px;
}

.otherclass {
 color: red;
 background: blue;
 font-size: 15px;
}

// 好的
.someclass, .otherclass {
 color: red;
 background: blue;
 font-size: 15px;
}

编写更好的CSS

尽可能精简规则

在上面规则的基础上,你可以进一步合并不同类里的重复的规则。

编写更好的CSS

// 糟糕
.someclass {
 color: red;
 background: blue;
 height: 150px;
 width: 150px;
 font-size: 16px;
}

.otherclass {
 color: red;
 background: blue;
 height: 150px;
 width: 150px;
 font-size: 8px;
}

// 好的
.someclass, .otherclass {
 color: red;
 background: blue;
 height: 150px;
 width: 150px;
}

.someclass {
 font-size: 16px;
}

.otherclass {
 font-size: 8px;
}

编写更好的CSS

避免不明确的命名约定

最好使用表示语义的名字。一个好的CSS类名应描述它是什么而不是它像什么。

避免 !importants

其实你应该也可以使用其他优质的选择器。

遵循一个标准的声明顺序

虽然有一些排列CSS属性顺序常见的方式,下面是我遵循的一种流行方式。

编写更好的CSS

.someclass {
 /* Positioning */
 /* Display & Box Model */
 /* Background and typography styles */
 /* Transitions */
 /* Other */
}

编写更好的CSS

组织好的代码格式

代码的易读性和易维护性成正比。下面是我遵循的格式化方法。

相关图片

相关文章