BEM CSS: Similar blocks and style sharing

最后都变了- 提交于 2019-12-10 13:23:28

问题


Reading about BEM CSS and having coded some small sites using - I am fairly familiar with it. However, I'm still unsure about how to deal with blocks that are very similar, but have no relation.

Say I have lots of unordered list blocks, which all have an identical style for the top row. The other list items can be laid out differently and are all completely unrelated to each other.

I find myself naming the block what it is (e.g. 'latest-news', 'upcoming-events'), and then it becomes cumbersome to stack all these blocks in the CSS - not to mention hard to manage.

Appreciate this stuff isn't a one size fits all solution; but imagine lots of folks come up against the same questions. Would it not be more efficient to call these blocks, 'standard-list' and then have the list items as blocks?

Just seems against the whole principle of what BEM is trying to achieve. I should be able to put 'latest-news' anywhere I want. This way I'd have to just get the correct, 'standard-list' that held the latest news content?

Hope that isn't too confusing! Any advice would be great!


回答1:


There's one important thing which often slips out of sight — usage of mixes.

A mix is a possibility to put a few different blocks on the same DOM node.

So for your example you may create

<ul class="latest-news list">

which will give you all the common styling needed for lists and add peculiarities just for latest-news.



来源:https://stackoverflow.com/questions/25611948/bem-css-similar-blocks-and-style-sharing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!