​Less一种动态样式语言,LESS将CSS赋予了动态语言的特性,如变量,继承,运算,函数...LESS 既可以在客户端上运行 (支持IE 6+, Webkit, Firefox),也可以借助Node.js或者Rhino在服务端运行。

安装与使用:

//客户端引用
<link rel="stylesheet/less" type="text/css" href="styles.less">
<script src="less.js" type="text/javascript"></script>
//node服务端使用
npm install less var less = require('less');//或者import less from 'less'
less.render('.class { width: 1 + 1 }', function (e, css) {
console.log(css);
});
//输出
.class {
width: ;
}

客户端不建议直接引用less.js,可通过编译为css引入,推荐编译工具koala。

变量

定义变量用@

//less
@ly_width:100px;
.box {
width:@ly_width;
}
/*编译css*/
.box {
width:100px;
}

混合

混合可以将一个定义好的class A轻松的引入到另一个class B中,从而简单实现class B继承class A中的所有属性。我们还可以带参数地调用,就像使用函数一样。

// LESS
.rounded-corners (@radius: 5px) {
border-radius: @radius;
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
}
#header {
.rounded-corners;
}
#footer {
.rounded-corners(10px);
}
/*生成的 CSS */
#header {
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
#footer {
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
}

嵌套规则

实现样式的继承关系,代码看起来层次分明,提高代码可维护性

// LESS
#header {
p { font-size: 12px;
a { text-decoration: none;
&:hover { border-width: 1px }
}
}
}
* 生成的 CSS */
#header p {
font-size: 12px;
}
#header p a {
text-decoration: none;
}
#header p a:hover {
border-width: 1px;
}

函数 & 运算

运算提供了加,减,乘,除操作,还可以做属性值和颜色的运算...

//less
@ly_width:100px;
.one {
width:@ly_widht + ;
}
/*生成css*/
.one {
idth:200px;
}

match函数:

round(1.67); //returns `2`
eil(2.4); //returns `3`
floor(2.6); //returns `2`)

Color函数:

lighten(@color, %);     // return a color which is 10% *lighter* than @color
darken(@color, %); // return a color which is 10% *darker* than @color
saturate(@color, %); // return a color 10% *more* saturated than @color
desaturate(@color, %); // return a color 10% *less* saturated than @color
fadein(@color, %); // return a color 10% *less* transparent than @color
fadeout(@color, %); // return a color 10% *more* transparent than @color
fade(@color, %); // return @color with 50% transparency
spin(@color, ); // return a color with a 10 degree larger in hue than @color
spin(@color, -); // return a color with a 10 degree smaller hue than @color
mix(@color1, @color2); // return a mix of @color1 and @color2

命名空间

有时候,你可能为了更好组织CSS或者单纯是为了更好的封装,将一些变量或者混合模块打包起来, 你可以像下面这样在#bundle中定义一些属性集之后可以重复使用

#bundle {
.button () {
display: block;
border: 1px solid black;
background-color: grey;
&:hover { background-color: white }
}
.tab { ... }
.citation { ... }
}
//你只需要在 #header a中像这样引入 .button:
#header a {
color: orange;
#bundle > .button;
}

作用域

LESS 中的作用域跟其他编程语言非常类似,首先会从本地查找变量或者混合模块,如果没找到的话会去父级作用域中查找,直到找到为止.

文件引用

一个less文件头部引入另一个less文件

@import "lib.less";
@import "lib";

注释

两种注释方式:

//这是注释哦...

/*这是注释*/

