How would you BEM this?

<div class="application-header">
<div class="inner-container">
<div class="title">
<div class="application-header">
<div class="inner-container">
<div class="title">
ChatGPT is showing me:
<div class="application-header">
<div class="header__inner-container">
<div class="header__title">
<div class="application-header">
<div class="header__inner-container">
<div class="header__title">
But that doesn't look right to me. I suck at BEM but I always thought that - denoted the modifier. I thought it should be something like this maybe?
.application-header
.application-header__inner
header__title
.application-header
.application-header__inner
header__title
7 Replies
Chris Bolson
Chris Bolson2w ago
I asked chatGPT the same and got this which I would say looks closer:
<div class="application-header">
<div class="application-header__inner-container">
<div class="application-header__title">
<div class="application-header">
<div class="application-header__inner-container">
<div class="application-header__title">
I don't use BEM so am unsure whether the title class name is correct That said, chatGPT is just basing the name on what we are giving it. I agree that application-header__innerwould be better than application-header__inner-container . ... just checked with the documentation and it states that
block structure should be flattened; you do not need to reflect nested DOM structure of the block
So I think that, in my test, chatGPT got it right for the title element
Kevin Powell
Kevin Powell2w ago
It's very hard to give good BEM advice without more context. Not everything needs to be a element when it's nested... so like, if the title is the same as titles that are outside of that element, then just use whatever class you'd use there. Same for inner-container. If it's a common pattern, you can re-use it. Most common example for this is a .button. If you have a button in your application header, you probably don't want to bother with .application-header__button If you do want to be very strict, though, I'd go with - .application-header - .application-header__inner-container (or just __inner) - .application-header__title
vince
vinceOP2w ago
Thanks guys! Yea unfortunately the markup is a mess and it's got to be very specific, so I wanted to bring some structure to the class names hence why I'm using BEM. For .application-header__inner-container, I always thought -container was the modifier part? So in that example, if we had to add a modifer to .application-header__inner-container, how would we add it?
Rägnar O'ock
Rägnar O'ock2w ago
Modifiers are after a double hyphen -- single hyphens are for separating words
vince
vinceOP2w ago
Ohhh right 😅 I knew that, not sure what I was smoking this afternoon 😂 Okay makes sense, good to know you can write BEM names like this, thanks guys!
Rägnar O'ock
Rägnar O'ock2w ago
use BEM with caution. It's pretty rare that you need the high specificity it provides, most of the time you can just do .app .container instead of .application__container and it's even worse when you write it in SCSS like you've shown because it compiles to .application .application_container which is like 3 times as long as you need it to be for no gain, you find yourself quickly with CSS files that are 30 or 40% selectors instead of actual usefull stuff
vince
vinceOP2w ago
That's a good point maybe I don't need it to be that specific, I just wanted some structure to my components since I can't edit the html but I guess I can just add wrappers (in the drag and drop thingy 😭) and just give them a class name and target with .app .container like you said

Did you find this page helpful?