更少 - 組合爆炸



描述

& 可以獲得用逗號分隔的列表中的選擇器的所有可能排列。

示例

以下示例演示了在 LESS 檔案中使用 & 獲得選擇器的所有可能排列:-

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Combinatorial Explosion</title>
   </head>

   <body>
      <p>This is first paragraph.</p>
      <p>This is second paragraph which is adjecent to first paragraph ( i.e. p + p ). This will be highlighted.</p>
      <div>
         This div is adjecent to second paragraph ( i.e. p + div ). This will be highlighted.
      </div>

      <p>This is third paragraph adjecent to div ( i.e. p + div ). This will be highlighted.</p>
      <i>This is italic. This will not be highlighted since there is no (p + i) in CSS</i>
      <div>This is second div</div>
      <div>This is div adjecent to second div ( i.e. div + div ). This will be highlighted</div>
   </body>
</html>

接下來,建立 style.less 檔案。

style.less

p, div {
   color : red;
   font-family:Lucida Console;
   & + & {
      color : green;
      background-color: yellow;
      font-family: "Comic Sans MS";
   }
}

你可以使用以下命令將 style.less 編譯到 style.css:-

lessc style.less style.css

執行以上命令;它將自動建立 style.css 檔案,其中包含以下程式碼:-

style.css

p,
div {
   color: red;
   font-family: Lucida Console;
}

p + p,
p + div,
div + p,
div + div {
   color: green;
   background-color: yellow;
   font-family: "Comic Sans MS";
}

輸出

按照以下步驟瞭解以上程式碼如何工作:-

  • 將以上 html 程式碼儲存在 combinatorial_explosion.html 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。

LESS Combinatorial Explosion
less_parent_selectors.htm
廣告
© . All rights reserved.