Table of Contents
1 命名
1.1 文件命名
1.2 选择器命名
2 代码风格
2.1 缩进
2.2 空格
2.3 换行
2.4 行长度
3 值与单位
3.1 文本
3.2 数值
3.3 单位
3.4 url()
3.5 颜色
4 通用
4.1 选择器
4.2 属性缩写
4.3 属性书写顺序
4.4 变换与动画
4.5 属性前缀
Home Web Front-end HTML Tutorial css编码规范_html/css_WEB-ITnose

css编码规范_html/css_WEB-ITnose

Jun 24, 2016 am 11:33 AM

* 约定: 以下事例代码中所用单位均为 rem ,关于 rem 单位的使用可参照 《移动端web app自适应布局探索与总结》

1 命名

1.1 文件命名

常用的文件命名:

  • 全局:global.css

  • 结构:layout.css

  • 模块:module.css

  • 主题: teemes.css

较长文件名必须以 - 中横杠符连接,项目里面的私有样式文件:项目名-业务模块名称.css。

例:

/* 项目名为clout *//* good */clout-home.css/* bad */cloutHome.css;
Copy after login

1.2 选择器命名

(重要)在不是必须的情况下尽可能不用id选择器。

  • 选择器名字全小写,不得使用大写。

  • 较长选择器名字之间使用-中横杆连接。

  • 当判断容易出现命名冲突的时候,命名需按规则:模块名-你的选择器名,如果出现多层级选择器的情况(应尽量避免超过3级的情况),每个层级间使用-中横杆连接,不建议直接使用嵌套。

例:

