- 语言特性
- 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
广告