Home >Web Front-end >CSS Tutorial >CSS code naming convention
Use class selectors and abandon ID selectors
The uniqueness of ID in a page means that if you write CSS with ID as the selector, it cannot be reused.
NEC special characters: "-" hyphen
"-" does not represent the meaning of a hyphen in this specification.
She only represents two meanings: classification prefix separator and expansion separator. Please refer to the following specific rules for details.
Category naming method: Use a single letter + "-" as the prefix
Layout (grid) (.g-); module (module) (.m-); component (unit) (.u-); function ( function) (.f-); skin (.s-); status (.z-).
Note: The selectors in your styles should always start with the first five categories, and then use descendant selectors inside.
If these five categories cannot meet your needs, you can define one or more additional categories, but they must comply with the naming rule of a single letter + "-" as the prefix, that is, the .x- format.
Special: .j- will be used exclusively for JS to get nodes, please do not use .j- to define styles.
Descendant selector naming
It is agreed that class selectors that are not prefixed by a single letter + "-" and have a length greater than or equal to 2 are descendant selectors, such as: .item is each item in the m-list module, .text It is the text part in the m-list module: .m-list .item{}.m-list .text{}.
A semantic tag can also be a descendant selector, such as: .m-list li{}.
Single-letter class selectors are not allowed. The reason is detailed in "Extended classes for descendant selectors of modules and components" below.
By using the descendant selector method, you don’t need to consider whether its name has been used, because it only takes effect in the current module or component. The same style name can be reused in different modules or components, regardless of each other. Interference; the effect is particularly obvious when multiple people collaborate or collaborate in sub-modules!
The descendant selector does not need to fully represent the structure tree level, and should be as short as possible.
Note: Do not use descendant selectors in page layouts because the possibility of contamination is greater;
/* 这里的.itm和.cnt只在.m-list中有效 */ .m-list{margin:0;padding:0;} .m-list .itm{margin:1px;padding:1px;} .m-list .cnt{margin-left:100px;} /* 这里的.cnt和.num只在.m-page中有效 */ .m-page{height:20px;} .m-page .cnt{text-align:center;} .m-page .num{border:1px solid #ddd;}
Names should be concise but not semantic
/* 反对:表现化的或没有语义的命名 */ .m-abc .green2{} .g-left2{} /* 推荐:使用有语义的简短的命名 */ .m-list .wrap2{} .g-side2{}
Different class naming with the same semantics
Method: Direct Just add numbers or letters to distinguish them (for example: .m-list, .m-list2, .m-list3, etc., are all list modules, but they are completely different modules).
Other examples: .f-fw0, .f-fw1, .s-fc0, .s-fc1, .m-logo2, .m-logo3, u-btn, u-btn2, etc.
The naming method of extended classes of modules and components
When A, B, C,... are of the same type and have similar appearance with little difference, then the one with the highest occurrence rate among them will be used as the base class, and the others will be used. An extension of the base class.
Method: + “-” + numbers or letters (for example: the extended classes of .m-list are .m-list-1, .m-list-2, etc.).
Addition: The base class itself can be used independently (for example: class="m-list"), and the extended class must be used based on the base class (for example: class="m-list m-list-2").
If your extension class represents different states, then you can name it like this: u-btn-dis, u-btn-hov, m-box-sel, m-box-hov, etc., and then use it like this: class ="u-btn u-btn-dis".
If your website is not compatible with browsers such as IE6, then your method of identifying status can also adopt the independent status classification (.z-) method: .u-btn.z-dis, .m-box.z-sel , and then use it like this: class="u-btn z-dis".
Extended classes for descendant selectors of modules and components
Sometimes there are similar things in modules. If you don’t make them into components and extensions, you can also use descendant selectors and extensions.
Descendant selector: .m-login .btn{}.
Descendant selector extensions: .m-login .btn-1{}, .m-login .btn-dis{}.
You can also take the independent status classification (.z-) method: .m-login .btn.z-dis{}, and then use it like this: class="btn z-dis".
Note: This method is used for class selectors. If you use labels directly as selectors, you do not need to use this naming method.
Note: In order to prevent the naming convention of extended classes and large classes of descendant selectors from conflicting, descendant selectors are not allowed to use a single letter.
For example: .m-list .a{} is not allowed, because when .a needs to be extended, it will become .a-bb, which conflicts with the naming convention of large categories.
Group selectors can sometimes replace extension methods
Sometimes although two modules of the same type are very similar, you hope there will be no dependency between them, which means you don’t want to use extension methods, then you can pass Combine selectors to set common styles.
The premise for using this method is: the same type, function and appearance are similar, and it is written in the same code area for easy maintenance.
/* 两个元件共性的样式 */ .u-tip1,.u-tip2{} .u-tip1 .itm,.u-tip2 .itm{} /* 在分别是两个元件各自的样式 */ /* tip1 */ .u-tip1{} .u-tip1 .itm{} /* tip2 */ .u-tip2{} .u-tip2 .itm{}
Prevent contamination and contamination
When modules or components are nested in each other and use the same label selector or other descendant selectors, then the selector inside will be selected by the same selector outside Influence.
So, if your module or component may be nested or nested within other modules or components, use tag selectors with caution, use class selectors when necessary, and pay attention to the naming method, you can use .m-layer .layerxxx , .m-list2 .list2xxx form to reduce the pollution of descendant selectors.