Less常用知识点
上篇文章介绍了如何安装Less,我们将所有东西都写在.less里面,最后通过命令将.less转换成.css文件,就可以放入到项目里用了。今天了解一些less常用知识点。
1.变量:声明两个变量,一个是背景颜色,一个是文本颜色
Less代码:
@background-color: #ffffff;
@text-color: #1A237E; p{
background-color: @background-color;
color: @text-color;
padding: 15px;
} ul{
background-color: @background-color;
} li{
color: @text-color;
}
将其编译成css后的代码:
p{
background-color: #ffffff;
color: #1A237E;
padding: 15px;
}
ul{
background-color: #ffffff;
}
li{
color: #1A237E;
}
这就是Less里面的变量用法,用起来非常方便。比如上面想切换那两个颜色只需要将变量值互换一下即可。
2.Mixin :可以将已有的 class 和 id 的样式应用到另一个不同的选择器上。比如看下面例子。
#circle{
background-color: #4CAF50;
border-radius: %;
}
#small-circle{
width: 50px;
height: 50px;
#circle
}
#big-circle{
width: 100px;
height: 100px;
#circle
}
将其编译成css后的代码:
#circle {
background-color: #4CAF50;
border-radius: %;
}
#small-circle {
width: 50px;
height: 50px;
background-color: #4CAF50;
border-radius: %;
}
#big-circle {
width: 100px;
height: 100px;
background-color: #4CAF50;
border-radius: %;
}
注意看上面这个.css里面是不是也出现了 #circle的样式,如果你不想让#circle再出现到.css里面那就加一个()即可,例如:
#circle(){
background-color: #4CAF50;
border-radius: %;
}
#small-circle{
width: 50px;
height: 50px;
#circle
}
#big-circle{
width: 100px;
height: 100px;
#circle
}
将其编译成css后的代码:
#small-circle {
width: 50px;
height: 50px;
background-color: #4CAF50;
border-radius: %;
}
#big-circle {
width: 100px;
height: 100px;
background-color: #4CAF50;
border-radius: %;
}
另外Mixin还可以传参,比如传入一个指定宽高的参数,默认是30。创建一个 30×30的小圆和一个 200×200 的大圆
#circle(@size: 30px){
background-color: #4CAF50;
border-radius: %;
width: @size;
height: @size;
}
#small-circle{
#circle
}
#big-circle{
#circle(200px)
}
将其编译成css后的代码:
#small-circle {
background-color: #4CAF50;
border-radius: %;
width: 30px;
height: 30px;
}
#big-circle {
background-color: #4CAF50;
border-radius: %;
width: 200px;
height: 200px;
}
3.嵌套:可以和html相匹配的方式构造.Less样式表,例如:
ul{
background-color: #03A9F4;
padding: 10px;
list-style: none;
li{
background-color: #fff;
border-radius: 3px;
margin: 10px ;
}
}
将其编译成css后的代码:
ul {
background-color: #03A9F4;
padding: 10px;
list-style: none;
}
ul li {
background-color: #fff;
border-radius: 3px;
margin: 10px ;
}
就像在其它高级语言中一样, Less 的变量根据范围接受它们的值。如果在指定范围内没有关于变量值的声明, less 会一直往上查找,直至找到离它最近的声明。例如:
@text-color: #;
ul{
@text-color: #fff;
background-color: #03A9F4;
padding: 10px;
list-style: none;
li{
color: @text-color;
border-radius: 3px;
margin: 10px ;
}
}
将其编译成css后的代码:
ul {
background-color: #03A9F4;
padding: 10px;
list-style: none;
}
ul li {
color: #ffffff;
border-radius: 3px;
margin: 10px ;
}
4.运算:和+ - * /一样 可以操作任何数字类型变量。例如:两个紧邻的 div 标签,第二个标签是第一个标签的两倍宽并且拥有不同的背景色。
@div-width: 100px;
@color: #03A9F4; div{
height: 50px;
display: inline-block;
} #left{
width: @div-width;
background-color: @color - ;
} #right{
width: @div-width * ;
background-color: @color;
}
将其编译成css后的代码:
div {
height: 50px;
display: inline-block;
}
#left {
width: 100px;
background-color: #;
}
#right {
width: 200px;
background-color: #03a9f4;
}
5.函数: 看一下 fadeout, 一个降低颜色透明度的函数
@var: #;
div{
height: 50px;
width: 50px;
background-color: @var;
&:hover{
background-color: fadeout(@var, %)
}
}
将其编译成css后的代码:
div {
height: 50px;
width: 50px;
background-color: #;
}
div:hover {
background-color: rgba(, , , 0.5);
}
当鼠标放在div上时,降低透明度0.5
想学习更多Less知识,推荐:https://www.w3cschool.cn/less/importing.html
原文来自:https://www.jianshu.com/p/c676041f387e.
Less常用知识点的更多相关文章
- DB2_SQL_常用知识点&实践
DB2_SQL_常用知识点&实践 一.删除表中的数据(delete或truncate) 1 truncate table T_USER immediate; 说明:Truncate是一个能够快 ...
- JAVA常用知识点及面试题总结
1. String.StringBuffer.StringBuilder三者区别? (1)三者在执行速率上的比较: String<StringBuffer<StringBuilder 原因 ...
- HTML常用知识点代码演示
1 HTML部分常用知识点 <!-- 版本声明 --> <!DOCTYPE html> <!-- 唯一根元素 --> <html> <!-- 对网 ...
- Java 常用知识点
Java 常用知识点 1.日期格式化 SimpleDateFormat Date date=new Date(System.currentTimeMillis()) ; SimpleDateForma ...
- BIOS备忘录之EC常用知识点
BIOS工程师眼中常用的EC知识点汇总: EC的硬件架构 EC硬件结构上主要分为两部分:Host Domain和EC Domain Host Domain就是通过LPC与CPU通信的部分(LPC部分需 ...
- YII2常用知识点总结
YII2常用知识点总结 (一)总结性语句 (1)经常看看yii源码比如vendor\yiisoft\yii2\web这个目录(很重要)下的文件中的方法(这些文件中的公共方法,大致看了下基本上都可以通过 ...
- CSS3常用知识点
CSS3常用知识点 1 css3选择器 1.1 属性选择器 /* E[attr~=val] 表示的一个单独的属性值 这个属性值是以空格分隔的*/ .attr2 a[class~="kawa& ...
- javaScript常用知识点有哪些
javaScript常用知识点有哪些 一.总结 一句话总结:int = ~~myVar, // to integer | 是二进制或, x|0 永远等于x:^为异或,同0异1,所以 x^0 还是永远等 ...
- 一文学会 TypeScript 的 82% 常用知识点(下)
一文学会 TypeScript 的 82% 常用知识点(下) 前端专栏 2019-11-23 18:39:08 都已经 9021 年了,TypeScript(以下简称 TS)作为前端工程师不得 ...
随机推荐
- 对空间数据(Shape)重新排序
打开ArcToolBox,数据管理工具->常规(General)->排序
- springboot使用jpa+mongodb时,xxxRepository不能Autowired的问题
springboot启动类: @SpringBootApplication public class MainApp { public static void main(String[] args) ...
- JVM学习(一)简介
一.java程序编译到运行大概流程 1.Source Code Files为.java文件 2.通过编译产生可执行的字节码. 3.通过jvm得到机器可以执行的机器码 4.操作系统运行机器码,并与硬件进 ...
- 5-23 CSS知识的补充
1,后代选择器 使用空格表示后代选择器.顾名思义,父元素的后代(包括儿子,孙子,重孙子). <!DOCTYPE html> <html lang="en"> ...
- 【tmos】使用joda-time来个格式化时间
代码 @Test public void test(){ DateTime dateTime = new DateTime(); String str = dateTime.toString(&quo ...
- 【tmos】spring data jpa 创建方法名进行简单查询
参考链接 spring data jpa 创建方法名进行简单查询:http://www.cnblogs.com/toSeeMyDream/p/6170790.html
- Redis 深度历险
学习资料 https://juejin.im/book/5afc2e5f6fb9a07a9b362527 包括下面几方面的内容 基础 应用 原理 集群 拓展 源码 to be done
- 关于vue监听dom与传值问题
1. 代码初始化一次执行部分属性为空的情况 原因: 异步加载 + 立马 传值时 直接渲染 dom里面 能实时更新 (无影响) 不能直接dom中渲染(有影响) 解决方法:需要通过监听的方式来处 ...
- POJ 3253 Fence Repair (贪心)
题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度.求出按照要求将木板切割完毕后的最小开销. 思路:比较奇特的贪心 每次切割都会将当前木板一分为二,可以按切 ...
- Nginx系列4:用GoAccess实现可视化并实时监控access日志
1.ubuntu16.04安装GoAccess GoAccess下载地址:https://goaccess.io/download 安装步骤: $ wget https://tar.goaccess. ...