Less学习笔记 -- Mixins(混合)一

简介:

混合:

混合可以将一个定义好的classA轻松的引入到另一个classB中,从而简单实现classB继承classA中的所有属性。任何CSS中的class或者id都可以引入

Less:

1
2
3
4
5
6
.aWidth{ width : 400px ;}
#aHeight{ height : 600px ;}
p{
   .aWidth;
   #aHeight;
}

CSS:

1
2
3
4
5
6
7
8
9
10
.aWidth {
   width 400px ;
}
#aHeight {
   height 600px ;
}
p {
   width 400px ;
   height 600px ;
}


带选择器的混合集:

混合集不仅可以包含各种属性,而且可以包含各种选择器

Less:

1
2
3
4
5
6
7
8
.my-hover-mixin(){//加个空括号,不执行这段代码的编译
   &:hover{
     border : 1px  solid  #ddd ;
   }
}
button{
   .my-hover-mixin;
}


CSS:

1
2
3
button:hover {
   border 1px  solid  #ddd ;
}


不输出混合集:

如果你想创建一个混合集,但是却不想让它输出到你的样式中,你可以在混合集的名字后面加一个括号。这句话怎么理解呢?对比以下两段代码:

代码一:

Less:

1
2
3
4
5
6
7
8
9
10
11
12
.my-mixin{
   color : black ;
}
/*看这里*/
.my-other-mixin{
   background : white ;
}
.class{
   .my-mixin;
   /*看这里*/
   .my-other-mixin;
}


CSS:

1
2
3
4
5
6
7
8
9
10
11
12
.my-mixin {
   color black ;
}
/*看这里*/
.my-other-mixin {
   background white ;
}
.class {
   color black ;
   /*看这里*/
   background white ;
}


代码二:

Less:

1
2
3
4
5
6
7
8
9
10
11
12
.my-mixin{
   color : black ;
}
/*看这里*/
.my-other-mixin(){
   background : white ;
}
.class{
   .my-mixin;
   /*看这里*/
   .my-other-mixin;
}


CSS:带空括号的混合集并没有编译过来,但是可以编译到另一个引用它的选择器里面

1
2
3
4
5
6
7
8
9
.my-mixin {
   color black ;
}
/*看这里*/
.class {
   color black ;
   /*看这里*/
   background white ;
}


我们还可以带参数的调用,就像使用函数一样

Less:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.filter(@blur){
   -webkit-filter:blur(@blur);
   -moz-filter:blur(@blur);
   -ms-filter:blur(@blur);
   filter:blur(@blur);
}
.border-radius(@radius: 5px ){
   -webkit-border-radius: @radius;
   -moz-border-radius: @radius;
   border-radius: @radius;
}
#section{
   .border-radius;
   /*如果写公共样式的时候没有设定具体的数值,引用的时候就必须设定,否则出不来效果*/
   .filter( 5px );
}
#footer{.border-radius( 10px );}

CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#section {
   -webkit-border-radius:  5px ;
   -moz-border-radius:  5px ;
   border-radius:  5px ;
   /*如果写公共样式的时候没有设定具体的数值,引用的时候就必须设定,否则出不来效果*/
   -webkit-filter: blur( 5px );
   -moz-filter: blur( 5px );
   -ms-filter: blur( 5px );
   filter: blur( 5px );
}
#footer {
   -webkit-border-radius:  10px ;
   -moz-border-radius:  10px ;
   border-radius:  10px ;
}


带多个参数的混合

参数可以用逗号分号分隔,但是推荐用分号分隔。

定义多个具有相同名称和参数数量的mixins是合法的,less会使用它可以使用的属性。如果使用mixin的时候只带一个参数,比如.mixin(red),这个属性会导致所有的mixin都会强制使用这个明确的参数:

Less:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.mixin(@color) {
   color -1:  @color;
}
.mixin(@color; @ padding 2px ) {
   color -2:  @color;
   padding -2:  @padding;
}
.mixin(@color; @padding; @ margin 5px ) {
   color -3:  @color;
   padding -3:  @padding;
   margin : @margin @margin @margin @margin;
}
 
h 1 {
   .mixin( red );
}
div{
   .mixin( green ; 4px );
}
span{
   .mixin( blue 6px 8px );
}


CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
h 1  {
   color -1:  #ff0000 ;
   color -2:  #ff0000 ;
   padding -2:  2px ;
}
div {
   color -2:  #008000 ;
   padding -2:  4px ;
   color -3:  #008000 ;
   padding -3:  4px ;
   margin 5px  5px  5px  5px ;
}
span {
   color -3:  #0000ff ;
   padding -3:  6px ;
   margin 8px  8px  8px  8px ;
}


