如果你的项目中需要写的css特别多,那么你可以尝试一下使用less去管理你的css。
less是一种动态样式语言,既能跑在浏览器中也能跑在服务端(与node)。
less的官网:http://lesscss.org/
下面介绍下less特性:
1、你可以在最前面定义好你的全局样式,下面可以重用
@color: #4D926F;
#header {
color: @color;
}
h2 {
color: @color;
}
|
#header {
color: #4D926F;
}
h2 {
color: #4D926F;
}
|
2、你可以像函数一样调用一个写好的class(但必须是整个的引入),并且可以传参,太强大了
.rounded-corners (@radius: 5px) {
border-radius: @radius;
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
}
#header {
.rounded-corners;
}
#footer {
.rounded-corners(10px);
}
|
#header {
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
#footer {
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
}
|
3、嵌套规则让你更容易去写模块css
#header {
h1 {
font-size: 26px;
font-weight: bold;
}
p { font-size: 12px;
a { text-decoration: none;
&:hover { border-width: 1px }
}
}
}
|
#header h1 {
font-size: 26px;
font-weight: bold;
}
#header p {
font-size: 12px;
}
#header p a {
text-decoration: none;
}
#header p a:hover {
border-width: 1px;
}
|
4、我们居然可以用less像javascript那样计算
@the-border: 1px;
@base-color: #111;
@red: #842210;
#header {
color: @base-color * 3;
border-left: @the-border;
border-right: @the-border * 2;
}
#footer {
color: @base-color + #003300;
border-color: desaturate(@red, 10%);
}
|
#header {
color: #333;
border-left: 1px;
border-right: 2px;
}
#footer {
color: #114411;
border-color: #7d2717;
}
|
客户端如何使用:
首先引入你自己写的样式,less写的样式文件以less后缀结尾
<link rel= "stylesheet/less" type= "text/css" href= "styles.less" >
|
再引入less.js(一定要先引入你自定义的less样式,再引入less.js)
<script src= "less.js" type= "text/javascript" ></script>
|
服务端使用方法:
最简单的方式是通过npm安装
更多请移步less官网吧:http://lesscss.org/
欢迎关注下方“非著名资深码农“公众号进行交流~