目录

点击这里

描述 (Description)

扩展选择器时,将在引用@import语句的位置@import新选择器,并且仅将新选择器标记为not referenced 。 我们可以使用extend来避免这种冗余,只引入特定的目标样式。

例子 (Example)

以下示例演示了在LESS文件中使用extend方法 -

<html>
   <head>
     <link rel = "stylesheet" href = "style.css" type = "text/css" />
     <title>Import Options Extend</title>
   </head>
   <body>
      <h1>Welcome to IoWiki</h1>
      <p class = "para_1">LESS is a CSS pre-processor that enables customizable, manageable and reusable style sheet for web site.</p>
      <p class = "para_2">LESS is a CSS pre-processor that enables customizable, manageable and reusable style sheet for web site.</p>
   </body>
</html>

现在创建style.less文件。

style.less

@import (reference)  "http://www.iowiki.com/less/import_options_extend.less";
.para_1 {
   &:extend(.style1);
   background-color: #81F7F3;
}
.para_2 {
   &:extend(.style1);
   background-color: #F6E3CE;
}

以下代码将import_options_extend.less文件从https://www.iowiki.com/less/import_options_extend.less路径导入style.less文件 -

import_options_extend.less

.style1 {
   color: #A0A0A0;
   font-family: "Comic Sans MS";
   font-size: 20px;
}

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

lessc style.less style.css

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

style.css

.para_1,
.para_2 {
   color: #A0A0A0;
   font-family: "Comic Sans MS";
   font-size: 20px;
}
.para_1 {
   background-color: #81F7F3;
}
.para_2 {
   background-color: #F6E3CE;
}

输出 (Output)

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

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

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

少导入选项扩展
↑回到顶部↑
WIKI教程 @2018