less

2023年05月02日 10:59 · 阅读(92) ·

css 原生变量设置

● 定义变量

  1. html {
  2. /* css 原生也支持变量的设置 */
  3. --color: #bfa;
  4. --length: 100px;
  5. }

● 使用变量

  1. .box1 {
  2. width: var(--length);
  3. height: var(--length);
  4. background-color: var(--color);
  5. }

less 简介

来源:less 简介

less 是一门 css 预处理语言,less 是一个 css 的增强版,通过 less 可以编写更少的代码实现更强大的样式

less 中添加了许多的新特性:

● 对变量的支持
● 对 mixin 的支持
● ……

less 语法大体上和 css 语法一致,但是 less 中增加了许多对 css 的扩展
所以浏览器无法直接执行 less 代码,要执行必须将 less 转换为 css,然后再由浏览器执行。

less 插件

Easy Less

VSCode 中添加插件 Easy Less

less 插件测试

项目中新建文件 css-style.less

编写下面内容

  1. body {
  2. width: 100px;
  3. height: 100px;
  4. div {
  5. color: red;
  6. }
  7. }

保存后,同文件夹下自动生成了 style.css,内容如下

  1. body {
  2. width: 100px;
  3. height: 100px;
  4. }
  5. body div {
  6. color: red;
  7. }

less 注释

作用
// less 中的单行注释,不会被解析到 css 文件中
/**/ less 中的多行注释,会被解析到 css 文件中

less 变量

来源:less 变量

变量,在变量中可以存储一个任意的值
并且我们可以在需要时,任意修改变量中的值

● 变量定义

变量的语法:@变量名:变量值

  1. @a:100px;
  2. @b:#bfa;
  3. @c:box6;
  4. @img: '../images/';
  5. @a:200px;

● 变量使用

  1. .box1 {
  2. //使用变量时,如果是直接使用则以 @变量名 的形式使用即可。
  3. width: @a;
  4. color: @b;
  5. }
  6. //作为类名或者一部分值使用时,必须以 @{变量名} 的形式使用
  7. .@{c} {
  8. width: @a;
  9. background-image: url("@{img}1.png");
  10. height: @d;
  11. }
  12. //可以在变量声明之前使用变量
  13. @d:335px;
  14. div{
  15. width: @a;
  16. //引用 width 的宽度
  17. height: $width;
  18. }

生成 css 效果

  1. .box1 {
  2. /*334*/
  3. background-color: #bfa;
  4. }
  5. .box1 {
  6. width: 200px;
  7. color: #bfa;
  8. }
  9. .box6 {
  10. width: 200px;
  11. background-image: url("../images/1.png");
  12. height: 335px;
  13. }
  14. div {
  15. width: 200px;
  16. height: 200px;
  17. }

父元素和扩展

来源:父元素和扩展

● 父元素

定义

  1. /*&*/
  2. .box1 {
  3. >.box2 {
  4. color: red;
  5. //& 表示外层的父元素 box2
  6. &:hover {
  7. color: blue;
  8. }
  9. }
  10. //为 box1 设置一个 hover
  11. //& 表示外层的父元素 box1
  12. &:hover {
  13. color: orange;
  14. }
  15. }

生成 css

  1. /*&*/
  2. .box1 > .box2 {
  3. color: red;
  4. }
  5. .box1 > .box2:hover {
  6. color: blue;
  7. }
  8. .box1:hover {
  9. color: orange;
  10. }

● 扩展

  1. /*extend*/
  2. .p1 {
  3. width: 100px;
  4. height: 200px;
  5. }
  6. //:extend() 对当前选择器扩展指定选择器的样式(选择器分组)
  7. .p2:extend(.p1) {
  8. color: red;
  9. }
  10. .p3 {
  11. //直接对指定的样式进行引用,这里相当于将 p1 的样式在这里进行了复制
  12. //mixin 混合
  13. .p1();
  14. }
  15. //使用类选择器时可以在选择器后面添加 (),这时实际上创建了一个 mixins
  16. .p4() {
  17. width: 100px;
  18. height: 100px;
  19. background-color: blue;
  20. }
  21. .p5 {
  22. .p4();
  23. }

生成 css

  1. /*extend*/
  2. .p1,
  3. .p2 {
  4. width: 100px;
  5. height: 200px;
  6. }
  7. .p2 {
  8. color: red;
  9. }
  10. .p3 {
  11. width: 100px;
  12. height: 200px;
  13. }
  14. .p5 {
  15. width: 100px;
  16. height: 100px;
  17. background-color: blue;
  18. }

混合函数

来源:混合函数

● 定义使用

  1. //混合函数,在混合函数中可以直接设置变量
  2. .test(@w,@h,@bg-color) {
  3. width: @w;
  4. height: @w;
  5. border: 1px solid @bg-color;
  6. }
  7. div {
  8. //调用一个混合函数,按顺序传递参数
  9. //.test(100px,200px,blue);
  10. //调用一个混合函数,按指定参数名称传递参数
  11. .test(@bg-color:red,@h:100px,@w:200px);
  12. }
  13. span {
  14. //调用 less 定义的函数,取颜色中间值
  15. color: average(red,yellow);
  16. }

● 生成的 css

  1. div {
  2. width: 200px;
  3. height: 200px;
  4. border: 1px solid red;
  5. }
  6. span {
  7. color: #ff8000;
  8. }

less 补充

来源:less 补充

import

  1. //import 将其它 less 引入当前文件,当前文件生成 css 时,会加入引入文件生成的 css
  2. @import "syntax2.less";
  3. div {
  4. }

运算

● 定义 less

  1. div {
  2. //在 less 中所有的数值都可以直接进行运算
  3. // + - * /
  4. width: 200px + 100px;
  5. height: 200px * 2;
  6. border: 1px solid red;
  7. }

● 生成的 css

  1. div {
  2. width: 300px;
  3. height: 400px;
  4. border: 1px solid red;
  5. }

浏览器中定位 less

因为 less 最终是生成 css 在浏览器中呈现,假如某个元素需要修改,怎么定位到具体的 less 呢?

● 找到 less 插件,复制下面的内容

● VSCode 设置

设置-扩展-Easy LESS configuration-在 settings.json 中编辑

设置下面内容,主要是 "sourceMap": true

  1. {
  2. "typescript.locale": "zh-CN",
  3. "editor.fontSize": 18,
  4. "emmet.triggerExpansionOnTab": true,
  5. "liveServer.settings.AdvanceCustomBrowserCmdLine": "",
  6. "liveServer.settings.ChromeDebuggingAttachment": false,
  7. "liveServer.settings.port": 5500,
  8. "files.autoSave": "afterDelay",
  9. "less.compile": {
  10. "compress": false,
  11. "sourceMap": true,
  12. "out": true
  13. }
  14. }

● other.less

  1. div {
  2. //在 less 中所有的数值都可以直接进行运算
  3. // + - * /
  4. width: 200px + 100px;
  5. height: 200px * 2;
  6. border: 1px solid red;
  7. }

● 同目录生成了 other.css

  1. div {
  2. width: 300px;
  3. height: 400px;
  4. border: 1px solid red;
  5. }
  6. /*# sourceMappingURL=./other.css.map */

● 同目录生成了 other.css.map

  1. {"version":3,"sources":["other.less"],"names":[],"mappings":"AAAA;EAGI,YAAA;EACA,aAAA;EACA,qBAAA"}

● 测试页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <link rel="stylesheet" href="./css/other.css">
  7. </head>
  8. <body>
  9. <div class="box1"></div>
  10. </body>
  11. </html>

● 浏览器测试