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>
目录
相关文章
|
2天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
253 116
|
17天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
11天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
650 220
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
886 61
|
9天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1470 157
|
6天前
|
编解码 Linux 数据安全/隐私保护
教程分享免费视频压缩软件,免费视频压缩,视频压缩免费,附压缩方法及学习教程
教程分享免费视频压缩软件,免费视频压缩,视频压缩免费,附压缩方法及学习教程
282 139
|
8天前
|
存储 安全 固态存储
四款WIN PE工具,都可以实现U盘安装教程
Windows PE是基于NT内核的轻量系统,用于系统安装、分区管理及故障修复。本文推荐多款PE制作工具,支持U盘启动,兼容UEFI/Legacy模式,具备备份还原、驱动识别等功能,操作简便,适合新旧电脑维护使用。
593 109