Why are elements a thing with syntax? #246
Replies: 1 comment
-
I think there are might be a misunderstanding of some of the goals of BEM. From the documentation on why you would use it:
Before answering specific questions the main reason to use
This is true, however, because The
Relying on cascading rules can be very helpful, even in BEM. There are times and places to still rely on the cascade part of CSS. I think this is one area of general confusion in BEM, a lot of folks come to a conclusion to not use Cascade styles. The rule of thumb (for projects I have worked on) is to not make the cascading part a very specific selector, so it's easy to override in the For example, all elements of the
While technically true, this is not in the spirit of BEM. The goal of BEM is to have a semantic language to talk about the UI. If a
The choice isn't so much arbitrary, as making a choice to help readability and understandability (if that's a word) of the overall CSS. I'm pretty sure the reason for
I've tried to answer the context that I think you are missing. A great resource around CSS, that uses a lot of the same principles as BEM, is https://cssguidelin.es/ . It's a long read, but I think well worth it, and goes into detail about some of the philosophy behind ides that are also in BEM. |
Beta Was this translation helpful? Give feedback.
-
I didn't find this covered in the FAQ, so:
I get the
--
separator for modifiers is necessary - you need a reserved sequence to disambiguate from other word separators and prevent collisions.What I don't get, is why do you need the
__
separator to separate blocks and elements?Let's say you have a
panel
block, and apanel__title
element.While still following BEM, I could instead choose to merely describe the same components as a
panel
block and a secondpanel-title
block, right?These would be functionally equivalent, and we're not supposed to rely on cascading rules in any case, right?
Which would seem to imply that the
panel__title
element would work just as well without thepanel
block as a parent.So it seems the choice between the two is completely arbitrary. You don't strictly need to have any elements at all.
It also doesn't prevent collisions, except between my own
panel__title
andpanel-title
, which is totally ambiguous, and I'd never have those in the first place. Mypanel__title
element orpanel-title
block can both collide with yours - we'll need namespace prefixes to prevent that case either way.The only function I can see this having, is communicating intent - the use of the
__
could suggest "you're supposed to use these together", but again,block
andblock-title
suggest this just as well, and it's more idiomatically what CSS authors everywhere have been doing practically forever.On top of which, following that convention only suggests a direct relationship between a parent and child - it does not indicate or suggest any internal parent/child relations between individual elements, so you'll need a style guide for the precise structure of complex components anyway.
Am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions