Table of Contents
Encoding settings" >Encoding settings
Namespace specification" >Namespace specification
Namespace thinking" >Namespace thinking
It is not recommended to use underscore_ to connect" >It is not recommended to use underscore_ to connect
Selector" >Selector
规则声明块" >规则声明块
数值与单位" >数值与单位
样式属性顺序" >样式属性顺序
合理使用使用引号" >合理使用使用引号
font-family 内使用引号" >font-family 内使用引号
background-image 的 url 内使用引号" >background-image 的 url 内使用引号
避免使用 !important" >避免使用 !important
代码注释" >代码注释
单行注释" >单行注释
多行注释" >多行注释
规则声明块内注释" >规则声明块内注释
文件注释" >文件注释
SASS 使用建议" >SASS 使用建议
嵌套层级规定" >嵌套层级规定
组件/公用类的使用方法" >组件/公用类的使用方法
组件类的思考" >组件类的思考
尽量避免使用标签名" >尽量避免使用标签名
Home Web Front-end HTML Tutorial CSS usage writing specifications

CSS usage writing specifications

Jul 20, 2017 pm 03:59 PM
css specification

Maybe different teams have their own specifications, or many people still write whatever they think of when writing CSS, without too many constraints.

I think CSS code standards are still necessary, especially in team cooperation and multi-person collaboration, standards are particularly important.

What is listed in this article is a relatively good set of CSS writing specifications derived from practice. I do not hope that everyone will adopt them completely, but you can develop one based on your own team needs. Set CSS code specifications that suit you.

I also hope to have more suggestions and improve together. This specification can also be seen on my Github. You are welcome to leave a message or submit a PR.

Encoding settings

Using UTF-8 encoding, Use in the CSS code header:

@charset "utf-8";
Copy after login

Note that it must be defined in front of all characters in the CSS file (including encoding comments), @charset will take effect.

For example, the following examples will make @charset invalid:

/* 字符编码 */
@charset "utf-8";
html,
body {
  height: 100%;
}

@charset "utf-8";
Copy after login

Namespace specification

  • Layout: use g as the namespace, for example: .g-wrap, .g-header, .g-content.

  • State: With s as the namespace, it represents a dynamic and interactive state, such as: .s-current, s-selected.

  • Tools: With u as the namespace, it represents reusable tools that are not coupled to business logic, such as: u-clearfix, u-ellipsis.

  • Component: m is the namespace, representing reusable and portable component modules, such as: m-slider, m-dropMenu.

  • Hook: With j as the namespace, it represents the class name specific to JavaScript calls, such as: j-request, j-open.

Namespace thinking

No choice BEM This kind of naming is too strict and style The rule about names that are too long and too ugly adopts a more compromise solution.

  • Save operations and less time typing Pressing a shift key

  • can distinguish JavaScript variable naming

Selector

When a rule contains multiple selectors, each selector occupies its own line. .

, +, ~, > Leave one space on each side of the selector.

.g-header > .g-header-des,
.g-content ~ .g-footer {
    
}
Copy after login

规则声明块

  • 当规则声明块中有多个样式声明时,每条样式独占一行。

  • 在规则声明块的左大括号 { 前加一个空格。

  • 在样式属性的冒号 : 后面加上一个空格,前面不加空格。

  • 在每条样式后面都以分号 ; 结尾。

  • 规则声明块的右大括号 } 独占一行。

  • 每个规则声明间用空行分隔。

  • 所有最外层引号使用单引号 ' 。

  • 当一个属性有多个属性值时,以逗号 , 分隔属性值,每个逗号后添加一个空格,当单个属性值过长时,每个属性值独占一行。

完整示例如下:

.g-footer,
.g-header {
  position: relative;
}

.g-content {
  background:
    linear-gradient(135deg, deeppink 25%, transparent 25%) -50px 0,
    linear-gradient(225deg, deeppink 25%, transparent 25%) -50px 0,
    linear-gradient(315deg, deeppink 25%, transparent 25%),
    linear-gradient(45deg, deeppink 25%, transparent 25%);
  }

.g-content::before {
  content: '';
}
Copy after login

数值与单位

  • 当属性值或颜色参数为 0 - 1 之间的数时,省略小数点前的 0 。

    color: rgba(255, 255, 255, 0.5)

    color: rgba(255, 255, 255, .5);

  • 当长度值为 0 时省略单位。

    margin: 0px auto

    margin: 0 auto

  • 十六进制的颜色属性值使用小写和尽量简写。

    color: #ffcc00

    color: #fc0

样式属性顺序

单个样式规则下的属性在书写时,应按功能进行分组,并以 Positioning Model > Box Model > Typographic > Visual 的顺序书写,提高代码的可读性。

  • 如果包含 content 属性,应放在最前面;

  • Positioning Model 布局方式、位置,相关属性包括:position / top / right / bottom / left / z-index / display / float / ...

  • Box Model 盒模型,相关属性包括:width / height / padding / margin / border / overflow / ...

  • Typographic 文本排版,相关属性包括:font / line-height / text-align / word-wrap / ...

  • Visual 视觉外观,相关属性包括:color / background / list-style / transform / animation / transition / ...

Positioning 处在第一位,因为他可以使一个元素脱离正常文本流,并且覆盖盒模型相关的样式。盒模型紧跟其后,因为他决定了一个组件的大小和位置。其他属性只在组件内部起作用或者不会对前面两种情况的结果产生影响,所以他们排在后面。

