Home > Web Front-end > CSS Tutorial > Study Guide to CSS Code Writing Standards

Study Guide to CSS Code Writing Standards

高洛峰
Release: 2017-03-09 18:06:43
Original
1434 people have browsed it

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


1.3 Spaces
[Mandatory]: There must be a space between the selector and {.

.selector {   
}
Copy after login


[Mandatory]: There must be a space between the selector and {.

margin: 0;
Copy after login


[Mandatory]: When the list attribute book is on a single line, it must be followed by a space

font-family: Aria, sans-serif;
Copy after login


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


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


[Mandatory]: >, +, ~ Leave one space on both sides of the selector.

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


[Mandatory] The value in the attribute selector must be surrounded by double quotes.


css   
/* good */  
article[character="juliet"] {   
    voice-family: "Vivien Leigh", victoria, female   
}   
/* bad */  
article[character='juliet'] {   
    voice-family: "Vivien Leigh", victoria, female   
}
Copy after login


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

[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.


Example:


/* good */  
#username input {}   
.comment .avatar {}   
/* bad */  
.page .header .login #username input {}   
.comment p * {}
Copy after login


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


[Recommendation] When using abbreviations such as border / margin / padding, you should pay attention to the impact of the implicit value on the actual value. It is indeed necessary Use abbreviations only when setting values ​​in multiple directions.

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


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

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: &#39;Microsoft YaHei&#39;, sans-serif;   
    content: &#39;“&#39;;   
}   
html[lang|=zh] q:after {   
    font-family: "Microsoft YaHei", sans-serif;   
    content: "”";   
}
Copy after login

3.2 Value

[Mandatory] When the value is a decimal between 0 - 1, the integer part is omitted of 0.

Example:

/* good */  
panel {   
    opacity: .8   
}   
/* bad */  
panel {   
    opacity: 0.8   
}
Copy after login

3.3 url()

[Mandatory] Unquote the path in the url() function.

Example:

body {   
    background: url(bg.png);   
}
Copy after login


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

3.5 Color

[Mandatory] RGB color values ​​must use sixteen Base notation form #rrggbb. rgb() is not allowed.


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

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

[Mandatory] Named color values ​​are not allowed for color values.

Example:

/* good */  
.success {   
    color: #90ee90;   
}   
/* bad */  
.success {   
    color: lightgreen;   
}
Copy after login

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


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% */  
}
Copy after login

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:

Font Operating systemFamily Name宋体(中易宋体)WindowsSimSun黑体(中易黑体)WindowsSimHeiMicrosoft亚黑WindowsMicrosoft YaHeiMicrosoft正黑WindowsMicrosoft JhengHei华文黑体Mac/iOSSTHeitiHolly HeitiMac/iOSHiragino Sans GB文 Quan Yi Zheng HeiLinuxWenQuanYi Zen Hei文 Quan Yi Micro HeiLinuxWenQuanYi Micro Hei


示例:

h1 {   
    font-family: "Microsoft YaHei";   
}
Copy after login

[强制] font-family 按「西文字体在前、中文字体在后」、「效果佳 (质量高/更能满足需求) 的字体在前、效果一般的字体在后」的顺序编写,最后必须指定一个通用字体族( serif / sans-serif )


示例:

/* Display according to platform */  
.article {   
    font-family: Arial, sans-serif;   
}   
/* Specific for most platforms */  
h1 {   
    font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", "WenQuanYi Micro Hei", "Microsoft YaHei", sans-serif;   
}
Copy after login


[强制] font-family 不区分大小写,但在同一个项目中,同样的 Family Name 大小写必须统一。
示例:

/* good */  
body {   
    font-family: Arial, sans-serif;   
}   
h1 {   
    font-family: Arial, "Microsoft YaHei", sans-serif;   
}   
/* bad */  
body {   
    font-family: arial, sans-serif;   
}   
h1 {   
    font-family: Arial, "Microsoft YaHei", sans-serif;   
}
Copy after login


4.2 字号
[强制] 需要在 Windows 平台显示的中文内容,其字号应不小于 12px。
解释:由于 Windows 的字体渲染机制,小于 12px 的文字显示效果极差、难以辨认。
4.3 字体风格
[建议] 需要在 Windows 平台显示的中文内容,不要使用除 normal 外的 font-style。其他平台也应慎用。
解释:
由于中文字体没有 italic 风格的实现,所有浏览器下都会 fallback 到 obilique 实现 (自动拟合为斜体),小字号下 (特别是 Windows 下会在小字号下使用点阵字体的情况下) 显示效果差,造成阅读困难。

5 变换与动画
[强制] 使用 transition 时应指定 transition-property。
示例:

/* good */  
.box {   
    transition: color 1s, border-color 1s;   
}   
/* bad */  
.box {   
    transition: all 1s;   
}
Copy after login


[建议] 尽可能在浏览器能高效实现的属性上添加过渡和动画。
解释:
见本文,在可能的情况下应选择这样四种变换:

transform: translate(npx, npx);   
transform: scale(n);   
transform: rotate(ndeg);   
opacity: 0..1;
Copy after login


典型的,可以使用 translate 来代替 left 作为动画属性。
示例:

/* good */  
.box {   
    transition: transform 1s;   
}   
.box:hover {   
    transform: translate(20px); /* move right for 20px */  
}   
/* bad */  
.box {   
    left: 0;   
    transition: left 1s;   
}   
.box:hover {   
    left: 20px; /* move right for 20px */  
}
Copy after login


6 响应式
[强制] Media Query 如果有多个逗号分隔的条件时,应将每个条件放在单独一行中。
示例:

@media   
(-webkit-min-device-pixel-ratio: 2), /* Webkit-based browsers */  
(min--moz-device-pixel-ratio: 2),    /* Older Firefox browsers (prior to Firefox 16) */  
(min-resolution: 2dppx),             /* The standard way */  
(min-resolution: 192dpi) {           /* dppx fallback */  
    /* Retina-specific stuff here */  
}
Copy after login


7.CSS注释
普通注释

/* 普通注释 */
Copy after login


区块注释

/**  
 * 模块:m-detail  
 * author: xxx  
 * edit:   2016.5.02  
 */
Copy after login


8.CSS命名规范
8.1命名组成
命名必须由单词,中划线组成。例如:.info,.news-list
不推荐使用拼音来作为样式名,尤其是缩写的拼音、拼音与英文的混合
所有命名都使用小写,使用中划线 “-” 作为连接字符,而不是下划线 “_“
8.2命名前缀

前缀说明示例
g-全局通用样式命名g-mod
m-模块命名方式m-detail
ui-组件命名方式ui-selector
j-所有用于纯交互的命名,不涉及任何样式规则。J-switch


不允许出现以类似:.info, .current, .news 开头的选择器,比如:

.info{sRules;}
Copy after login

因为这样将给我们带来不可预知的管理麻烦以及沉重的历史包袱。你永远也不会知道哪些样式名已经被用掉了,如果你是一个新人,你可能会遭遇,你每定义个样式名,都有同名的样式已存在,然后你只能是换样式名或者覆盖规则。所以我们推荐这样写:

.m-xxx .info{sRules;}
Copy after login

所有的选择器必须是以 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;   
}
Copy after login


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


[建议] 尽量使用简单的 属性 hack。
示例:

.box {   
    _display: inline; /* fix double margin */  
    float: left;   
    margin-left: 20px;   
}   
.container {   
    overflow: hidden;   
    *zoom: 1; /* triggering hasLayout */  
}
Copy after login


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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template