LESS - nth 表示式



描述

nth 表示式的形式在擴充套件中很重要,否則它會將選擇器視為不同的。nth 表示式1n+2n+2是等價的,但擴充套件將此表示式視為不同的。

例如,建立一個包含以下程式碼的 LESS 檔案:

:nth-child(n+2) {
   color: #BF70A5;
   font-style: italic;
}
.child:extend(:nth-child(1n+2)){}

當我們在命令提示符中編譯上述程式碼時,您將收到如下所示的錯誤訊息。

Less Extend

編譯後,您將獲得以下 CSS 程式碼。

:nth-child(n+2) {
   color: #BF70A5;
   font-style: italic;
}

在屬性選擇器中,引號型別並不重要,您可以在以下示例中看到:

示例

以下示例演示了在 LESS 檔案中使用nth 表示式:

extend_syntax.htm

<!doctype html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <div class = "style">
         <h2>Hello!!!!!</h2>
      </div>
      <p class = "img">Welcome to TutorialsPoint</p>
   </body>
</html>

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

style.less

[title = tutorialspoint] {
   font-style: italic;
}

[title = 'tutorialspoint'] {
   font-style: italic;
}

[title = "tutorialspoint"] {
   font-style: italic;
}
.style:extend([title = tutorialspoint]) {}
.container:extend([title = 'tutorialspoint']) {}
.img:extend([title = "tutorialspoint"]) {}

您可以使用以下命令將style.less檔案編譯為style.css

lessc style.less style.css

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

style.css

[title = tutorialspoint],
.style,
.container,
.img {
   font-style: italic;
}

[title = 'tutorialspoint'],
.style,
.container,
.img {
   font-style: italic;
}

[title = "tutorialspoint"],
.style,
.container,
.img {
   font-style: italic;
}

輸出

按照以下步驟檢視上述程式碼的工作原理:

  • 將上述 html 程式碼儲存在extend_syntax.htm檔案中。

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

Less Extend
less_extend.htm
廣告

© . All rights reserved.