/* 选择器名称 *//* good */.mydialog {    font-size: .28rem;} /* bad */.myDialog {    font-size: .28rem;}/* 模块及嵌套 *//* good */<div class="mydialog">    <div class="mydialog-hd">        <p class="mydialog-hd-title">标题</p>    </div></div>.mydialog-hd-title {    color: #3f3f3f;}/* bad */<div class="mydialog">    <div class="hd">        <p class="title">标题</p>    </div></div>.mydialog .hd .title {    color: #3f3f3f;}
Copy after login

(建议)常用的选择器命名:

  • 头部:header(hd)

  • 标题:title

  • 内容:container/content(cont)

  • 页面主题:body(bd)/main

  • 尾部:footer(ft)

  • 导航:nav

  • 子导航:subnav

  • 标签页:tab

  • 侧栏:sidebar

  • 栏目:column/col

  • 外围控制布局:wrapper

  • 左中右:left center right

  • 菜单:menu

  • 子菜单:submenu

  • 列表:list

  • 当前的:active

  • 图标:icon

  • 提示信息:msg

  • 小技巧:tips

2 代码风格

2.1 缩进

(重要)统一使用 4 个空格缩进,不得使用 tab 和 2 个空格(没规范前的缩进方式不管)。

  • sublime -> tab键转空格

  • eclipse、sts -> tab键转空格

2.2 空格

(重要)选择器跟 { 之间必须包含空格。

例:

/* good */.selector {}/* bad */ .selector{}
Copy after login

(重要)属性跟 : 之间不能有空格,: 跟属性值之间必须包含空格。

例:

/* good */.selector {    color: #3f3f3f;}/* bad */.selector {    color:#3f3f3f; /* 或 color : #3f3f3f; */}
Copy after login

(重要) >、+、~选择器的两边各保留一个空格。

例:

/* good */.header > .title {    padding: .1rem;}label + input {    margin-left: .04rem;}input:checked ~ .input-face {    background-color: #ccc;}/* bad */.header>.title {    padding: .1rem;}......
Copy after login

2.3 换行

(重要)一个rule中有多个选择器时,选择器间必须换行。

例:

/* good */p,div,input,textarea {    font-size: .28rem;}/* bad */p, div, input, textarea {    font-size: .28rem;}
Copy after login

(重要)属性值之间必须换行。

例:

/* good */.content {    padding: .1rem;    color: #3f3f3f;}/* bad */.content {    padding: .1rem; color: #3f3f3f;}
Copy after login

(建议)对于超长的样式属性值,可在 空格 或 , 处换行。

例:

.selector {    bakcground:         url(veryveryveryveryveryLongUrlHere/image/icon.png)         no-repeat 0 0;}.selector {    background-image: -webkit-gradient(        linear,        left bottom,        left top,        color-stop(0.04, rgb(88,94,124)),        color-stop(0.52, rgb(115,123,162))    )}
Copy after login

2.4 行长度

(重要) 每行不得超过 120 个字符,除非单行不可分割(例如url超长)。

3 值与单位

3.1 文本

(重要)文本内容必须用双引号包围。

例:

/* good */body {    font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif;}/* bad */body {    font-family: 'Helvetica Neue', Helvetica, STHeiTi, sans-serif;}
Copy after login

3.2 数值

(重要)数值为 0 - 1 之间的小数,省略整数部分的 0 。

例:

/* good */body {    font-size: .28rem;}/* bad */ {    font-size: 0.28rem;}
Copy after login

3.3 单位

(重要)数值为 0 的属性值须省略单位。

例:

/* good */body {    padding: 0 .1rem;}/* bad */body {    padding: 0rem .1rem;}
Copy after login

3.4 url()

(重要) url() 函数中的路径不加引号

例:

/* good */body {    background: url(bg.png);}/* bad */body {    background: url("bg.png");}
Copy after login

(建议) url() 函数中的绝对路径可省去协议名

例:

/* good */body {    background: url(//yunzhijia.com/images/bg.png);}/* bad */body {    background: url(http://yunzhijia.com/images/bg.png);}
Copy after login

3.5 颜色

(重要)RGB颜色值必须使用十六进制形式 #3f3f3f。不允许使用 rgb()。

解释:

带有alpha(不透明度)的颜色信息可以使用 rgba()。使用 rgba() 时每个逗号后须保留一个空格。

例:

/* good */.border {    border: 1px solid #dce1e8;}.overlayer {   background-color: rgba(0, 0, 0, .7); }/* bad */.border {    border: 1px solid rgb(220, 225, 232);}.overlayer {    background-color: rgba(0,0,0,.7);}
Copy after login

(重要)颜色值可缩写时,须使用缩写形式。

例:

/* good */.text-grey {    color: #ccc;}/* bad */.text-grey {    color: #cccccc;}
Copy after login

(重要)颜色值不可使用颜色单词。

例:

/* good */.text-red {    color: #f00;}/* bad */.text-red {    color: red;}
Copy after login

(建议)颜色值中的英文字母使用小写,如果采用大写字母,则必须保证同一项目内是一致的。

例:

/* good */.border-color {    border-color: #dce1e8;}/* bad */.border-color {    border-color:  #DCE1E8;}
Copy after login

4 通用

4.1 选择器

(重要)DOM节点 id、class 属性赋值时 = 之间不得有空格,属性值必须用双引号包围,不得用单引号。

例:

/* good */<div class="container" id="container"></div>/* bad */<div class = "container" id='container'></div>
Copy after login

(重要)如无必要,尽量不使用 id 选择器,给 id、class 选择器设置属性时不需要添加类型选择器进行限定。

例:

/* good */#footer,.container {    background-color: #fff;}/* bad */div#footer,div.container {    background-color: #fff;}
Copy after login

(重要) id 选择器不需嵌套其他选择器。

例:

/* good */<div class="footer">    <span id="tips">提示语</span></div>#tips {    color: #bdbdbd;}/* bad */.footer #tips {    color: #bdbdbd;}
Copy after login

4.2 属性缩写

(建议)在可以使用缩写的情况下,尽量使用属性缩写。

例:

/* good */body {    font: .28rem/1.25 Helvetica;}/* bad */body {    font-family: Helvetica;    font-size: .28rem;    line-height: 1.25;}
Copy after login

(建议)使用 border、margin、padding 等缩写时,应注意确实需要设置多个方向的值时才使用缩写,避免其他方向的有用值被覆盖掉

例:

<div class="wrap list-wrap"></div>.wrap {    padding: .1rem;    border: 1px solid #dce1e8;}/* good */.list-wrap {    padding-left: .2rem;    padding-right: .2rem;    border-color: #ccc;}/* bad */.list-wrap {    padding: .2rem .1rem;    border: 1px solid #ccc;}
Copy after login

4.3 属性书写顺序

(建议)按如下顺序书写,摘自http://www.zhihu.com/question/19586885/answer/48933504

  • 位置属性(position, top, right, z-index,display, float, overflow 等)

  • 大小(width, height, padding, margin, border)  

  • 文字系列(font, line-height, letter-spacing,color- text-align等)

  • 视觉(background, color, list-style等)  

  • 其他(animation, transition等)

  • 例:

    .footer-fixed {    position: fixed;    bottom: 0;    left: 0;    overflow: hidden;        width: 100%;    height: .5rem;    padding: .1rem;    border: 1px solid #dce1e8;    box-sizing: border-box;        font-size: .28rem;    line-height: 1.25;        background: #e9ecf1;    color: #3f3f3f;        -webkit-transition: color .5s;       -moz-transition: color .5s;            transition: color .5s;}
    Copy after login

    4.4 变换与动画

    (重要) 使用 transition 时应指定 transiton-property,不用 all。

    例:

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

    4.5 属性前缀

    (建议)属性的私有前缀按长到短排列,按 : 对其

    例:

    /* good */.tab {    -webkit-transition: color .5s;       -moz-transition: color .5s;            transition: color .5s;}/* bad */.tab {    -webkit-transition: color .5s;    -moz-transition: color .5s;    transition: color .5s;}
    Copy after login
    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 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 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)

    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    See all articles