CSS Code, too . Other topics in this guide apply to . It needs code review , And automatic and manual quality checks and rules are needed - Just like any other application code . pitiful CSS It's hard to modify and extend , And it's hard to change or refactor applications .
send CSS Component code close to its style . read ： file .
Solve design inconsistencies .UI Design is sometimes inconsistent in its style , Such as spacing or color . Avoid transferring this content to the application's style code ; Inconsistent inconsistencies affect the product , But they also tend to lead to bad CSS. Some inconsistencies are trivial , Others need to discuss with designers .
Use semantic class names . Describe the purpose of the rule （ for example product）, Not the content of the rules （ for example redBackground）. However , With a few exceptions , Performance / Utility classes can be useful .
Put the relevant rules in the file . This makes it easier for maintenance personnel to find them , So it's more likely to take them into account when making changes .
One selector per line . More than one per line is bad for readability .
Avoid tight coupling of selectors to DOM structure . This will make the style and DOM Difficult to modify without destroying another . Note that many classes are specified （ Mirror image DOM Trees ） Or use > The selectors of sub combinations .
Put the statements in a deliberate order . Include random （ Or by time ） The rules of sequential statements are harder to read and faster to visualize psychologically . They are also prone to multiple conflicts （ That is, accidental coverage ） Statement . A well thought out ranking , The most important statement came first , Related statements are grouped together , Avoid these problems . Use linter Force sort . Established and popular sequencing （ It includes presupposition ） Include Concentric,RECESS and SMACSS.
Use standard colors , Spacing and other variables . This will avoid repetition , And help ensure consistency .
Features and Technology
Avoid fixing dimensions in inappropriate places . Fixed size removes elements from the browser's sizing function , Causes the layout not to fit the browser size , And you need to adjust it manually when you change the content . They usually don't have modern layout functions .
Avoid absolute positioning in inappropriate places . Absolute positioning by positioning elements at specific coordinates , Take the element out of the layout flow . These need to be manually adjusted to fit the surrounding components together , And once it's introduced into the layout, it tends to proliferate . They usually don't have modern layout functions .
Avoid using floats to locate elements . The purpose of floating point numbers is to allow content to flow around elements . However , They have been used historically in ways that were not designed for them , To realize at that time CSS A layout that cannot be implemented in . Modern layout functions such as flexbox It's a better choice , So it's rarely necessary or inappropriate to use floats now .
Avoid using text-align Align elements . This is an inherited property , So the children of the elements that use it to align must set themselves to offset unwanted text alignment text-align. Use flexbox To align elements .
Avoid using !important. You should avoid adding this to the declaration to force you to win the selector specific race . It breaks through the normal special rules （ At the declaration level ）, And cannot be overridden without using another rule !important- Tends to lead to the spread of its use .
Use... As appropriate margin or padding .Margin Margins create space around elements ; Use it to separate elements from other elements . Adjacent vertical folds .padding Fill in the element （ Between the border and the content ） Create space ; Use it to separate the content and edges of elements . Think about what you need to achieve , If you find that you need to repeat yourself to keep the required spacing consistent , Think again .
Avoid using outdated technology .CSS Advances in functionality continue to update many traditional technologies , A better alternative , And they are widely supported in browsers . Use them . With flexbox Appearance , No more hacking and solutions .
Pay attention to the function support in different browsers . Or just use all the browsers supported by the application , Or perform incremental enhancement . see also MDN Document page Upper “ I can use Reference resources ” And browser compatibility table . Use linter Automatically perform compatibility checks .
Namespace selectors avoid inadvertently affecting unwanted elements . Class names and selectors are in global space （ page ） Defined and operated in , Therefore, policies must be adopted to consistently and easily avoid rules that affect unwanted elements . Examples include using the root class of a component as a selector for all its style rules ,BEM and CSS The prefix of the module . Even with this strategy , Special care should also be taken to prevent the parent component from inadvertently affecting other components nested in it .
avoid “reaching ” Nest components to add / Overlay its style . These styles are related to the internal implementation of nested components （DOM And your own style ） Related to , If there is a change, it is unlikely to be considered - Make them vulnerable and difficult to maintain . This is similar to using... In object-oriented programming / Modify private state . contrary , Let nested components support “ Options / Pattern class ”, The parent component can be applied to choose to join .
Make components immune to any information about where they are / How to use the impact of . Styles that affect the exterior of component elements （ For example, positioning / Layout / spacing ） Leave it to the parent code of the component to specify .
About writing scalable CSS For more information , I suggest Jarno Rantanen Use “8 There are two simple rules to make it powerful , Extensible CSS Architecture ” guide .
Create a document to explain the browser error resolution . Doing so highlights and explains unusual or confusing styles , Bring benefits to your future self and others . For well-known problems , You can use short phrases and point to, for example flexbugs The link is done concisely .
The explanation is not obvious “ Magic value ” The choice of , For example, width and spacing . Keeping track of this can make future changes with more confidence , And avoid regression . Choosing a width suitable for a particular longest-term expectation is an example .
Usually avoid using id attribute . Use the same... Multiple times on a page id Invalid value . In an application composed of multiple components , Some components have multiple instances , It's easy to break that requirement . however , This property is useful for scrolling links to specific locations on the page .
Use semantic markers . except <div> and <span> Besides, there are many other elements , They convey meaning . Use them in the right place , for example ： title , part , Tables and paragraphs .
Put complex code / The expression remains in HTML Outside the template . Templates usually rely on these things for conditional rendering and data binding . Combine complex templates with HTML Template mixing makes it harder to follow itself and tags . contrary , Extract variables and / Or method .