I am trying to learn BEM and having difficulty appreciating it's benefits. As an example, I went to Tailwind's Utility-First page to grab this code:
<div class="chat-notification">
<div class="chat-notification__logo-wrapper">
<img class="chat-notification__logo" src="/img/logo.svg" alt="ChitChat Logo">
</div>
<div class="chat-notification__content">
<h4 class="chat-notification__title">ChitChat</h4>
<p class="chat-notification__message">You have a new message!</p>
</div>
</div>
.chat-notification {
display: flex;
max-width: 24rem;
margin: 0 auto;
padding: 1.5rem;
border-radius: 0.5rem;
background-color: #fff;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
.chat-notification__logo-wrapper {
flex-shrink: 0;
}
.chat-notification__logo {
height: 3rem;
width: 3rem;
}
.chat-notification__content {
margin-left: 1.5rem;
padding-top: 0.25rem;
}
.chat-notification__title {
color: #1a202c;
font-size: 1.25rem;
line-height: 1.25;
}
.chat-notification__message {
color: #718096;
font-size: 1rem;
line-height: 1.5;
}
When I look at it, I can't help but think it should be simplified to just
<div class="chat-notification">
<img src="logo.jpg" alt="ChitChat Logo">
<h4>ChitChat</h4>
<p>You have a new message!</p>
</div>
.chat-notification {
position:relative;
max-width: 24rem;
margin: 0 auto;
padding: 1.5rem;
border-radius: 0.5rem;
background-color: #fff;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
box-sizing:border-box;
padding-left: 3.5rem;
}
.chat-notification img {
position:absolute;
top:0;
left:0;
clip: rect(0,200,400,0);
width: 3rem;
}
.chat-notification h4 {
color: #1a202c;
font-size: 1.25rem;
line-height: 1.25;
}
.chat-notification p {
color: #718096;
font-size: 1rem;
line-height: 1.5;
}
I basically simplified the HTML and got rid of unnecessary wrappers, divitis, classitis. My question is why are class descriptors like __logo-wrapper
and __title
useful when tags like img
, h4
, p
are already quite distinctive and semantic?