合理使用使用引号

在某些样式中,会出现一些含有空格的关键字或者中文关键字。

font-family 内使用引号

当字体名字中间有空格,中文名字体及 Unicode 字符编码表示的中文字体,为了保证兼容性,都建议在字体两端添加单引号或者双引号:

body {
  font-family: 'Microsoft YaHei', '黑体-简', '\5b8b\4f53';
}
Copy after login

background-image 的 url 内使用引号

如果路径里面有空格,旧版 IE 是无法识别的,会导致路径失效,建议不管是否存在空格,都添加上单引号或者双引号:

div {
  background-image: url('...');
}
Copy after login

避免使用 !important

除去某些极特殊的情况,尽量不要不要使用 !important

!important 的存在会给后期维护以及多人协作带来噩梦般的影响。

当存在样式覆盖层叠时,如果你发现新定义的一个样式无法覆盖一个旧的样式,只有加上 !important 才能生效时,是因为你新定义的选择器的优先级不够旧样式选择器的优先级高。所以,合理的书写新样式选择器,是完全可以规避一些看似需要使用 !important 的情况的。

代码注释

单行注释

星号与内容之间必须保留一个空格。

/* 表格隔行变色 */
Copy after login

多行注释

星号要一列对齐,星号与内容之间必须保留一个空格。

/**
 * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough.
 */
Copy after login

规则声明块内注释

使用 // 注释,// 后面加上一个空格,注释独立一行。

.g-footer {
    border: 0;
    // ....
}
Copy after login

文件注释

文件顶部必须包含文件注释,用 @name 标识文件说明。星号要一列对齐,星号与内容之间必须保留一个空格,标识符冒号与内容之间必须保留一个空格。

/**
 * @name: 文件名或模块名
 * @description: 文件或模块描述
 * @author: author-name(mail-name@domain.com)
 *          author-name2(mail-name2@domain.com)
 * @update: 2015-04-29 00:02
 */
Copy after login
  • @description为文件或模块描述。

  • @update为可选项,建议每次改动都更新一下。

当该业务项目主要由固定的一个或多个人负责时,需要添加@author标识,一方面是尊重劳动成果,另一方面方便在需要时快速定位责任人。

SASS 使用建议

嵌套层级规定

使用 SASSLESS 等预处理器时,建议嵌套层级不超过 3 层。

组件/公用类的使用方法

组件/公用类使用 %placeholders 定义,使用 @extend 引用。如:

%clearfix {
  overflow: auto;
  zoom: 1;
}

.g-header {
  @extend %clearfix;
}
Copy after login

组件类的思考

使用 SASS ,经常会预先定义好一些常用公用组件类,譬如清除浮动,水平垂直居中,文字 ellipsis。又或者多个元素具有同样的样式,我们希望能够少写这部分代码,公共部分抽离出来只写一次,达到复用。

但是复用的方式在 SASS 中有多种,那么是使用单独使用一个类定义,给需要的标签添加,还是使用 @include 或者 @extend在定义的类中引入一个 @mixin,或者一个 @function 呢?

基于让 CSS 更简洁以及代码的复用考虑,采用上面的使用 %placeholders 定义,使用 @extend 引用的方案。

  • %placeholders,只是一个占位符,只要不通过 @extend 调用,编译后不会产生任何代码量

  • 使用 @extend 引用,则是因为每次调用相同的 %placeholders 时,编译出来相同的 CSS 样式会进行合并(反之,如果使用 @include 调用定义好的 @mixin,编译出来相同的 CSS 样式不会进行合并)

  • 这里的组件类特指那些不会动态改变的 CSS 样式,注意与那些可以通过传参生成不同数值样式的 @mixin 方法进行区分

尽量避免使用标签名

使用 SASS ,或者说在 CSS 里也有这种困惑。

假设我们有如下 html 结构:

<div class="g-content">
    <ul class="g-content-list">
        <li class="item"></li>
        <li class="item"></li>
        <li class="item"></li>
        <li class="item"></li>
    </ul>
</div>
Copy after login

在给最里层的标签命名书写样式的时候,我们有两种选择:

.g-content {
  .g-content-list {
    li {
      ...
    }
  }
}
Copy after login

或者是

.g-content {
  .g-content-list {
    .item {
      ...
    }
  }
}
Copy after login

也就是,编译之后生成了下面这两个,到底使用哪个好呢?

  • .g-content .g-content-list li { }

  • .g-content .g-content-list .item { }

基于 CSS 选择器的解析规则(从右向左),建议使用上述第二种 .g-content .g-content-list .item { } ,避免使用通用标签名作为选择器的一环可以提高 CSS 匹配性能。

浏览器的排版引擎解析 CSS 是基于从右向左(right-to-left)的规则,这么做是为了使样式规则能够更快地与渲染树上的节点匹配。 

 

本规范也可以在我的 Github 上看到,欢迎留言或者提 PR。

到此本文结束,如果还有什么疑问或者建议,可以多多交流,原创文章,文笔有限,才疏学浅,文中若有不正之处,万望告知。

The above is the detailed content of CSS usage writing specifications. For more information, please follow other related articles on the PHP Chinese website!

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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 resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

See all articles