KittyGiraudel / ama

Ask me anything!
43 stars 4 forks source link

BEM syntax regarding nested elements #47

Closed zhouzi closed 9 years ago

zhouzi commented 9 years ago

Hey there! We've been discussing about the BEM syntax regarding nested elements with a friend who prefers the recommended way, e.g:

<div class="person">
  <div class="person__head">
    <div class="person__eye"></div>
  </div>
</div>

I am ok with this notation as long as person__eye can be moved anywhere within person. But then, what if it actually depends on person__head's styling? I believe that writing it like person__head__eye makes it clear that the eye element depends on the head that itself depends on person. Should the "head" be moved to its own block? Isn't the purpose of BEM to make elements' dependencies clearer and more readable?

I know that it's up to everyone to adapt the "convention" to its needs but I'd be curious to have your opinion on this :)

KittyGiraudel commented 9 years ago

Hey.

.person__eye should be movable anywhere inside .person. If you want the eye to be restricted to the head, you need a head block (.head) with an eye element (.head__eye).

<div class="person">
  <div class="person__head head">
    <div class="head__eye"></div>
  </div>
</div>

I hope it helps.

zhouzi commented 9 years ago

I am a bit confused by the combination of person__head and head, any chance you could elaborate? Are they completely independent or one of them depends on the presence of the other? If so, isn't this one becoming a modifier? And otherwise, isn't mixing block classes a bit risky?

Then, what happens if we need to create a new block animal with its own head and eyes? Will we need to apply a animal__head class along with animal-head? e.g:

<div class="person">
  <div class="person__head person-head">
    <div class="person-head__eye"></div>
  </div>
</div>

<div class="animal">
  <div class="animal__head animal-head">
    <div class="animal-head__eye"></div>
  </div>
</div>
KittyGiraudel commented 9 years ago

Like so:

<div class="person">
  <div class="person__head head head--person">
    <div class="head__eye"></div>
  </div>
</div>

<div class="animal">
  <div class="animal__head head head--animal">
    <div class="head__eye head--animal__eye"></div>
  </div>
</div>
zhouzi commented 9 years ago

That's pretty interesting, thank you very much for sharing! :)