【发布时间】:2020-02-11 04:22:17
【问题描述】:
我正在尝试学习 BEM,但很难理解它的好处。举个例子,我去Tailwind's Utility-First页面抓取这段代码:
<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;
}
看的时候不禁觉得应该简化成就行了
<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;
}
我基本上简化了 HTML,去掉了不必要的包装器、divitis、classitis。我的问题是,当 img、h4、p 等标签已经非常独特和语义化时,为什么像 __logo-wrapper 和 __title 这样的类描述符很有用?
【问题讨论】:
标签: bem