This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging
Preface ✍ ️
- Recently I was building my own component library, about
CSS
The preprocessor uses aSCSS
. - The naming of the style is used
BEM
“, take this opportunity to share.
How to name 🔥
How hard it is to name styles
- I used to think that this is a very, very difficult problem, it is scary, it is just like the product let me make a page but no design or even prototypes just a few boxes let me make a particularly beautiful, bright color, really (tian) point (DA) difficult (PI) 🤏.
- Have you ever seen a situation where you write another module and its contents give it a style name
container
What about his left? What about the right-hand side?left-container
?right-container
? If there is content in the content, forgive my poor English, I will still want to usecontainer
, be crazy 🤷♂️. class
This is a difficult problem for most front-end programmers, but for a small group of front-end programmers it’s not worth mentioning. Why? Because I’ve seen names in orderone
.two
andthree
To arrange theclass
I guess it’s not a big deal to them 🤦♂️.- Just kidding, in normal circumstances, when we name a style, we will look up the English of the corresponding module to name it (I didn’t say good English) and give it a unique name.
- But in fact, we can’t guarantee that a name we thought was unique is unique, which we might use when we add a dynamic to a TAB
actived
To represent theTo be activated
To be selected
After using this, other module dynamics will naturally want to use this nameactived
, if you can leave a like, that we are a kind of people hahaha 😵. - Some of you may say, “Oh, as long as it runs, it doesn’t conflict. The customer won’t look at your source code and see how you write it.” But imagine if the project is not your own development, when it comes to larger, more complex projects, how you organize the code is the key to efficiency! What if you take the pail and someone else takes it? People look at your code and say, “Look! What shit mountain this guy wrote!” Have you ever made fun of others like this 🙇.
About the BEM 🌊
What is the BEM
BEM
—Block Element Modifier
Is a way to help you create reusable components and code sharing in front-end development, provided byYandex
A front end proposed by the teamCSS
Naming methodology.- The so-called
BEM
To put it bluntly is to follow the patternB
E
M
Three ways to name it.
-
BEM:
- B –
Block
A separate module, a separate entity that is meaningful in and of itself such as:header
,menu
,container
- E –
Element
Element, part of a block but has no independent meaning of its own. For example:header title
,container input
- M –
Modifier
A modifier, a state or attribute of a block or element such as:small
,checked
- B –
BEM used
- Know the
BEM
After the basic concept of the next is to use, I also said above, to be clearBEM
It’s just connecting the three together to make oneclass
Name. how do you connect them? BEM
One proposed concept is the use of a concatenation symbol, which does not specify what concatenation must be used, but does specify the use of different concatenations as an intra-team convention to distinguish BEM 3 types of elements.B
You can actually think of it as a module, like thisElement
theel-button
,Ant Design
theant-btn
.E
Officially, the concatenation is the block name followed by two underscores and the element name (__
) likeElement
theel-radio__input
.M
The official way to connect is.block__elem--mod
, likeElement
theel-button--small
,vant
thevan-button--danger
.
The advantage of the BEM
- Many of the best open source component libraries are named after the BEM specification, so why are they so popular?
- You know, we use a technology or a framework or a specification for one purpose,Make yourself more comfortable, just like an editor with plugins, not native
js
Develop with a framework. Convenient to see
BEM
You can let the developer know where the style belongs from the name, you can know the meaning of a tag from the name, suppose a style nameleft-content
It was hard enough just to find out where the pattern was.Structure and clear
In ourscss
andless
The preprocessor was used to nesting multiple layers, but here we areBEM
The named module hierarchy is simple and clear, andcss
There is no need to choose too many levels in writing.reusability
After you’ve styled an element, you need to concatenate its modifiersscss
Can be used in@mixin
Do some processing splicingE
andM
Put in publicscss
And then reuse it somewhere else, likeelement
theMixins configuration.
The sample
Said so much light to read the text is tired of it, or put a simple code example out of the product, the following is a button component encapsulation.
<template>
<button
class="zl-button"
:class="[
type ? 'zl-button--' + type : '',
size ? 'zl-button--' + size : ''
]"
type="button"
>
<slot />
</button>
</template>
<script>
export default {
name: 'ZlButton',
props: {
type: {
validator (value) {
return oneOf(value, ['default', 'primary', 'info', 'success', 'warning', 'danger']);
},
type: String,
default: 'default'
},
size:{
validator (value) {
return oneOf(value, ['','medium', 'small', 'mini']);
},
type: String,
default: ''
}
}
};
</script>
Copy the code
zl-button
You can see it’s used hereB
The connection.zl-button--small
You can see it’s used hereB
+M
In the middle with--
Join to represent block and modifier join.
Put it at the end 👋
- It’s not that hard to define a style, it’s just that you abandon it for the sake of speed, and if time permits, you have to use the mainstream norms to keep your code from becoming a nuisance.
- If you think this article is helpful to your words might as well 🍉 attention + thumbs up + favorites + comments + forwarding 🍉 support yo ~~😛
Reference 👈
What do you think of BEM naming in CSS?
GET BEM
Past highlights 🌅
How to write an elegant component example using Vuepress (above) 👈
How to write an elegant component example using Vuepress (2) 👈
Hand in hand 🧑🤝🧑 Don’t worry about learning Gulp
For convenience, I changed someone else’s wheel 😅