目录

点击这里

描述 (Description)

Parent selectors运算符有许多用途,例如,当您需要以默认的其他方式组合嵌套规则的选择器时。 &另一个典型用法是重复生成类名。

例子 (Example)

以下示例演示了在LESS文件中重复生成类名 -

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Parent Selector</title>
   </head>
   <body>
      <h2>Welcome to IoWiki</h2>
      <p class = "select-first">It is possible to reference the parent 
      selector by using &(ampersand) operator.</p>
      <p class = "select-second">It is possible to reference the 
      parent selector by using &(ampersand) operator</p>
      <p class = "select-third">It is possible to reference the 
      parent selector by using &(ampersand) operator.</p>
   </body>
</html>

现在创建style.less文件。

style.less

.select {
   &-first {
      background-color: #58D3F7;
   }
   &-second {
      background-color: #F5F6CE;
   }
   &-third {
      background-color: #F5A9E1;
   }
}

您可以使用以下命令将style.less文件编译为style.css -

lessc style.less style.css

执行上面的命令; 它将使用以下代码自动创建style.css文件 -

style.css

.select-first {
   background-color: #58D3F7;
}
.select-second {
   background-color: #F5F6CE;
}
.select-third {
   background-color: #F5A9E1;
}

输出 (Output)

请按照以下步骤查看上述代码的工作原理 -

  • 将上述html代码保存在parent_selector22.htm文件中。

  • 在浏览器中打开此HTML文件,将显示以下输出。

较少的父选择器
↑回到顶部↑
WIKI教程 @2018