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ò)誤消息,如下所示。
:nth-child(n+2) { color: #BF70A5; font-style: italic; }
在屬性選擇器中,引用類型不重要,您可以在以下示例中看到它:
下面的例子演示了在LESS文件中使用nth表達(dá)式:
<!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>
接下來(lái),創(chuàng)建文件 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
接下來(lái)執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 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 文件中。
在瀏覽器中打開(kāi)此HTML文件,將顯示如下輸出。
更多建議: