Home > Article > Web Front-end > Study Guide to CSS Code Writing Standards
Here is a study guide for CSS code writing standards, including code comments, naming standards, space indentation, etc., which are all based on everyone’s usual conventions. It is definitely worth learning and reference. Friends who need it can refer to it. Next
1. Formatting code
1.1 File
[Recommendation]: CSS files use UTF-8 encoding without BOM
1.2Indentation
[Mandatory]: Use 4 spaces as an indentation level. 2 spaces or tab characters are not allowed.
.selector { margin: 0; padding: 0; }
1.3 Spaces
[Mandatory]: There must be a space between the selector and {.
.selector { }
[Mandatory]: There must be a space between the selector and {.
margin: 0;
[Mandatory]: When the list attribute book is on a single line, it must be followed by a space
font-family: Aria, sans-serif;
1.4 Line length
[Mandatory]: Each line must not exceed 120 characters, unless a single line is indivisible.
[Recommendation]: For very long styles, it is recommended to wrap the style values at spaces or after , and to group them logically.
/* 不同属性值按逻辑分组 */ background: transparent url(aVeryVeryVeryLongUrlIsPlacedHere) no-repeat 0 0; /* 可重复多次的属性,每次重复一行 */ background-image: url(aVeryVeryVeryLongUrlIsPlacedHere) url(anotherVeryVeryVeryLongUrlIsPlacedHere); /* 类似函数的属性值可以根据函数调用的缩进进行 */ background-image: -webkit-gradient( linear, left bottombottom, left top, color-stop(0.04, rgb(88,94,124)), color-stop(0.52, rgb(115,123,162)) );
##1.5 Selector
[Mandatory]: When a rule contains multiple selectors, each selector declaration must Own a line.
/* good */ .post, .page, .comment { line-height: 1.5; } /* bad */ .post, .page, .comment { line-height: 1.5; }
Example:
/* good */ main > nav { padding: 10px; } label + input { margin-left: 5px; } input:checked ~ button { background-color: #69C; } /* bad */ main>nav { padding: 10px; } label+input { margin-left: 5px; } input:checked~button { background-color: #69C; }
css /* good */ article[character="juliet"] { voice-family: "Vivien Leigh", victoria, female } /* bad */ article[character='juliet'] { voice-family: "Vivien Leigh", victoria, female }
2. Selector and attribute abbreviation2.1 Selector
[Mandatory] If not necessary, it shall not be The id and class selectors are qualified by adding type selectors. Explanation: It has a certain impact on performance and maintainability.
Example:
css
/* good */ #error, .danger-message { font-color: #c00; } /* bad */ dialog#error, p.danger-message { font-color: #c00; }[Recommendation] The nesting level of the selector should be no more than 3 levels, and the qualification conditions at the rear should be as precise as possible.
/* good */ #username input {} .comment .avatar {} /* bad */ .page .header .login #username input {} .comment p * {}
2.2 Attribute abbreviation
[Recommendation] Where abbreviations can be used In this case, try to use attribute abbreviations. Example:
/* good */ .post { font: 12px/1.5 arial, sans-serif; } /* bad */ .post { font-family: arial, sans-serif; font-size: 12px; line-height: 1.5; }
Explanation: Abbreviations such as border / margin / padding will set the values of multiple properties at the same time, and it is easy to override settings that do not need to be overridden. If some directions need to inherit values from other declarations, they should be set separately.
Example:
/* centering <article class="page"> horizontally and highlight featured ones */ article { margin: 5px; border: 1px solid #999; } /* good */ .page { margin-right: auto; margin-left: auto; } .featured { border-color: #69c; } /* bad */ .page { margin: 5px auto; /* introducing redundancy */ } .featured { border: 1px solid #69c; /* introducing redundancy */ }
2.3 Leave a blank line between each rule set
/* good */ .selector1 { display: block; width: 100px; } .selector2 { padding: 10px; margin: 10px auto; } /* bad */ .selector1 { display: block; width: 100px; } .selector2 { padding: 10px; margin: 10px auto; }
3. Value and unit3.1 Text
[Mandatory] The text content must be surrounded by double quotes. Explanation: Text type content may be in selectors, attribute values, etc.
Example:
/* good */ html[lang|="zh"] q:before { font-family: "Microsoft YaHei", sans-serif; content: "“"; } html[lang|="zh"] q:after { font-family: "Microsoft YaHei", sans-serif; content: "”"; } /* bad */ html[lang|=zh] q:before { font-family: 'Microsoft YaHei', sans-serif; content: '“'; } html[lang|=zh] q:after { font-family: "Microsoft YaHei", sans-serif; content: "”"; }
3.2 Value
Example:
/* good */ panel { opacity: .8 } /* bad */ panel { opacity: 0.8 }
3.3 url()
Example:
body { background: url(bg.png); }
3.4 Length
[Mandatory] When the length is 0, the unit must be omitted. (Only the length unit can be omitted)Example:
/* good */ body { padding: 0 5px; } /* bad */ body { padding: 0px 5px; }
3.5 Color
/* good */ .success { box-shadow: 0 0 2px rgba(0, 128, 0, .3); border-color: #008000; } /* bad */ .success { box-shadow: 0 0 2px rgba(0,128,0,.3); border-color: rgb(0, 128, 0); }[Mandatory] When the color value can be abbreviated, the abbreviated form must be used.
Example:
/* good */ .success { background-color: #aca; } /* bad */ .success { background-color: #aaccaa; }[Mandatory] Named color values are not allowed for color values.
Example:
/* good */ .success { color: #90ee90; } /* bad */ .success { color: lightgreen; }[Recommendation] Use lowercase letters for English characters in color values. If you don’t use lowercase letters, you need to ensure that they are consistent within the same project.
Example:
/* good */ .success { background-color: #aca; color: #90ee90; } /* good */ .success { background-color: #ACA; color: #90EE90; } /* bad */ .success { background-color: #ACA; color: #90ee90; }
3.6 2D Position
[Mandatory] Both horizontal and vertical positions must be given. Explanation:
The initial value of the 2D position is 0% 0%, but when there is only a value in one direction, the value in the other direction will be parsed as center. To avoid confusion in understanding, values in both directions should be given simultaneously. Definition of background-position attribute value
Example:
/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
4. Text layout4.1 Font family
[mandatory] in the font-family attribute The font family name should use the English Family Name of the font. If there are spaces, they must be placed in quotation marks. Explanation:
The so-called English Family Name is a metadata of the font file. Common names are as follows:
Operating system | Family Name | |
---|---|---|
Windows | SimSun | |
Windows | SimHei | |
Windows | Microsoft YaHei | |
Windows | Microsoft JhengHei | |
Mac/iOS | STHeiti | |
Mac/iOS | Hiragino Sans GB | |
Linux | WenQuanYi Zen Hei | |
Linux | WenQuanYi Micro Hei |
前缀 | 说明 | 示例 |
---|---|---|
g- | 全局通用样式命名 | g-mod |
m- | 模块命名方式 | m-detail |
ui- | 组件命名方式 | ui-selector |
j- | 所有用于纯交互的命名,不涉及任何样式规则。 | J-switch |
不允许出现以类似:.info, .current, .news 开头的选择器,比如:
.info{sRules;}
因为这样将给我们带来不可预知的管理麻烦以及沉重的历史包袱。你永远也不会知道哪些样式名已经被用掉了,如果你是一个新人,你可能会遭遇,你每定义个样式名,都有同名的样式已存在,然后你只能是换样式名或者覆盖规则。所以我们推荐这样写:
.m-xxx .info{sRules;}
所有的选择器必须是以 g-, m-, ui- 等有前缀的选择符开头的,意思就是说所有的规则都必须在某个相对的作用域下才生效,尽可能减少全局污染。 J- 这种级别的className完全交由JSer自定义,但是命名的规则也可以保持跟重构一致,比如说不能使用拼音之类的
8.3命名单词
不以表现来命名,而是根据内容来命名。比如:left, right, center, red, black这种以表现来定命名,不允许出现;
推荐使用功能和内容相关词汇的命名,如:
全选复制放进笔记套系:package
相册:photo-album
作品:works
攻略:raiders
普通用户:normal-user
达人:talent-user
摄影师:photographer
用户昵称:user-alias
头像:head
地区:area
关注数:follow
粉丝数:followers
互相注意:attention
标签:label
发表时间:publish-date,publish-time
标题:title
信息:info
内容:content
关于我:about
简介内容:intro-content
评论:review
服务:service
封面:cover
流行:popular
收藏:collect
查看:view
预约:reservation
促销:sale-promotion
9.兼容性
9.1 属性前缀
[强制] 带私有前缀的属性由长到短排列,按冒号位置对齐。
解释:
标准属性放在最后,按冒号对齐方便阅读,也便于在编辑器内进行多行编辑。
示例:
.box { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
9.2 Hack
[建议] 需要添加 hack 时应尽可能考虑是否可以采用其他方式解决。
解释:
如果能通过合理的 HTML 结构或使用其他的 CSS 定义达到理想的样式,则不应该使用 hack 手段解决问题。通常 hack 会导致维护成本的增加。
[建议] 尽量使用 选择器 hack 处理兼容性,而非 属性 hack。
解释:
尽量使用符合 CSS 语法的 selector hack,可以避免一些第三方库无法识别 hack 语法的问题。
示例:
/* IE 7 */ *:first-child + html #header { margin-top: 3px; padding: 5px; } /* IE 6 */ * html #header { margin-top: 5px; padding: 4px; }
[建议] 尽量使用简单的 属性 hack。
示例:
.box { _display: inline; /* fix double margin */ float: left; margin-left: 20px; } .container { overflow: hidden; *zoom: 1; /* triggering hasLayout */ }
The above is the detailed content of Study Guide to CSS Code Writing Standards. For more information, please follow other related articles on the PHP Chinese website!