Hi Harry,
I haven't seen any particular examples that use this pattern, so I'm not sure if I might be "doing it wrong".
Consider that I have a button:
<button class="c-button c-button--large">Click Me</button>
And I want to use the button inside a card, but I don't want to have to repeat the styles. And I'd rather make it clear in the markup that the card is extending the styles of the button - as opposed to using a SASS mixin or @extend in the CSS.
<div class="c-card">
<button class="c-card__button c-button c-button--large">Click Me</button>
</div>
The c-card__button class still has low specificity and I DON'T do this with the selectors in my CSS:
// I DON'T do this
.c-card__button .c-button {
// styles
}
So it's not actually nested in the conventional sense - it doesn't increase specificity via nesting.
The c-card__button will add styles that are relevant only in the card - it may be positioning or some extra spacing or whatever - but ultimately those styles would only be relevant when the c-button is used inside the c-card.
Harry Roberts
Consultant Front-end Architect
Ahhh this is such a good question! And you’ve inadvertently discovered/reinvented BEM mixes , which are about as advanced as BEM gets.
This kind of contextual styling has a number of potential solutions, and the correct one is the exact same as you’ve come up with. The addition of the third
.c-card__buttonclass is the Mix pattern. Good work 👍🏼H