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>
目录
相关文章
|
3天前
|
数据采集 人工智能 安全
|
13天前
|
云安全 监控 安全
|
4天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1090 152
|
18天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1754 9
|
10天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
697 152
|
12天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
661 13
|
6天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
447 5