Less-CSS预处理语言快速入门

简介: Less-CSS预处理语言快速入门

Less css预处理语言

特性:变量、继承、运算、函数

http://lesscss.cn/

编译器

1、koala

http://koala-app.com/index-zh.html

https://github.com/oklai/koala

2、sublime插件

less       # 语法高亮
less2css   # 保存自动生成同名的css文件

需要安装node环境+node插件

npm install -g less

npm install -g less-plugin-clean-css

报错:

less2css error: `lessc` is not available

重启sublime

配置保存时不进行压缩

{
"minify": false
}

语法

1、注释

/**/  # css中的注释
// # 编译时会被自动过滤

2、变量

以@开头,例如:

@变量:值;


@text_with: 200px;

.box{
width: @text_with;
heigth: @text_with;
background-color: yellow;
}

编译结果


.box {
width: 200px;
height: 200px;
background-color: yellow;
}

3、混合

类似其他语言中的函数

.border{
border: solid 5px pink;
}

.box-border{
.border;
width: 200px;
height: 200px;
background-color: green;
}

编译结果

.border {
border: solid 5px pink;
}

.box-border {
border: solid 5px pink;
width: 200px;
height: 200px;
background-color: green;
}

4、混合带参数

.box{
width: 200px;
height: 200px;
background-color: yellow;
.border(green); // 混合
}

.border(@color){
border: solid 5px @color;
}

编译结果

.box {
width: 200px;
height: 200px;
background-color: yellow;
border: solid 5px green;
}

5、混合默认参数

.box{
width: 200px;
height: 200px;
background-color: yellow;
.border();
}

.border(@color: 10px){
border: solid 5px @color;
}

编译结果

.box {
width: 200px;
height: 200px;
background-color: yellow;
border: solid 5px 10px;
}

6、混合示例

.box{
width: 200px;
height: 200px;
background-color: green;
.border-radius()
}

.border-radius(@radius: 5px){
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
border-radius: @radius;
}

编译结果

.box {
width: 200px;
height: 200px;
background-color: green;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}

7、匹配模式

类似其他语言中的if语句

@charset "utf-8";

.box-line(top, @boder-width:5px){
border-top: solid @boder-width red;
}

.box-line(bottom, @boder-width:5px){
border-bottom: solid @boder-width red;
}

.box-line(left, @boder-width:5px){
border-left: solid @boder-width red;
}

.box-line(right, @boder-width:5px){
border-right: solid @boder-width red;
}

// 不管匹配到谁,以下样式都会被输出
.box-line(@_, @boder-width:5px){
background-color: green;
width: 200px;
height: 200px;
}

.box{
.box-line(right)
}

编译结果

@charset "utf-8";

.box {
border-right: solid 5px red;
background-color: green;
width: 200px;
height: 200px;
}

8、运算

支持 + - * /

@default-width: 20px;

.box{
width: @default-width + 20;
}

编译结果

.box {
width: 40px;
}

9、嵌套

.list{
list-style: none;
width: 500px;
margin: 30px auto;

li{
height: 20px;

}

a{
float: left;

// & 表示上一层选择器
&:hover{
color: red;
}

}

}

编译效果

.list {
list-style: none;
width: 500px;
margin: 30px auto;
}

.list li {
height: 20px;
}

.list a {
float: left;
}

.list a:hover {
color: red;
}

10、@arguments

获取所有参数

.box{
.border-color();
}

.border-color(@width: 30px, @color: red){
border: solid @arguments;
}

编译效果

.box {
border: solid 30px red;
}

11、避免编译

.box{
height: calc(20px + 10px);
// 避免编译
width: ~'calc(20px + 10px)';
}

编译效果

.box {
height: calc(20px + 10px);
width: calc(20px + 10px);
}

12、!important

.box{
height: 20px !important;
}

编译效果

.box {
height: 20px !important;
}

12、文件导入

hi.css

.hi{
height: 20px;
}

hello.less

.hello{
width: 20px
}

