Table of Contents
黄金定律
语法:
语言属性:
IE兼容模式:  
字符编码:
引入css和JavaScript文件:
实用为王: 
属性顺序:
HTML属性应当按照以下顺序依次排列,确保代码的易读性:
减少标签的数量
JavaScript生成的标签
CSS语法:
声明顺序:
不要使用@import  
与标签相比,@import指令要慢很多,不光增加了额外的请求次数,还会导致不可预料的问题.替代办法有一下几种:
媒体查询(media query)的位置
带前缀的属性: 
单行规则声明:
Less和Sass中的嵌套
注释:
class命名
选择器
代码组织:
Home Web Front-end HTML Tutorial 如何编写规范,灵活,稳定,高质量的HTML和css代码 - 兜兜里有糖哦

如何编写规范,灵活,稳定,高质量的HTML和css代码 - 兜兜里有糖哦

May 22, 2016 pm 05:19 PM
flexible Stablize

黄金定律

永远遵循同一套编码规范,不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的.

语法:

1.用两个空格来代替制表符(tab);2.嵌套元素应当缩进一次(两个空格);  3.对于属性的定义,确保全部使用双引号,绝不要使用单引号;  4.不要在自闭合元素的尾部添加斜线--HTML5规范(https://dev.w3.org/html5/spec-author-view/syntax.html#syntax-start-tag)中明确说明这是可选的; 5.不要省略可选的结束标签;6.为每个HTML页面的第一行添加标准模式的声明,这样能确保在每个浏览器中有一只的展现;

语言属性:

根据HTML5规范,建议为HTML根元素指定 lang 属性,从而为文本设置正确的语言.这将有助于语音合成工具确定其应该采用的发音,有助于翻译工具确定翻译时应遵守的规则等等.lang 属性列表:http://www.sitepoint.com/web-foundations/iso-2-letter-language-codes/

IE兼容模式:  

IE支持通过特定的标签来确定回执当前页面所应该采用的IE版本.除非有强烈要求,否则最好是设置为edge mode,从而统治IE采用其所支持的最新的模式.

字符编码:

通过声明字符编码,能够确保浏览器快速并容易的判断页面内容的渲染方式.这样做可以避免在HTML中使用字符实体标记,从而全部与文档编码一致.

引入css和JavaScript文件:

根据HTML5规范,再引入css和JavaScript文件时一般不需要指定type属性,因为text/css和text/javascript分别是他们的默认值.

实用为王: 

尽量遵循HTML标准和语义,但是不要以牺牲实用性为代价.任何时候都要尽量使用最少的标签并保持最小的复杂度.

属性顺序:

HTML属性应当按照以下顺序依次排列,确保代码的易读性:

1.class2.id,name    3.data-*    4.src,for,type,href    5.title,alt    6.Aria,role  class用于标示高度可复用的组件,因此应该排在首位.

减少标签的数量

编写HTML代码时,尽量避免多余的父元素.很多时候,这需要迭代和重构来实现.

JavaScript生成的标签

通过JavaScript生成的标签让内容变得不易查找、编辑,并且境地性能.能避免是尽量避免.

CSS语法:

1.用两个空格来代替制表符(tab);  2.为选择器分组时,将单独的选择器单独放在一行;  3.为了代码的易读性,在每个声明块的左花括号钱添加一个空格;  4.声明块的右花括号应该单独成行;  5.每条声明语句的 : 后面应该插入一个空格;  6.为了获得更准确的错误报告,每条声明都应该独占一行;  7.所有声明语句都应该以分号结尾,最后一条声明语句后面的分号是可选的,但是如果省略这个分号,代码可能更容易出;  8.对于以逗号分隔的属性值,每个逗号后面都应该插入一个空格;  9.对于属性值或颜色参数,省略小于1的小数前面的0(例如 .5代替0.5);  10.十六进制值应该全部小写,例如:#fff,尽量使用简写形式的十六进制值,例如用#fff代替#ffffff;  11.为选择其中的属性添加双引号,例如 input[type="text"];  12.避免为 0 值指定单位,例如用margin:0 代替margin:0px.

声明顺序:

 相关的属性声明应该归为一组,并且按照下面的顺序排列:   

1.positioning(position: absolute; top: 0; bottom: 0; right: 0; left: 0; z-index: 100;);   2.box model(display: block; float: left; width: 100px; height: 100px;);   3.typographic(font: normal 13px "Microsoft YaHei"; line-height: 1.5em; color: #333; text-align:center;);   4.visual(background: yellow; border: 1px solid #c00; border-radius: 3px; opacity: 1; );    由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位.盒模型排在第二位,因为他决定了组建的尺寸和位置.其他属性知识影响组建的内部(inside)或者是不影响前两组属性,因此排在后面.参考http://twitter.github.io/recess/

不要使用@import  

与标签相比,@import指令要慢很多,不光增加了额外的请求次数,还会导致不可预料的问题.替代办法有一下几种:

1.使用多个元素;     2.通过sass或less类似的css预处理器将多个css文件便以为一个文件;     3.通过rails、jekyll或者其他系统中提供过css文件合并功能.

媒体查询(media query)的位置

将媒体查询尽可能放在相关规则的附近.不要将他们打包放在一个单一样式文件中或者放在文档底部.

带前缀的属性: 

当使用特定厂商的带前缀的属性时,通过锁紧的方式,让每个属性的值在垂直方向对齐,这样方便多行编辑.例如:     .selector {     

        -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);         

            box-shadow: 0 1px 2px rgba(0,0,0,.15);     

}

单行规则声明:

  对于值包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行.对于带有多条声明的样式,还是应当将声明分为多行.这样做的关键因素是为了错误检测.例如css校验器之处在180行有语法错误,如果是单行单条声明,你就不会忽略这个错误,如果是单行多条声明的话,你就要仔细分析避免漏掉错误了.

Less和Sass中的嵌套

 避免非必要的嵌套.这是因为虽然你可以使用嵌套,但是并不意味着应该使用嵌套.只有在必须将样式限制在父元素内(也就是后代选择器),并且存在多个需要嵌套的元素师才能使用嵌套.

注释:

代码是由人编写并维护的.请确保你的代码能够自描述、注释良好并且易于他人理解.好的代码注释能够传递上下文关系和代码目的;
不要简单地重申组件或者class名称;对于较长的注释,务必书写完整的句子,对于一般性的注释,可以书写简介的短语.

class命名

class名称中只能出现谢小字符和破折号(不是下划线也不是驼峰命名法).破折号应当用于相关class的命名(类似命名空间,例如.btn和.btn-danger)避免过度任意的简写. .btn代表button,但是.s不能表达任何意思;  class名应该尽可能短,并且意义明确;  使用有意义的名称,使用有组织的或者目的明确的名称,不要使用表现形式的名称;  基于最近的class或者基本class作为新class的前缀;  使用.js-* class来标识行为(与样式相对),并且不要将这些class包含到css文件中;  在为sass和less变量命名时也可以参考上面列出的各项规范.

选择器

对于通用元素使用class,这样利于渲染性能的优化;  对于经常出现的组件,避免使用属性选择器(例如:[class^="···"]),浏览器的性能会受到这些因素的影响;  选择器尽可能短,并且尽量限制组成选择器的元素个数,建议不要超过3个;  只有在必要的时候才将class限制在最近的父元素内.

代码组织:

以组建为单位组织代码段;  指定一致的注释规范;  使用一致的空白符将代码分隔成块,这样利于扫描较大的文档;  如果使用了多个css文件,将其按照组件而不是页面的形式拆分,因为页面会被重组,而组件只会被移动. 文章来源:http://www.css88.com/doc/codeguide/

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

The most stable version of Win10 The most stable version of Win10 Dec 25, 2023 pm 07:58 PM

Many users will encounter freezes or blue screens when operating the computer. At this time, we need to find the most stable win10 version to operate. Overall, it is very easy to use and can make your daily use smoother. The most stable win10 version in history 1. Win10 genuine original system. Here users can use simple operations. The system has been optimized and has strong stability, security and compatibility. Users can follow the steps to achieve the perfect machine. 2. Russian master streamlined The version of win10 has been strictly streamlined and many unnecessary functions and services have been deleted. After streamlining, the system has lower CPU and memory usage and runs faster. 3. Win10 Lite Edition 1909 is installed on multiple computers with different hardware models.

What is the importance of @JsonIdentityInfo annotation using Jackson in Java? What is the importance of @JsonIdentityInfo annotation using Jackson in Java? Sep 23, 2023 am 09:37 AM

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Basic concepts and applications of PHP algorithms Basic concepts and applications of PHP algorithms Jul 07, 2023 pm 05:22 PM

Basic Concepts and Applications of PHP Algorithms With the rapid development of the Internet, PHP, as a simple, easy-to-learn and powerful programming language, has been widely used in Web development. As the basis of computer science, algorithms play a vital role in solving problems and optimizing programs. This article will introduce the basic concepts of PHP algorithms and provide some practical application code examples. 1. Basic concepts of algorithms Definition of algorithms An algorithm is a description of a finite sequence that solves a specific problem. It consists of a series of steps and rules that follow a specific sequence

Why do newly purchased computers now generally use the Windows 10 operating system? Why do newly purchased computers now generally use the Windows 10 operating system? Jan 09, 2024 pm 03:37 PM

As we all know, the systems installed on the new computers we buy now are all Windows 10 systems. Some friends don’t understand why. Regarding this problem, the editor feels that win10 is now the mainstream operating system on the market, and using win10 will be the general trend in the future. Microsoft will also stop updating the win7 system in 2020. This shows that win10 will be the operating system popular in society in the future. Why do all newly purchased computers have Windows 10? In my opinion, the key factor in this situation is that starting from Intel’s sixth-generation processors and even higher-level derivative models, products compatible with 100 series and newer motherboards All have built-in new model XHCI main control interface, so it does not

Laravel monitoring errors: improve application stability Laravel monitoring errors: improve application stability Mar 06, 2024 pm 04:48 PM

Monitoring errors in Laravel is an important part of improving application stability. During the development process, various errors will inevitably be encountered, and how to detect and resolve these errors in a timely manner is one of the keys to ensuring the normal operation of the application. Laravel provides a wealth of tools and functions to help developers monitor and handle errors. This article will introduce some of the important methods and attach specific code examples. 1. Use logging Logging is one of the important means of monitoring errors. Laravel has a powerful logging system built-in, developers

Why is Python so popular? Explore the advantages of Python in the field of programming Why is Python so popular? Explore the advantages of Python in the field of programming Mar 26, 2024 am 09:15 AM

Why is Python so popular? To explore the advantages of Python in the field of programming, specific code examples are required. As a high-level programming language, Python has been loved and respected by programmers since its inception. The reason is not only because of its simplicity, readability and powerful functions, but also because it has shown unparalleled advantages in various fields. This article will explore the advantages of Python in the field of programming and explain why Python is so popular through specific code examples. First, Python

Look at the comparison between C language and Python from different angles Look at the comparison between C language and Python from different angles Mar 18, 2024 am 10:57 AM

C language and Python are two common programming languages, each with its own characteristics and advantages. This article will compare these two languages ​​from different perspectives and analyze their applicability, advantages and disadvantages in different scenarios. 1. Syntax simplicity: C language is a low-level language with relatively cumbersome syntax, requiring manual memory management, variable declaration, etc. For example, write a simple HelloWorld program. The C language code is as follows: #includeintmain()

PHP 8.3 update: more secure and stable PHP 8.3 update: more secure and stable Nov 27, 2023 pm 12:09 PM

PHP is a widely used programming language that plays an important role in the development of many websites and applications. Recently, PHP launched an update to version 8.3, which brings many exciting new features and improvements, making PHP more secure and stable. First, PHP8.3 introduces a new feature called Fibers. Fibers is a lightweight coroutine implementation that enables execution to be paused and resumed during the processing of a single request. This mechanism improves performance and scalability because it eliminates

See all articles