- LESS(Leaner Style Sheets)定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些编程的特性,将 CSS 作为目标生成文件,然后开发者就只要使用这种语言进行编码工作。
- LESS 做为 CSS 的一种形式的扩展,它并没有阉割 CSS 的功能,而是在现有的 CSS 语法上,添加了很多额外的功能
| //注释内容 不会被编译到css文件里 |
| |
| /* 注释内容 */ 会被编译到css文件里 |
| |
| // 顺便说说 & 符号,它其实就是指上一个父级标签 |
2.变量
变量允许我们单独定义一系列通用的样式,然后在需要的时候去调用。所以在做全局样式调整的时候我们可能只需要修改几行代码就可以了。
| |
| @color: #4D926F; |
| #header { |
| color: @color; |
| } |
| h2 { |
| color: @color; |
| } |
| |
| |
| #header { |
| color: #4D926F; |
| } |
| h2 { |
| color: #4D926F; |
| } |
| |
| @color: #4D926F; |
| #header { |
| color: @color; |
| } |
| h2 { |
| color: @color; |
| } |
| |
| |
| #header { |
| color: #4D926F; |
| } |
| h2 { |
| color: #4D926F; |
| } |
- less变量还可以作为属性名字
- 作为属性值调用时:@变量名
- 作为属性名调用时:@{变量名}
| |
| .box{ |
| position: relative; |
| @{lf}:10px; |
| margin-@{lf}:20px; |
| float: @lf; |
| } |
| |
| .box { |
| position: relative; |
| left: 10px; |
| margin-left: 20px; |
| float: left; |
| } |
| |
| .box{ |
| position: relative; |
| @{lf}:10px; |
| margin-@{lf}:20px; |
| float: @lf; |
| } |
| |
| .box { |
| position: relative; |
| left: 10px; |
| margin-left: 20px; |
| float: left; |
| } |
| |
| |
| .inner{ |
| @cl: #f00; |
| color:@cl; |
| } |
| |
| .inner { |
| color: #ff0000; |
| } |
| |
| |
| .inner{ |
| @cl: #f00; |
| color:@cl; |
| } |
| |
| .inner { |
| color: #ff0000; |
| } |
3.嵌套
我们可以在一个选择器中嵌套另一个选择器来实现继承,这样很大程度减少了代码量,并且代码看起来更加的清晰。 - & 表示选择父元素
| //less |
| #header { |
| h1 { |
| font-size: 26px; |
| font-weight: bold; |
| } |
| p { font-size: 12px; |
| a { text-decoration: none; |
| &:hover { border-width: 1px } |
| } |
| } |
| } |
| //css |
| #header h1 { |
| font-size: 26px; |
| font-weight: bold; |
| } |
| #header p { |
| font-size: 12px; |
| } |
| #header p a { |
| text-decoration: none; |
| } |
| #header p a:hover { |
| border-width: 1px; |
| } |
| //less |
| #header { |
| h1 { |
| font-size: 26px; |
| font-weight: bold; |
| } |
| p { font-size: 12px; |
| a { text-decoration: none; |
| &:hover { border-width: 1px } |
| } |
| } |
| } |
| //css |
| #header h1 { |
| font-size: 26px; |
| font-weight: bold; |
| } |
| #header p { |
| font-size: 12px; |
| } |
| #header p a { |
| text-decoration: none; |
| } |
| #header p a:hover { |
| border-width: 1px; |
| } |
4.混合
混合可以将一个定义好的class A轻松的引入到另一个class B中,从而简单实现class B继承class A中的所有属性。我们还可以带参数地调用,就像使用函数一样。
| |
| .bg (@bg: #f00) { |
| background-color: @bg; |
| } |
| |
| #header { |
| .bg; |
| } |
| #main { |
| .bg(); |
| } |
| #footer { |
| .bg(#0f0); |
| font-size: 16px; |
| } |
| |
| #header { |
| background-color: #ff0000; |
| } |
| #main { |
| background-color: #ff0000; |
| } |
| #footer { |
| background-color: #00ff00; |
| font-size: 16px; |
| } |
| |
| .bg (@bg: #f00) { |
| background-color: @bg; |
| } |
| |
| #header { |
| .bg; |
| } |
| #main { |
| .bg(); |
| } |
| #footer { |
| .bg(#0f0); |
| font-size: 16px; |
| } |
| |
| #header { |
| background-color: #ff0000; |
| } |
| #main { |
| background-color: #ff0000; |
| } |
| #footer { |
| background-color: #00ff00; |
| font-size: 16px; |
| } |
| |
| .box1 (@w:100px, @h:200px) { |
| width: @w; |
| height: @h; |
| } |
| .wrap1 { |
| .box1; |
| } |
| .wrap2 { |
| .box1(200px,100px); |
| } |
| .wrap3 { |
| .box1(200px); |
| } |
| .wrap4 { |
| .box1(@h:100px); |
| } |
| .wrap5 { |
| .box1(); |
| } |
| |
| .wrap1 { |
| width: 100px; |
| height: 200px; |
| } |
| .wrap2 { |
| width: 200px; |
| height: 100px; |
| } |
| .wrap3 { |
| width: 200px; |
| height: 200px; |
| } |
| .wrap4 { |
| width: 100px; |
| height: 100px; |
| } |
| .wrap5 { |
| width: 100px; |
| height: 200px; |
| } |
| |
| .box1 (@w:100px, @h:200px) { |
| width: @w; |
| height: @h; |
| } |
| .wrap1 { |
| .box1; |
| } |
| .wrap2 { |
| .box1(200px,100px); |
| } |
| .wrap3 { |
| .box1(200px); |
| } |
| .wrap4 { |
| .box1(@h:100px); |
| } |
| .wrap5 { |
| .box1(); |
| } |
| |
| .wrap1 { |
| width: 100px; |
| height: 200px; |
| } |
| .wrap2 { |
| width: 200px; |
| height: 100px; |
| } |
| .wrap3 { |
| width: 200px; |
| height: 200px; |
| } |
| .wrap4 { |
| width: 100px; |
| height: 100px; |
| } |
| .wrap5 { |
| width: 100px; |
| height: 200px; |
| } |
| |
| .shadow (@x,@y,@b,@c){ |
| -webkit-box-shadow:@arguments; |
| -moz-box-shadow: @arguments; |
| -o-box-shadow:@arguments; |
| -ms-box-shadow: @arguments; |
| box-shadow: @arguments; |
| } |
| .box{ |
| .shadow(1px,1px,2px,#f00); |
| } |
| |
| .box { |
| -webkit-box-shadow: 1px 1px 2px #ff0000; |
| -moz-box-shadow: 1px 1px 2px #ff0000; |
| -o-box-shadow: 1px 1px 2px #ff0000; |
| -ms-box-shadow: 1px 1px 2px #ff0000; |
| box-shadow: 1px 1px 2px #ff0000; |
| } |
| |
| .shadow (@x,@y,@b,@c){ |
| -webkit-box-shadow:@arguments; |
| -moz-box-shadow: @arguments; |
| -o-box-shadow:@arguments; |
| -ms-box-shadow: @arguments; |
| box-shadow: @arguments; |
| } |
| .box{ |
| .shadow(1px,1px,2px,#f00); |
| } |
| |
| .box { |
| -webkit-box-shadow: 1px 1px 2px #ff0000; |
| -moz-box-shadow: 1px 1px 2px #ff0000; |
| -o-box-shadow: 1px 1px 2px #ff0000; |
| -ms-box-shadow: 1px 1px 2px #ff0000; |
| box-shadow: 1px 1px 2px #ff0000; |
| } |
5.继承
继承Extend,它允许一个选择器继承另一个选择器的样式。
| //less |
| nav{ |
| width: 100px; |
| height: 50px; |
| } |
| ul:extend(nav){ |
| background: #f00; |
| } |
| ol{ |
| &:extend(nav); |
| background:#0f0; |
| } |
| //css |
| nav, |
| ul, |
| ol { |
| width: 100px; |
| height: 50px; |
| } |
| ul { |
| background: #f00; |
| } |
| ol { |
| background: #0f0; |
| } |
| //less |
| nav{ |
| width: 100px; |
| height: 50px; |
| } |
| ul:extend(nav){ |
| background: #f00; |
| } |
| ol{ |
| &:extend(nav); |
| background:#0f0; |
| } |
| //css |
| nav, |
| ul, |
| ol { |
| width: 100px; |
| height: 50px; |
| } |
| ul { |
| background: #f00; |
| } |
| ol { |
| background: #0f0; |
| } |
| //less |
| nav{ |
| width: 100px; |
| height: 50px; |
| } |
| .color{ |
| color:#00f; |
| } |
| ul:extend(nav, .color){ |
| background: #f00; |
| } |
| //css |
| nav, |
| ul { |
| width: 100px; |
| height: 50px; |
| } |
| .color, |
| ul { |
| color: #00f; |
| } |
| ul { |
| background: #f00; |
| } |
| //less |
| nav{ |
| width: 100px; |
| height: 50px; |
| } |
| .color{ |
| color:#00f; |
| } |
| ul:extend(nav, .color){ |
| background: #f00; |
| } |
| //css |
| nav, |
| ul { |
| width: 100px; |
| height: 50px; |
| } |
| .color, |
| ul { |
| color: #00f; |
| } |
| ul { |
| background: #f00; |
| } |
6.运算
less运算提供了加,减,乘,除操作;我们可以做属性值和颜色的运算,这样就可以实现属性值之间的复杂关系。
| |
| @the-border: 1px; |
| @base-color: #111; |
| @red: #842210; |
| |
| #header { |
| color: @base-color * 3; |
| border-left: @the-border; |
| border-right: @the-border * 2; |
| } |
| #footer { |
| color: @base-color + #030; |
| } |
| |
| #header { |
| color: #333333; |
| border-left: 1px; |
| border-right: 2px; |
| } |
| #footer { |
| color: #114411; |
| } |
| |
| @the-border: 1px; |
| @base-color: #111; |
| @red: #842210; |
| |
| #header { |
| color: @base-color * 3; |
| border-left: @the-border; |
| border-right: @the-border * 2; |
| } |
| #footer { |
| color: @base-color + #030; |
| } |
| |
| #header { |
| color: #333333; |
| border-left: 1px; |
| border-right: 2px; |
| } |
| #footer { |
| color: #114411; |
| } |
7.引入
- 你可以在当前文件中通过下面的形势引入 .less 文件, .less 后缀可带可不带:
| @import "lib.less"; |
| @import "lib"; |
| @import "lib.less"; |
| @import "lib"; |
- 如果你想导入一个CSS文件而且不想LESS对它进行处理,只需要使用.css后缀就可以,这样LESS就会跳过它不去处理它.
| @import "lib.css"; |
| @import "lib.css"; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)