以下是一个使用嵌套属性的简单示例:
// 使用嵌套属性
button {
font: {
weight: bold;
size: 16px;
family: 'Helvetica', sans-serif;
}
margin: {
top: 10px;
right: 20px;
bottom: 10px;
left: 20px;
}
padding: {
top: 5px;
right: 10px;
bottom: 5px;
left: 10px;
}
&:hover {
background-color: #eee;
}
}
在这个例子中,font、margin 和 padding 是嵌套属性块,它们分别包含了相关的子属性。当 Sass 编译成 CSS 时,嵌套属性将被展开为相应的 CSS 属性:
button {
font-weight: bold;
font-size: 16px;
font-family: 'Helvetica', sans-serif;
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
padding-top: 5px;
padding-right: 10px;
padding-bottom: 5px;
padding-left: 10px;
}
button:hover {
background-color: #eee;
}
使用嵌套属性可以使样式表更结构化,减少冗余的代码,提高可维护性。然而,需要注意不要滥用嵌套,以免导致生成的 CSS 过于复杂。
转载请注明出处:http://www.zyzy.cn/article/detail/6171/SASS