Home Web Front-end CSS Tutorial CSS code naming convention

CSS code naming convention

Nov 21, 2016 pm 02:21 PM
css

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;}
Copy after login

Names should be concise but not semantic

/* 反对:表现化的或没有语义的命名 */
.m-abc .green2{}
.g-left2{}
/* 推荐:使用有语义的简短的命名 */
.m-list .wrap2{}
.g-side2{}
Copy after login

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{}
Copy after login

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.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

How to layout bootstrap How to layout bootstrap Apr 07, 2025 pm 02:24 PM

To use Bootstrap to layout a website, you need to use a grid system to divide the page into containers, rows, and columns. First add the container, then add the rows in it, add the columns within the row, and finally add the content in the column. Bootstrap's responsive layout function automatically adjusts the layout according to breakpoints (xs, sm, md, lg, xl). Different layouts under different screen sizes can be achieved by using responsive classes.

How to build a bootstrap framework How to build a bootstrap framework Apr 07, 2025 pm 12:57 PM

To create a Bootstrap framework, follow these steps: Install Bootstrap via CDN or install a local copy. Create an HTML document and link Bootstrap CSS to the <head> section. Add Bootstrap JavaScript file to the <body> section. Use the Bootstrap component and customize the stylesheet to suit your needs.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

Does the image centering support image zooming? Does the image centering support image zooming? Apr 07, 2025 am 07:42 AM

How to achieve image centering and scaling in Bootstrap: Use d-flex justify-content-center to center images horizontally. Use align-items-center and fixed parent element height vertically center the image. Use the width and height attributes to control the image size, or use max-width and max-height to limit the maximum size. Use the img-fluid class or responsive design mechanism, such as media queries, to achieve responsive scaling. Optimize image size, control scaling using the object-fit attribute, and follow best practices to ensure performance and maintainability.

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

How is the compatibility of Bootstrap image centering How is the compatibility of Bootstrap image centering Apr 07, 2025 am 07:51 AM

Bootstrap image centering faces compatibility issues. The solution is as follows: Use mx-auto to center the image horizontally for display: block. Vertical centering Use Flexbox or Grid layouts to ensure that the parent element is vertically centered to align the child elements. For IE browser compatibility, use tools such as Autoprefixer to automatically add browser prefixes. Optimize image size, format and loading order to improve page performance.

See all articles