我正在为我当前的项目使用Sass (.scss)。
下面的例子:
HTML
<div class="container desc">
<div class="hello">
Hello World
</div>
</div>
SCSS
.container {
background:red;
color:white;
.hello {
padding-left:50px;
}
}
这很有效。
我可以处理多个类同时使用嵌套样式。
在上面的例子中,我说的是:
CSS
.container.desc {
background:blue;
}
在这种情况下,所有div.container通常是红色的,但div.container.desc将是蓝色的。
我如何能巢这个内部容器与Sass?
如果是这种情况,我认为您需要使用更好的方法来创建类名或类名约定。例如,如您所说,您希望.container类根据特定的用法或外观具有不同的颜色。你可以这样做:
SCSS
.container {
background: red;
&--desc {
background: blue;
}
// or you can do a more specific name
&--blue {
background: blue;
}
&--red {
background: red;
}
}
CSS
.container {
background: red;
}
.container--desc {
background: blue;
}
.container--blue {
background: blue;
}
.container--red {
background: red;
}
上面的代码基于类命名约定中的BEM方法。你可以检查这个链接:BEM -块元素修饰器方法学
如果是这种情况,我认为您需要使用更好的方法来创建类名或类名约定。例如,如您所说,您希望.container类根据特定的用法或外观具有不同的颜色。你可以这样做:
SCSS
.container {
background: red;
&--desc {
background: blue;
}
// or you can do a more specific name
&--blue {
background: blue;
}
&--red {
background: red;
}
}
CSS
.container {
background: red;
}
.container--desc {
background: blue;
}
.container--blue {
background: blue;
}
.container--red {
background: red;
}
上面的代码基于类命名约定中的BEM方法。你可以检查这个链接:BEM -块元素修饰器方法学