LESS 原理,一款css的预处理程序Less的使用的更多相关文章

  1. 给 Web 前端开发人员推荐20款 CSS 编辑器

    CSS 和 HTML,JavaScript 是网页的基础,作为前端开发人员,对这三者都要很熟悉.特别是未来流行全栈开发的时代,每项技术都是你知识结构中必要的一个节点. 在开发中,选择好工具是非常重要的 ...

  2. 强烈推荐一款CSS导航菜单

    强烈推荐一款CSS导航菜单,用到政府学校类网站上超级不错,有点类似站长网菜单的味道,只不过颜色不一样而已,这种菜单还不是真正意义上的“下拉”菜单,应该叫滑出菜单吧?反正比较不错,不多说了. <! ...

  3. 分享一款CSS框架

    1.http://bulma.io/documentation/elements 2.支持IE9+ 3.内容:

  4. 25款css动画库

    http://www.swiper.com.cn/usage/animate/index.html   //swiper https://cssanimation.io/ http://ianlunn ...

  5. 2014 年 20 款最好的 CSS 工具

    说到 WEB 设计,不得不介绍介绍 CSS 工具,CSS 工具在这里面扮演很重要的角色,可以简化无数开发者和设计师的工作,写出更好的 CSS 代码. 而网上有非常多的 CSS 工具,帮助设计师和开发者 ...

  6. 值得拥有!精心推荐几款超实用的 CSS 开发工具

    当你开发一个网站或 Web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文章中,我为大家收集了超有用的 CSS 开发工具. 对于 Web 开发人员来说,找到有用的 CSS 开发工 ...

  7. 11 款最好 CSS 框架 让你的网站独领风骚

    网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒的设计.网页编码一定要合适.精确,才能保证不发生 ...

  8. 11 款最好 CSS 框架

    11 款最好 CSS 框架 让你的网站独领风骚 网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒 ...

  9. css 10 款非常棒的CSS代码格式化工具推荐

    http://www.iteye.com/news/23692/  10 款非常棒的CSS代码格式化工具推荐 2011-12-14 09:31 by 副主编 wangguo 评论(0) 有9111人浏 ...

随机推荐

  1. java实现第六届蓝桥杯机器人数目

    机器人数目 少年宫新近邮购了小机器人配件,共有3类,其中, A类含有:8个轮子,1个传感器 B类含有: 6个轮子,3个传感器 C类含有:4个轮子,4个传感器 他们一共订购了100套机器人,收到了轮子6 ...

  2. 【CSS】电脑、移动端公用样式

    电脑端: /* Public */ @charset "utf-8"; html, body, div, p, ul, ol, li, dl, dt, dd, h1, h2, h3 ...

  3. SpringSceurity(3)---图形验证码功能实现

    SpringSceurity(3)---图形验证码功能实现 有关springSceurity之前有写过两篇文章: 1.SpringSecurity(1)---认证+授权代码实现 2.SpringSec ...

  4. 河青的持久层框架hqbatis

    谈到对数据库的操作,powerbuilder 的嵌入式SQL还是最方便的,增.删.改.查都无比的方便,可惜它落败于BS架构的盛起.java 以mvc的框架,实现对数据库的操作,写起来是相当麻烦,jav ...

  5. react中使用decorator 封装context

    2020-03-27 react中使用decorator 封装context 在传统的react context中,子组件使用context十分繁琐,如果需要使用context的子组件多的话 每个组件 ...

  6. Python使用wxpy模块实现微信两两群组消息同步

    python使用wxpy模块提供的微信API接口实现两两群组的消息同步 安装模块: pip install wxpy 注意:需要同步的微信群需要保存到通讯录中 以下是自己闲来无事写的代码,暂时还存在以 ...

  7. Source Insight 中的 Auto Indenting

    编码过程中,希望输入花括号时能自动对齐,Source Insigth 应如何设置? 先来看一下Source Insight 中的帮助. “ Auto Indenting The auto-indent ...

  8. BrainF**k的编译器

      按照语法编写了这个"编译器"(对于解释性语言而言"编译"二字的确很奇怪).   功能:     1."编译".运行一个后缀为 '.bf' ...

  9. fiddler下载安装

    1.官网https://www.telerik.com/fiddler 2.按照要求填写 3.傻瓜式安装 4.安装https证书

  10. c++. Run-Time Check Failure #2 - Stack around the variable 'cc' was corrupted.

    Run-Time Check Failure #2 - Stack around the variable 'cc' was corrupted. char cc[1024];   //此处如果索引值 ...