LESS-nth表達(dá)式

2018-01-10 18:24 更新

描述

nth表達(dá)式的形式在擴(kuò)展中很重要,否則它將選擇器視為不同。 nth表達(dá)式1n + 2和n + 2是等效的,但擴(kuò)展將該表達(dá)式視為不同。


例如,使用以下代碼創(chuàng)建一個(gè)LESS文件:

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


當(dāng)我們?cè)诿钐崾痉芯幾g上面的代碼時(shí),你會(huì)得到一個(gè)錯(cuò)誤消息,如下所示。


錯(cuò)誤消息


編譯之后,您將獲得以下CSS代碼。
:nth-child(n+2) {
  color: #BF70A5;
  font-style: italic;
}


在屬性選擇器中,引用類型不重要,您可以在以下示例中看到它:

例子

下面的例子演示了在LESS文件中使用nth表達(dá)式:

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 W3Cschool</p>
</body>
</html>


接下來,創(chuàng)建文件 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


接下來執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 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;
}


輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上述html代碼保存在 extend_syntax.htm 文件中。

  • 在瀏覽器中打開此HTML文件,將顯示如下輸出。


輸出

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)