less使用方法总结
1 变量
我们常常在 CSS 中 看到同一个值重复多次,这样难易于代码维护。 理想状态,应是下面这样:
- const bgColor="skyblue";
- $(".post-content").css("background-color",bgColor);
- $("#wrap").css("background-color",bgColor);
- $(".arctive").css("background-color",bgColor);
- 只要我们修改 bgColor这一个变量, 整个页面的背景颜色都会随之改变。
- 而 Less 中的变量十分强大,可化万物,值得一提的是,其变量是常量 ,所以只能定义一次,不能重复使用。
1.1 值变量
- /* Less */
- @color: #;
- @bgColor: skyblue;//不要添加引号
- @width: %;
- #wrap {
- color: @color;
- width: @width;
- }
- /* 生成后的 CSS */
- #wrap {
- color: #;
- width: %;
- }
以 @ 开头 定义变量,并且使用时 直接 键入 @名称。
在平时工作中,我们就可以把 常用的变量 封装到一个文件中,这样利于代码组织维护。
- @lightPrimaryColor: #c5cae9;
- @textPrimaryColor: #fff;
- @accentColor: rgb(, , );
- @primaryTextColor: #;
- @secondaryTextColor: #;
- @dividerColor: #b6b6b6;
- @borderColor: #dadada;
1.2 选择器变量
让 选择器 变成 动态
- /* Less */
- @mySelector: #wrap;
- @Wrap: wrap;
- @{mySelector}{ //变量名 必须使用大括号包裹
- color: #;
- width: %;
- }
- .@{Wrap}{
- color:#ccc;
- }
- #@{Wrap}{
- color:#;
- }
- /* 生成的 CSS */
- #wrap{
- color: #;
- width: %;
- }
- .wrap{
- color:#ccc;
- }
- #wrap{
- color:#;
- }
1.3 属性变量
可减少代码书写量
- /* Less */
- @borderStyle: border-style;
- @Soild:solid;
- #wrap{
- @{borderStyle}: @Soild;//变量名 必须使用大括号包裹
- }
- /* 生成的 CSS */
- #wrap{
- border-style:solid;
- }
1.4 url 变量
项目结构改变时,修改其变量即可。
- /* Less */
- @images: "../img";//需要加引号
- body {
- background: url("@{images}/dog.png");//变量名 必须使用大括号包裹
- }
- /* 生成的 CSS */
- body {
- background: url("../img/dog.png");
- }
1.5 声明变量
有点类似于 下面的 混合方法
- - 结构: @name: { 属性: 值 ;};
- - 使用:@name();
- /* Less */
- @background: {background:red;};
- #main{
- @background();
- }
- @Rules:{
- width: 200px;
- height: 200px;
- border: solid 1px red;
- };
- #con{
- @Rules();
- }
- /* 生成的 CSS */
- #main{
- background:red;
- }
- #con{
- width: 200px;
- height: 200px;
- border: solid 1px red;
- }
1.6 变量运算
不得不提的是,Less 的变量运算完全超出我的期望,十分强大。
- 加减法时 以第一个数据的单位为基准
- 乘除法时 注意单位一定要统一
- /* Less */
- @width:300px;
- @color:#;
- #wrap{
- width:@width-;
- height:@width-*;
- margin:(@width-)*;
- color:@color*;
- background-color:@color + #;
- }
- /* 生成的 CSS */
- #wrap{
- width:280px;
- height:200px;
- margin:1400px;
- color:#;
- background-color:#;
- }
1.7 变量作用域
理解:就近原则包。
- /* Less */
- @var: @a;
- @a: %;
- #wrap {
- width: @var;
- @a: %;
- }
- /* 生成的 CSS */
- #wrap {
- width: %;
- }
1.8 用变量去定义变量
- /* Less */
- @fnord: "I am fnord.";
- @var: "fnord";
- #wrap::after{
- content: @@var; //将@var替换为其值 content:@fnord;
- }
- /* 生成的 CSS */
- #wrap::after{
- content: "I am fnord.";
- }
2 嵌套
2.1 & 的妙用
& :代表的上一层选择器的名字,此例便是header。
- /* Less */
- #header{
- &:after{
- content:"Less is more!";
- }
- .title{
- font-weight:bold;
- }
- &_content{//理解方式:直接把 & 替换成 #header
- margin:20px;
- }
- }
- /* 生成的 CSS */
- #header::after{
- content:"Less is more!";
- }
- #header .title{ //嵌套了
- font-weight:bold;
- }
- #header_content{//没有嵌套!
- margin:20px;
- }
2.2 媒体查询
在以往的工作中,我们使用 媒体查询,都要把一个元素 分开写
- #wrap{
- width:500px;
- }
- @media screen and (max-width:768px){
- #wrap{
- width:100px;
- }
- }
Less 提供了一个十分便捷的方式
- /* Less */
- #main{
- //something...
- @media screen{
- @media (max-width:768px){
- width:100px;
- }
- }
- @media tv {
- width:2000px;
- }
- }
- /* 生成的 CSS */
- @media screen and (maxwidth:768px){
- #main{
- width:100px;
- }
- }
- @media tv{
- #main{
- width:2000px;
- }
- }
唯一的缺点就是 每一个元素都会编译出自己 @media 声明,并不会合并。
2.3 实战技巧
可以借助 Less 在元素中,去定义自己的私有样式。
- /* Less */
- #main{
- // something..
- &.show{
- display:block;
- }
- }
- .show{
- display:none;
- }
- const main = document.getElementById("main");
- main.classList.add("show");
结果:
- #main.show{
- display:block;
- }
- .show{
- display:none; //会被覆盖。
- }
3 混合方法
3.1 无参数方法
方法犹如 声明的集合,使用时 直接键入名称即可。
- /* Less */
- .card { // 等价于 .card()
- background: #f6f6f6;
- -webkit-box-shadow: 1px 2px rgba(, , , .);
- box-shadow: 1px 2px rgba(, , , .);
- }
- #wrap{
- .card;//等价于.card();
- }
- /* 生成的 CSS */
- #wrap{
- background: #f6f6f6;
- -webkit-box-shadow: 1px 2px rgba(, , , .);
- box-shadow: 1px 2px rgba(, , , .);
- }
其中 .card 与 .card() 是等价的。
个人建议,为了避免 代码混淆,应写成 :
- .card(){
- //something...
- }
- #wrap{
- .card();
- }
要点:
`.` 与 `#` 皆可作为 方法前缀。
方法后写不写 `()` 看个人习惯。
3.2 默认参数方法
Less 可以使用默认参数,如果 没有传参数,那么将使用默认参数。
@arguments 犹如 JS 中的 arguments 指代的是 全部参数。
传的参数中 必须带着单位。
- /* Less */
- .border(@a:10px,@b:50px,@c:30px,@color:#){
- border:solid 1px @color;
- box-shadow: @arguments;//指代的是 全部参数
- }
- #main{
- .border(0px,5px,30px,red);//必须带着单位
- }
- #wrap{
- .border(0px);
- }
- #content{
- .border;//等价于 .border()
- }
- /* 生成的 CSS */
- #main{
- border:solid 1px red;
- box-shadow:0px,5px,30px,red;
- }
- #wrap{
- border:solid 1px #;
- box-shadow: 0px 50px 30px #;
- }
- #content{
- border:solid 1px #;
- box-shadow: 10px 50px 30px #;
- }
3.3 方法的匹配模式
与 面向对象中的多态 很相似
- /* Less */
- .triangle(top,@width:20px,@color:#){
- border-color:transparent transparent @color transparent ;
- }
- .triangle(right,@width:20px,@color:#){
- border-color:transparent @color transparent transparent ;
- }
- .triangle(bottom,@width:20px,@color:#){
- border-color:@color transparent transparent transparent ;
- }
- .triangle(left,@width:20px,@color:#){
- border-color:transparent transparent transparent @color;
- }
- .triangle(@_,@width:20px,@color:#){
- border-style: solid;
- border-width: @width;
- }
- #main{
- .triangle(left, 50px, #)
- }
- /* 生成的 CSS */
- #main{
- border-color:transparent transparent transparent #;
- border-style: solid;
- border-width: 50px;
- }
要点
- 第一个参数
left
要会找到方法中匹配程度最高的,如果匹配程度相同,将全部选择,并存在着样式覆盖替换。 - 如果匹配的参数 是变量,则将会匹配,如
@_
。
3.4 方法的命名空间
让方法更加规范
- /* Less */
- #card(){
- background: #;
- .d(@w:300px){
- width: @w;
- #a(@h:300px){
- height: @h;//可以使用上一层传进来的方法
- width: @w;
- }
- }
- }
- #wrap{
- #card > .d > #a(100px); // 父元素不能加 括号
- }
- #main{
- #card .d();
- }
- #con{
- //不得单独使用命名空间的方法
- //.d() 如果前面没有引入命名空间 #card ,将会报错
- #card; // 等价于 #card();
- .d(20px); //必须先引入 #card
- }
- /* 生成的 CSS */
- #wrap{
- height:100px;
- width:300px;
- }
- #main{
- width:300px;
- }
- #con{
- width:20px;
- }
要点
- 在 CSS 中
>
选择器,选择的是 儿子元素,就是 必须与父元素 有直接血源的元素。 - 在引入命令空间时,如使用
>
选择器,父元素不能加 括号。 - 不得单独使用命名空间的方法 必须先引入命名空间,才能使用 其中方法。
- 子方法 可以使用上一层传进来的方法
3.5 方法的条件筛选
Less 没有 if else,可是它有 when
- /* Less */
- #card{
- // and 运算符 ,相当于 与运算 &&,必须条件全部符合才会执行
- .border(@width,@color,@style) when (@width>100px) and(@color=#){
- border:@style @color @width;
- }
- // not 运算符,相当于 非运算 !,条件为 不符合才会执行
- .background(@color) when not (@color>=#){
- background:@color;
- }
- // , 逗号分隔符:相当于 或运算 ||,只要有一个符合条件就会执行
- .font(@size:20px) when (@size>50px) , (@size<100px){
- font-size: @size;
- }
- }
- #main{
- #card>.border(200px,#,solid);
- #card .background(#);
- #card > .font(40px);
- }
- /* 生成后的 CSS */
- #main{
- border:solid # 200px;
- background:#;
- font-size:40px;
- }
要点
- 比较运算有: > >= = =< <。
- = 代表的是等于
- 除去关键字 true 以外的值都被视为 false
3.6 数量不定的参数
如果你希望你的方法接受数量不定的参数,你可以使用... ,犹如 ES6 的扩展运算符。
- /* Less */
- .boxShadow(...){
- box-shadow: @arguments;
- }
- .textShadow(@a,...){
- text-shadow: @arguments;
- }
- #main{
- .boxShadow(1px,4px,30px,red);
- .textShadow(1px,4px,30px,red);
- }
- /* 生成后的 CSS */
- #main{
- box-shadow: 1px 4px 30px red;
- text-shadow: 1px 4px 30px red;
- }
3.7 方法使用important!
使用方法 非常简单,在方法名后 加上关键字即可。
- /* Less */
- .border{
- border: solid 1px red;
- margin: 50px;
- }
- #main{
- .border() !important;
- }
- /* 生成后的 CSS */
- #main {
- border: solid 1px red !important;
- margin: 50px !important;
- }
3.8 循环方法
Less 并没有提供 for 循环功能,但这也难不倒 聪明的程序员,使用递归去实现。
下面是官网中的一个 Demo,模拟了生成栅格系统。
- /* Less */
- .generate-columns();
- .generate-columns(@n, @i: ) when (@i =< @n) {
- .column-@{i} {
- width: (@i * % / @n);
- }
- .generate-columns(@n, (@i + ));
- }
- /* 生成后的 CSS */
- .column- {
- width: %;
- }
- .column- {
- width: %;
- }
- .column- {
- width: %;
- }
- .column- {
- width: %;
- }
3.9 属性拼接方法
+_ 代表的是 空格;+ 代表的是 逗号。
逗号
- /* Less */
- .boxShadow() {
- box-shadow+: inset 10px #;
- }
- .main {
- .boxShadow();
- box-shadow+: 20px black;
- }
- /* 生成后的 CSS */
- .main {
- box-shadow: inset 10px #, 20px black;
- }
- /* Less */
空格
- /* Less */
- .Animation() {
- transform+_: scale();
- }
- .main {
- .Animation();
- transform+_: rotate(15deg);
- }
- /* 生成的 CSS */
- .main {
- transform: scale() rotate(15deg);
- }
3.10 实战技巧
下面是官网中的一个非常赞的 Demo
- /* Less */
- .average(@x, @y) {
- @average: ((@x + @y) / );
- }
- div {
- .average(16px, 50px); // 调用 方法
- padding: @average; // 使用返回值
- }
- /* 生成的 CSS */
- div {
- padding: 33px;
- }
可以说 Less 是一门优雅编程语言。
4 继承
extend 是 Less 的一个伪类。它可继承 所匹配声明中的全部样式。
4.1 extend 关键字的使用
- /* Less */
- .animation{
- transition: all .3s ease-out;
- .hide{
- transform:scale();
- }
- }
- #main{
- &:extend(.animation);
- }
- #con{
- &:extend(.animation .hide);
- }
- /* 生成后的 CSS */
- .animation,#main{
- transition: all .3s ease-out;
- }
- .animation .hide , #con{
- transform:scale();
- }
4.2 all 全局搜索替换
使用选择器匹配到的 全部声明。
- /* Less */
- #main{
- width: 200px;
- }
- #main {
- &:after {
- content:"Less is good!";
- }
- }
- #wrap:extend(#main all) {}
- /* 生成的 CSS */
- #main,#wrap{
- width: 200px;
- }
- #main:after, #wrap:after {
- content: "Less is good!";
- }
4.3 减少代码的重复性
从表面 看来,extend 与 方法 最大的差别,就是 extend 是同个选择器共用同一个声明,而 方法 是使用自己的声明,这无疑 增加了代码的重复性。
方法示例 与上面的 extend 进行对比:
- /* Less */
- .Method{
- width: 200px;
- &:after {
- content:"Less is good!";
- }
- }
- #main{
- .Method;
- }
- #wrap{
- .Method;
- }
- /* 生成的 CSS */
- #main{
- width: 200px;
- &:after{
- content:"Less is good!";
- }
- }
- #wrap{
- width: 200px;
- &:after{
- content:"Less is good!";
- }
- }
5 导入
5.1 导入 less 文件 可省略后缀
- import "main";
- //等价于
- import "main.less";
- 5.2 @import 的位置可随意放置
- #main{
- font-size:15px;
- }
- @import "style";
5.3 reference
Less 中 最强大的特性
使用 引入的 Less 文件,但不会 编译它。
- /* Less */
- @import (reference) "bootstrap.less";
- #wrap:extend(.navbar all){}
使用@import (reference)导入外部文件,但不会添加 把导入的文件 编译到最终输出中,只引用。
5.3 once
@import语句的默认行为。这表明相同的文件只会被导入一次,而随后的导入文件的重复代码都不会解析。
- @import (once) "foo.less";
- @import (once) "foo.less"; // this statement will be ignored
5.4 multiple
使用@import (multiple)允许导入多个同名文件。
- /* Less */
- // file: foo.less
- .a {
- color: green;
- }
- // file: main.less
- @import (multiple) "foo.less";
- @import (multiple) "foo.less";
- /* 生成后的 CSS */
- .a {
- color: green;
- }
- .a {
- color: green;
- }
6 函数
6.1 判断类型
- isnumber
判断给定的值 是否 是一个数字。
- isnumber(#ff0); // false
- isnumber(blue); // false
- isnumber("string"); // false
- isnumber(); // true
- isnumber(56px); // true
- isnumber(7.8%); // true
- isnumber(keyword); // false
- isnumber(url(...)); // false
- iscolor
判断给定的值 是否 是一个颜色。
- isurl
判断给定的值 是否 是一个 url 。
6.2 颜色操作
- saturate
增加一定数值的颜色饱和度。
- lighten
增加一定数值的颜色亮度。
- darken
降低一定数值的颜色亮度。
- fade
给颜色设定一定数值的透明度。
- mix
根据比例混合两种颜色。
6.3 数学函数
- ceil
向上取整。
- floor
向下取整。
- percentage
将浮点数转换为百分比字符串。
- round
四舍五入。
- sqrt
计算一个数的平方根。
- abs
计算数字的绝对值,原样保持单位。
- pow
计算一个数的乘方。
由于 文章 篇幅有限,所以 只能介绍一些 使用效率高的函数。
如果你想了解更多,可以去官网的函数链接
7 其他
7.1 注释
- /* */ CSS原生注释,会被编译在 CSS 文件中。
- / / Less提供的一种注释,不会被编译在 CSS 文件中。
7.2 避免编译
- /* Less */
- #main{
- width:~'calc(300px-30px)';
- }
- /* 生成后的 CSS */
- #main{
- width:calc(300px-30px);
- }
结构: `~' 值 '`
8 使用 JS
因为 Less 是由 JS 编写,所以 Less 有一得天独厚的特性:代码中使用 Javascript 。
- /* Less */
- @content:`"aaa".toUpperCase()`;
- #randomColor{
- @randomColor: ~"rgb(`Math.round(Math.random() * 256)`,`Math.round(Math.random() * 256)`,`Math.round(Math.random() * 256)`)";
- }
- #wrap{
- width: ~"`Math.round(Math.random() * 100)`px";
- &:after{
- content:@content;
- }
- height: ~"`window.innerHeight`px";
- alert:~"`alert(1)`";
- #randomColor();
- background-color: @randomColor;
- }
- /* 生成后的 CSS */
- // 弹出 1
- #wrap{
- width: 随机值(~)px;
- height: 743px;//由电脑而异
- background: 随机颜色;
- }
- #wrap::after{
- content:"AAA";
- }
前几个月 , 有个 CSS in JS 的概念非常火,现在 看来 JS in CSS 也未曾不可。 我觉得完全可以根据 Less 这个特性来造个轮子,JS来控制 CSS ,形成 动态属性,如果成功 很可能会改变 现在前端的打开姿势。
less使用方法总结的更多相关文章
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- 【.net 深呼吸】细说CodeDom(6):方法参数
本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...
- IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法
直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- [C#] C# 基础回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...
- ArcGIS 10.0紧凑型切片读写方法
首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...
- [BOT] 一种android中实现“圆角矩形”的方法
内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...
- JS 判断数据类型的三种方法
说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
随机推荐
- 多项式拟合的cpp实现
当我们拥有一组散点图数据时,通常更愿意看到其走势. 对现有数据进行拟合,并输出拟合优度是常用的方法之一. 拟合结果正确性的验证,可以使用excel自带的功能. 下面是c++代码的实现: #ifndef ...
- boost的单例模式
template <typename T> struct singleton_default { private: struct object_creator { ...
- Stanford概率图模型: 第一讲 有向图-贝叶斯网络
原文链接(系列):http://blog.csdn.net/yangliuy/article/details/8067261 概率图模型(Probabilistic Graphical Model)系 ...
- js页面传值,cookie
// 获取页面穿值 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...
- 提示 npm update check failed
执行npm命令时出现以下提示 虽然不影响代码运行,但总觉得看了很碍事, 查找资料后发现是因为文件夹权限的问题, .config / configstore文件夹中包含一个文件:update-notif ...
- JavaScript 字符串匹配 | JS 的正则用法 | 从后边匹配
// 字符串匹配命令是 match,不是 replace var text = "http://123.com/456.html" ; window.alert(text.matc ...
- [CodeForces]981C Useful Decomposition
李煜东dalao今天给我们讲课了QwQ ppt上一道题 英文题说一下题意吧,以后又看不懂了 将一棵树分割成多个简单路径,每个边只能在一条路径上,但至少有一个公共节点. 输出简单路径分割方法/No 由题 ...
- CF409C Magnum Opus
CF409C Magnum Opus 题意翻译 题目背景 愚人节题目,题面似乎是一位名叫Nicolas Flamel的炼金术士用拉丁文写的某种物质的配方,结合谷歌尝试翻译了一下: 吾友: 哲人石所言不 ...
- 由free命令想到的
root@xdj-Z9PA-D8-Series:~# free -m total used free shared buffers cached Mem: 15977 1683 14293 0 132 ...
- POJ 2133
类似于DP一样做,但这题有个大坑,自己看DIS吧.... #include <iostream> #include <cstdio> #include <cstring& ...