Login
芋圆社区 > 编程 > Less > Less的嵌套规则

Less的嵌套规则

949
0
2022-06-16
2022-06-16
Hey、小怪兽

  • ① 简单的嵌套,更有层级,实际写起来也不需要考虑优先级的问题
  • .div1 {
        color: red;
        .div2 {
            color: blue;
            .div3 {
                color: green;
            }
        }
    }
  • • 编译后:
  • .div1 {
        color: red;
    }
    
    .div1 .div2 {
        color: blue;
    }
    
    .div1 .div2 .div3 {
        color: green;
    }
    
  • ② 添加hover效果,用&(表示当前选择器的父级)
  • div {
        color: red;
        &:hover {
            color: blue;
        }
    }
  • • 编译后:
  • div {
        color: red;
    }
    div:hover {
        color: blue;
    }
  • ③ "父选择器"运算符&可以产生重复的类名
  • .button {
      &-ok {
        background-image: url("ok.png");
      }
    
      &-cancel {
        background-image: url("cancel.png");
      }
    
      &-custom {
        background-image: url("custom.png");
      }
    }
  • • 编译后:
  • .button-ok {
      background-image: url("ok.png");
    }
    .button-cancel {
      background-image: url("cancel.png");
    }
    .button-custom {
      background-image: url("custom.png");
    }
  • ④ &在选择器中可能出现多次,这使得重复引用父选择器而不重复其名称成为可能
  • .link {
      & + & {
        color: red;
      }
    
      & & {
        color: green;
      }
    
      && {
        color: blue;
      }
    
      &, &ish {
        color: cyan;
      }
    }
  • • 编译后:
  • .link + .link {
      color: red;
    }
    .link .link {
      color: green;
    }
    .link.link {
      color: blue;
    }
    .link, .linkish {
      color: cyan;
    }
  • ⑤ &代表所有父选择器(不仅仅是最近的祖先)
  • .grand {
      .parent {
        & > & {
          color: red;
        }
      }
    }
  • • 编译后:
  • .grand .parent > .grand .parent {
      color: red;
    }
  • ⑥ 组合爆炸,&也可用于在逗号分隔列表中生成选择器的所有可能排列
  • p, a, ul, li {
      border-top: 2px dotted #366;
      & + & {
        border-top: 0;
      }
    }
  • • 编译后:
  • // 这扩展到指定元素的所有可能 (16) 组合:
    p, a, ul, li {
      border-top: 2px dotted #366;
    }
    p + p, p + a, p + ul, p + li,
    a + p, a + a, a + ul, a + li,
    ul + p, ul + a, ul + ul, ul + li,
    li + p, li + a, li + ul, li + li {
      border-top: 0;
    }

上一篇:Less的注释和变量

下一篇:Less的混合Mixins

Message Board
回复
回复内容不允许为空
留言字数要大于2,小于200!
提交成功,5s后刷新页面!
编程导航

Less的注释和变量

Less的嵌套规则

Less的混合Mixins

Less的运算和作用域

Less的映射和逻辑函数

Less的合并与继承

Copyright © 2020 芋圆社区

Powered by 浙ICP备2020039309号-1

此页面不支持夜间模式!

已进入夜间模式!

已进入普通模式!

搜索框不允许为空

签到成功!经验+5!芋圆币+2!

签到失败!今日已签到!

需要登录社区账号才可以进入!

复制成功
寄,页面未加载完成或页面无锚点