main.less


// 引入less文件
@import "hello";

// 引入 css文件
@import "hi.css";

// 引入 css文件 转为 less
@import (less) "hi.css";

编译效果

main.css

@import "hi.css";
.hello {
width: 20px;
}
.hi {
height: 20px;
}


            </div>
目录
相关文章
|
数据采集 城市大脑 分布式计算
阿里云产业智能OpenTrek技术升级:发布数字孪生仿真技术架构
2022年11月4日,云栖大会——产业智能技术创新与实践峰会于杭州云栖小镇如期举办,本峰会联合中国科学院院士、中国工程院院士、海外院士与行业权威机构,共同探讨产业智能方法论及发展趋势。会上,阿里云产业智能OpenTrek发布了技术升级后的数字孪生仿真技术架构,为行业数字化转型方向的合作伙伴提供全过程状态数据元信息整合能力,帮助行业客户进行业务价值创新。
阿里云产业智能OpenTrek技术升级:发布数字孪生仿真技术架构
开源测试平台横向测评系列『流马』篇:流马使用及总结
【使用篇】 ● 接口测试:创建接口(添加引用公共参数、添加引用自定义参数)、测试用例(参数关联)、业务流程测试实践 ● web自动化测试:元素管理(添加元素)、测试用例(添加元素)、设计测试场景 ● 测试计划、测试集合与测试用例相互之间的关系 【总结篇】 ● 使用总结:常见的使用注意事项,如变量引用、函数引用、关联参数引用等 ● 优化建议:结合真实使用过程,从用户角度出发,提出的7条优化建议 ● 优缺点总结:优点、缺点、评分(从不同角度评测打分)
开源测试平台横向测评系列『流马』篇:流马使用及总结
|
Python
Python3,5句话实现自动接收短信提醒
Python3,5句话实现自动接收短信提醒
676 0
Python3,5句话实现自动接收短信提醒
|
资源调度
There appears to be trouble with your network connection.Retrying
There appears to be trouble with your network connection.Retrying
2234 0
There appears to be trouble with your network connection.Retrying
|
JSON 缓存 前端开发
MapStruct,降低无用代码的神器
在学习《告别BeanUtils,Mapstruct从入门到精通》后,我发觉MapStruct确实是一个提升系统性能,降低无用代码的神器。然而,在实践这篇文章过程中,我遇到了些问题,并由此对MapStruct框架有了更深入的理解,以下将我的学习收获分享给大家。
939 1
MapStruct,降低无用代码的神器
|
Java 编译器 API
JVM系列之:关于方法句柄的那些事
JVM系列之:关于方法句柄的那些事
408 1
JVM系列之:关于方法句柄的那些事
|
弹性计算 监控 容灾
EDAS 介绍|学习笔记
快速学习 EDAS 介绍
1970 0
EDAS 介绍|学习笔记
|
JavaScript
Vue3+Ts练习小案例——实现追踪鼠标的
本文给大家带来一个Vue3+Ts练习小案例,帮助大家更好的掌握Vue3+Ts的基础
705 0
Vue3+Ts练习小案例——实现追踪鼠标的
|
机器学习/深度学习 监控 Python
机器学习中的概念漂移(Aporia)
随着机器学习模型成为自动化和预测任务越来越流行的解决方案,许多科技公司和数据科学家采用了以下工作范式:数据科学家负责解决特定问题,他们会得到可用相关数据的快照,他们致力于训练模型来解决它。 一旦模型经过测试,它就会进入生产阶段。最终,模型的性能开始下降,这通常是由于概念漂移。 概念漂移是指目标变量(模型试图预测的内容)的统计特性随时间以不可预见的方式发生变化的情况。
|
自然语言处理 JavaScript 机器人
简单对话场景搭建和云小蜜接口集成 | 学习笔记
简介:快速学习简单对话场景搭建和云小蜜接口集成
634 0
简单对话场景搭建和云小蜜接口集成 | 学习笔记