命名参数

引用mixin时可以通过参数名称而不是参数的位置来为mixin提供参数值,任何参数都通过它的名称来引用,而不是特定的顺序

Less:

1
2
3
4
5
6
7
8
9
10
11
.mixin(@ color : blue ; @ padding : 10px ; @ margin : 15px ;){
   color :@color;
   padding :@padding;
   margin :@margin;
}
.class 1 {
   .mixin(@ margin : 20px ; @ color : #e4393c ;)
}
.class 2 {
   .mixin( #bf6da5 60px ;)
}


CSS:

1
2
3
4
5
6
7
8
9
10
.class 1  {
   color #e4393c ;
   padding 10px ;
   margin 20px ;
}
.class 2  {
   color #bf6da5 ;
   padding 60px ;
   margin 15px ;
}


@arguments变量

arguments在Javascript中代表所有的参数,在这里也是同样的意思,只不过用法稍有区别。如果你不想单个单个的处理参数,这一特性是很有用的:

Less:

1
2
3
4
5
6
7
8
.box-shadow(@x: 0 ; @y: 0 ; @blur: 1px ; @ color : #000 ;){
   -webkit-box-shadow: @arguments;
   -moz-box-shadow: @arguments;
   box-shadow: @arguments;
}
.big- block {
   .box-shadow( 2px 5px ;)
}


CSS:

1
2
3
4
5
.big- block  {
   -webkit-box-shadow:  2px  5px  1px  #000000 ;
   -moz-box-shadow:  2px  5px  1px  #000000 ;
   box-shadow:  2px  5px  1px  #000000 ;
}



!important关键字

在调用的混合集后面追加!important关键字,可以使混合集里面所有的属性都继承!important

Less:

1
2
3
4
5
6
7
8
9
10
.foo(@bg: #f00 , @ color : #666 ){
   background :@bg;
   color :@color;
}
.unimportant{
   .foo;
}
.important{
   .foo  !important ;
}


CSS:

1
2
3
4
5
6
7
8
.unimportant {
   background #ff0000 ;
   color #666666 ;
}
.important {
   background #ff0000  !important ;
   color #666666  !important ;
}



在这里,也可以体验一把Less在实际开发中关于提高代码维护,给我们带来的魅力

示例一

Less:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@pink: #f0f ;
#header{
   h 2 {
     font-size : 26px ;
     font-weight : bold ;
   }
   .sub_title{
     color :@pink;
   }
   .study_list{
     color :@pink+ 111 ;
   }
   p{
     font-size : 12px ;
     a{
       text-decoration none ;
       &:hover{
         border-width : 1px ;
         color :@pink+ 666 ;
       }
     }
   }
}


CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#header h 2  {
   font-size 26px ;
   font-weight bold ;
}
#header .sub_title {
   color #ff00ff ;
}
#header .study_list {
   color #ff6fff ;
}
#header p {
   font-size 12px ;
}
#header p a {
   text-decoration none ;
}
#header p a:hover {
   border-width 1px ;
   color #ffffff ;
}


示例二

Less:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@url: "../images" ;
.filter(@blur){
   -webkit-filter:blur(@blur);
   -moz-filter:blur(@blur);
   -ms-filter:blur(@blur);
   filter:blur(@blur);
}
.border-radius(@radius: 5px ){
   -webkit-border-radius: @radius;
   -moz-border-radius: @radius;
   border-radius: @radius;
}
 
#loginForm{
   width : 80% ;
   margin : 0  auto ;
   ul{
     li{
       margin : 15px  0 ;
       &:nth-child( 2 ){
         position : relative ;
       }
       label{
         color : #d4d2d2 ;
         font-family : 'Microsoft Yahei' ;
         font-weight : bold ;
         font-size : 1.1em ;
       }
     }
   }
   .imgBground{
     width : 100% ;
     height : 28 vh;
     filter: url (blur.svg#blur);
     .filter( 5px );
     .border-radius( 10px );
     background : url ( '@{url}/1.jpg' );
   }


CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#loginForm {
   width 80% ;
   margin 0  auto ;
}
#loginForm ul li {
   margin 15px  0 ;
}
#loginForm ul li:nth-child( 2 ) {
   position relative ;
}
#loginForm ul li label {
   color #d4d2d2 ;
   font-family 'Microsoft Yahei' ;
   font-weight bold ;
   font-size 1.1em ;
}
#loginForm .imgBground {
   width 100% ;
   height 28 vh;
   filter:  url (blur.svg#blur);
   -webkit-filter: blur( 5px );
   -moz-filter: blur( 5px );
   -ms-filter: blur( 5px );
   filter: blur( 5px );
   -webkit-border-radius:  10px ;
   -moz-border-radius:  10px ;
   border-radius:  10px ;
   background url ( '../images/1.jpg' );
}



