LESS父選擇器

2018-01-09 12:50 更新

描述

如果您需要以除默認(rèn)值之外的其他方式組合嵌套規(guī)則的選擇器,則 Parent選擇器運(yùn)算符有許多用途。 另一個(gè)典型的使用& 是重復(fù)生成類名。

例子

下面的例子演示了在LESS文件中重復(fù)生成類名:

<html>
<head>
  <link rel="stylesheet" href="style.css" type="text/css" />
  <title>Parent Selector</title>
</head>
<body>
<h2>Welcome to W3Cschool</h2>
<p class="select-first">It is possible to reference the parent selector by using &(ampersand) operator.</p>
<p class="select-second">It is possible to reference the parent selector by using &(ampersand) operator</p>
<p class="select-third">It is possible to reference the parent selector by using &(ampersand) operator.</p>
</body>
</html>

接下來(lái),創(chuàng)建文件 style.less

風(fēng)格

.select {
  &-first {
    background-color: #58D3F7;
  }
  &-second {
    background-color: #F5F6CE;
  }

  &-third {
    background-color: #F5A9E1;
  }
}

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

lessc style.less style.css

接下來(lái)執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 style.css 文件:

style.css

.select-first {
  background-color: #58D3F7;
}
.select-second {
  background-color: #F5F6CE;
}
.select-third {
  background-color: #F5A9E1;
}

輸出

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

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

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

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)