使用 BEM 时更合适的方法是什么? 我们是否允许不向元素添加额外的类并设置元素本身的样式
<section class="news-section">
<a>link</a>
</section>
.news-section {
a {
color: blue;
}
}
或者我们是否必须为所有元素添加额外的类并设置这些类的样式?
<section class="news-section">
<a class="news-section_link">link</a>
</section>
.news-section {
&_link {
color: blue;
}
}
最佳答案
Are we allowed to not add extra classes to elements and style the elements themselves
没有。
使用 BEM,您必须始终使用 CSS 类:
In HTML, BEM entities are represented by the
class
attribute. (bem.info)
作为对这种刚性的返回,BEM 将为您带来:
- 可扩展性,因为 block 可以无限制地嵌套;
- 灵 active ,因为 CSS 选择器与 HTML 标记松散耦合。
关于css - 您应该在使用 BEM 时设置元素样式还是应该添加额外的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49901120/