详情参考官方文档:

http://www.css88.com/doc/less/features/#mixins-feature

本文转自   frwupeng517   51CTO博客,原文链接:http://blog.51cto.com/dapengtalk/1862989

相关文章
|
27天前
|
人工智能 自然语言处理 Shell
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
36560 148
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
|
10天前
|
人工智能 自然语言处理 监控
OpenClaw skills重构量化交易逻辑:部署+AI全自动炒股指南(2026终极版)
2026年,AI Agent领域最震撼的突破来自OpenClaw(原Clawdbot)——这个能自主规划、执行任务的智能体,用50美元启动资金创造了48小时滚雪球至2980美元的奇迹,收益率高达5860%。其核心逻辑堪称教科书级:每10分钟扫描Polymarket近千个预测市场,借助Claude API深度推理,交叉验证NOAA天气数据、体育伤病报告、加密货币链上情绪等多维度信息,捕捉8%以上的定价偏差,再通过凯利准则将单仓位严格控制在总资金6%以内,实现低风险高频套利。
4304 33
|
5天前
|
存储 人工智能 负载均衡
阿里云OpenClaw多Agent实战宝典:从极速部署到AI团队搭建,一个人=一支高效军团
在AI自动化时代,单一Agent的“全能模式”早已无法满足复杂任务需求——记忆臃肿导致响应迟缓、上下文污染引发逻辑冲突、无关信息加载造成Token浪费,这些痛点让OpenClaw的潜力大打折扣。而多Agent架构的出现,彻底改变了这一现状:通过“单Gateway+多分身”模式,让一个Bot在不同场景下切换独立“大脑”,如同组建一支分工明确的AI团队,实现创意、写作、编码、数据分析等任务的高效协同。
1067 22
|
23天前
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
8126 23
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
|
21天前
|
人工智能 机器人 Linux
OpenClaw(Clawdbot、Moltbot)汉化版部署教程指南(零门槛)
OpenClaw作为2026年GitHub上增长最快的开源项目之一,一周内Stars从7800飙升至12万+,其核心优势在于打破传统聊天机器人的局限,能真正执行读写文件、运行脚本、浏览器自动化等实操任务。但原版全英文界面对中文用户存在上手门槛,汉化版通过覆盖命令行(CLI)与网页控制台(Dashboard)核心模块,解决了语言障碍,同时保持与官方版本的实时同步,确保新功能最快1小时内可用。本文将详细拆解汉化版OpenClaw的搭建流程,涵盖本地安装、Docker部署、服务器远程访问等场景,同时提供环境适配、问题排查与国内应用集成方案,助力中文用户高效搭建专属AI助手。
5443 12
|
4天前
|
人工智能 JavaScript Linux
别再花钱买云服务器了!OpenClaw 本地部署保姆级教程,10分钟拥有私人AI助理(附阿里云简单部署流程)
2026年,AI私人助理已从“高端配置”变成“日常刚需”,而OpenClaw(原Clawdbot,曾用名Moltbot)作为开源界的“黑马”,凭借自然语言驱动、多技能扩展、零门槛上手的核心优势,成为无数人打造私人AI助理的首选——它无需复杂代码基础,无需高价云服务器,只要你有一台普通电脑(Windows、Mac、Linux均可),跟着步骤操作,10分钟就能完成本地部署,同时也支持阿里云简单部署,兼顾“零成本本地使用”与“云端稳定托管”双重需求,彻底打破“AI助理必花钱”的误区。
1103 4
|
8天前
|
人工智能 JavaScript API
Windows系统OpenClaw保姆级部署指南:本地+云端双方案,零技术基础也能玩转AI助手
在AI办公自动化全面普及的2026年,OpenClaw(原Clawdbot、Moltbot)凭借“自然语言指令操控、多任务自动化执行、多工具无缝集成”的核心优势,成为个人与轻量办公群体打造专属AI助手的首选。它不仅能通过聊天互动响应需求,更具备“动手”和“跑腿”的硬核能力——“手”可读写本地文件、执行代码、操控命令行,“脚”能联网搜索、访问网页并分析内容,“大脑”则可灵活接入Qwen、OpenAI等云端API,或利用本地GPU运行模型,真正实现“聊天框里办大事”。
1019 6