Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

About BEM... What if I have nested __elements?

.list {
    margin: 0;
    padding: 0;

    // element
    &__item {
        display: inline-block;

        // Can be placed an element inside another element like this?
        &__sub-item {
            display: none;

        }

        // modifier
        &--active {
            background: red;
        }

    }

}

3 Answers

I think it may depend on how you view the submenu items... if they are just slight modifications (say, different background color) of standard .list__item elements, then you could get away with just a modifier:

<li class="list__item list__item--sub"><a href="#">Foo</a></li>
<li class="list__item list__item--sub list__item--active"><a href="#">Bar</a></li>

If the submenu could be used in places other than the .list, then perhaps treat it as its own block, which you would then style completely independently of the list block:

<ul class="submenu">
  <li class="submenu__item"><a href="#">Foo</a></li>
  <li class="submenu__item"><a href="#">Bar</a></li>
</ul>

And finally, if the nested ul tag isn't going to be modified in anyway, and only the nested li tags, then I guess you could just treat them as different elements under another list block:

<li class="list__item">
  <a href="#">Sub</a>
  <ul class="list">
    <li class="list__submenu-item"><a href="#">Foo</a></li>
    <li class="list__submenu-item"><a href="#">Bar</a></li>
  </ul>
</li>

I think the main thing is that the submenu isn't dependent on being nested within a .list__item element.

I'm no expert on BEM, but I think part of its appeal is avoiding nested CSS selectors, since they introduce complexity and can cause issues with specificity.

I think whatever HTML you planned to have nested, should be its own 'block', and independent of the parent block/element it happens to be nested in, OR just uses the higher level block name, which in your case would mean `.list__sub-item'.

Maybe paste in your HTML structure and we can figure out an appropriate naming scheme?

<ul class="list">
    <li class="list__item">
        <a href="#">Uno</a>
    </li>
    <li class="list__item">
        <a href="#">Dos</a>
    </li>
    <li class="list__item">
        <a href="#">Sub</a>
        <ul class="list__item__submenu">
            <li class="list__item__submenu__item"><a href="#">Foo</a></li>
            <li class="list__item__submenu__item"><a href="#">Bar</a></li>
        </ul>
    </li>
</ul>