LESS - Mixins 中的选择器

描述

mixins 不仅可以包含属性,还可以包含选择器。

示例

以下示例演示了在 LESS 文件中使用 mixin 选择器

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Selectors in Mixins</title>
   </head>

   <body>
      <h2>Welcome to Tutorialspoint</h2>
      <a href="http://www.tutorialspoint.com/">Tutorialspoint</a>
   </body>
</html>

接下来,创建 style.less 文件。

style.less

.mixin() {
   &:hover {
      background: #FFC0CB;
   }
}

a {
   .mixin();
}

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

lessc style.less style.css

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

style.css

a:hover {
   background: #FFC0CB;
}

输出

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

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

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

LESS Mixin Selectors

less_mixins.html