- 語言特性
- Less - 巢狀規則
- Less - 巢狀指令和冒泡
- Less - 運算
- Less - 轉義
- Less - 函式
- Less - 名稱空間和訪問器
- Less - 作用域
- Less - 註釋
- Less - 匯入
- Less - 變數
- Less - 擴充套件
- Less - Mixin
- Less - 引數化 Mixin
- Less - Mixin 作為函式
- Less - 向 Mixin 傳遞規則集
- Less - 匯入指令
- Less - 匯入選項
- Less - Mixin 保護
- Less - CSS 保護
- Less - 迴圈
- Less - 合併
- Less - 父選擇器
- 函式
- Less - 其他函式
- Less - 字串函式
- Less - 列表函式
- Less - 數學函式
- Less - 型別函式
- Less - 顏色定義函式
- Less - 顏色通道函式
- Less - 顏色運算
- Less - 顏色混合函式
- 用法
- Less - 命令列用法
- 在瀏覽器中使用 Less
- Less - 瀏覽器支援
- Less - 外掛
- Less - 程式設計方式使用
- Less - 線上編譯器
- Less - 圖形使用者介面
- Less - 編輯器和外掛
- Less - 第三方編譯器
- Less - 框架
- Less 有用資源
- Less - 快速指南
- Less - 有用資源
- Less - 討論
LESS - 父選擇器
描述
父選擇器運算子有很多用途,例如,當您需要以與預設方式不同的方式組合巢狀規則的選擇器時。& 的另一個典型用途是重複生成類名。
示例
以下示例演示如何在 LESS 檔案中重複生成類名:
<html>
<head>
<link rel = "stylesheet" href = "style.css" type = "text/css" />
<title>Parent Selector</title>
</head>
<body>
<h2>Welcome to TutorialsPoint</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>
現在建立 style.less 檔案。
style.less
.select {
&-first {
background-color: #58D3F7;
}
&-second {
background-color: #F5F6CE;
}
&-third {
background-color: #F5A9E1;
}
}
您可以使用以下命令將 style.less 檔案編譯為 style.css:
lessc style.less style.css
執行上述命令;它將自動建立包含以下程式碼的 style.css 檔案:
style.css
.select-first {
background-color: #58D3F7;
}
.select-second {
background-color: #F5F6CE;
}
.select-third {
background-color: #F5A9E1;
}
輸出
按照以下步驟檢視上述程式碼的工作方式:
將上述 html 程式碼儲存在 parent_selector22.htm 檔案中。
在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。
less_parent_selectors.htm
廣告