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)作为前端工程师不得 ...
随机推荐
- python里的input
python2和python3的input是不同的 python3的input 对于python3,只有input,官方文档里是这样描述的 def input(*args, **kwargs): # ...
- Sql分页代码示例
select * from (select ROW_NUMBER()over( order by id) orderid,* from test) a where a.orderid between ...
- 使用js弹出div刷新时闪烁解决方法
<div style="visibility: hidden"> //弹出div内容 </div>
- SQL Server Management Studio 使用技巧
Ø 前言 本文主要介绍 SQL Server Management Studio 工具的使用,相信很多开发人员都比较熟悉此工具,特别是做 C# 开发的程序员,基本上都会经常使用该工具,当然也可以使用 ...
- H5网页适配 iPhoneX,就是这么简单
iPhoneX 取消了物理按键,改成底部小黑条,这一改动导致网页出现了比较尴尬的屏幕适配问题.对于网页而言,顶部(刘海部位)的适配问题浏览器已经做了处理,所以我们只需要关注底部与小黑条的适配问题即可( ...
- Dapper.net 输出存储过程实例
1.存储过程名: public static class CampaignTrackingDomainSql { /// <summary> /// proc /// </summa ...
- Linux LVM 逻辑分区
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现.普通 ...
- IIS服务器的安全保护措施
转载自:https://www.williamlong.info/archives/118.html 部分内容做了修改. 通过标注Web服务器安全级别以及可用性的安全策略,网络管理员将能够从容地在不同 ...
- 关于“ubuntu18.04下网易云无法启动”的问题解决方案
1. 最简单的解决方案(参考文章:亢奋猫): 更正:在更改启动文件netease-cloud-music.desktop时应为:将第11行处的“Exec=netease-cloud-music %U” ...
- Kaldi中的L2正则化
steps/nnet3/train_dnn.py --l2-regularize-factor 影响模型参数的l2正则化强度的因子.要进行l2正则化,主要方法是在配置文件中使用'l2-regulari ...