HTML&CSS编码规范

黄金定律

不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的。

HTML规范

语法
  • 标签不要大写,即便是 doctype 标签。
  • 用两个空格来代替制表符(tab) – 这是唯一能保证在所有环境下获得一致展现的方法。
  • 嵌套元素应当缩进一次(即两个空格)。
  • 对于属性的定义,永远全部使用双引号,绝不要使用单引号。
  • 不要在自闭合(self-closing)元素的尾部添加斜线 — HTML5 规范 中明确说明斜线是可忽略的。
  • 不要省略可选的结束标签(closing tag)(例如,</li></body>)。
<!doctype html>
<html>
  <head>
    <title>Page title</title>
  </head>
  <body>
    <img src="images/company-logo.png" alt="Company">
    <h1 class="hello-world">Hello, world!</h1>
  </body>
</html>
H5 Doctype

为每个 HTML 页面的第一行添加 standards mode(标准模式) 声明,这样能够确保在每个浏览器中拥有一致的展现。

<!doctype html>
<html>
  <head>
  </head>
</html>
语言属性

根据 HTML5 规范:

强烈建议为 html 根元素指定 lang 属性,从而为文档设置正确的语言。这将有助于语音合成工具确定其所应该采用的发音,有助于翻译工具确定其翻译时所应遵守的规则等等。

更多关于 lang 属性的知识可以从 此规范 中了解。Sitepoint 站点上 给出了一份语言代码表

<html lang="en">
  <!-- ... -->
</html>
IE兼容模式&字符编码
  • IE 支持通过特定的 <meta> 标签来确定绘制当前页面所应该采用的 IE 版本。除非有强烈的特殊需求,否则最好是设置为 edge mode,从而通知 IE 采用其所支持的最新的绘制模式。了解更多信息请 阅读这篇 Stack Overflow 上的文章
  • 通过明确声明字符编码,能够确保浏览器快速并容易的判断页面内容的渲染方式。这样做的好处是,可以避免在 HTML 中使用字符实体标记(character entity),从而全部与文档编码一致(一般采用 UTF-8 编码)。
<!doctype html>
<html>
  <head>
      <!--字符编码-->
      <meta charset="UTF-8">
      <!--IE兼容模式-->
      <meta http-equiv="x-ua-compatible" content="ie=edge">
  </head>
</html>

引入 CSS 和 JavaScript 文件

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

<!-- External CSS -->
<link rel="stylesheet" href="code-guide.css">

<!-- In-document CSS -->
<style>
  /* ... */
</style>

<!-- JavaScript -->
<script src="code-guide.js"></script>
属性声明顺序

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

  • class
  • id, name
  • data-*
  • src, for, type, href, value
  • title, alt
  • role, aria-*

class 用于标识高度可复用组件,因此应该排在首位。id 用于标识具体组件,应当谨慎使用(例如,页面内的书签),因此排在第二位。

<a class="..." id="..." data-toggle="modal" href="#">
  Example link
</a>

<input class="form-control" type="text">

<img src="..." alt="...">
布尔(boolean)型属性

布尔型属性可以在声明时不赋值。XHTML 规范要求为其赋值,但是 HTML5 规范不需要。

更多信息请参考 WhatWG section on boolean attributes

元素的布尔型属性如果有值,就是 true,如果没有值,就是 false。

如果一定要为其赋值的话,请参考 WhatWG 规范:

如果属性存在,其值必须是空字符串或 […] 属性的规范名称,并且不要在首尾添加空白符。

简单来说,就是不用赋值。

<input type="text" disabled>

<input type="checkbox" value="1" checked>

<select>
  <option value="1" selected>1</option>
</select>
减少标签数量

编写 HTML 代码时,尽量避免多余的父元素。很多时候,这需要迭代和重构来实现。请看下面的案例:

<!-- 不合理的设计 -->
<span class="avatar">
  <img src="...">
</span>

<!-- 优化后 -->
<img class="avatar" src="...">

####实用为王

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

CSS规范

语法
  • 用两个空格来代替制表符(tab) – 这是唯一能保证在所有环境下获得一致展现的方法。
  • 为选择器分组时,将单独的选择器单独放在一行。
  • 为了代码的易读性,在每个声明块的左花括号前添加一个空格。
  • 声明块的右花括号应当单独成行。
  • 每条声明语句的 : 后应该插入一个空格。
  • 为了获得更准确的错误报告,每条声明都应该独占一行。
  • 所有声明语句都应当以分号结尾。最后一条声明语句后面的分号是可选的,但是,如果省略这个分号,你的代码可能更易出错。
  • 对于以逗号分隔的属性值,每个逗号后面都应该插入一个空格(例如,box-shadow)。
  • 不要在 rgb()rgba()hsl()hsla()rect() 值的内部的逗号后面插入空格。这样利于从多个属性值(既加逗号也加空格)中区分多个颜色值(只加逗号,不加空格)。
  • 对于属性值或颜色参数,省略小于 1 的小数前面的 0 (例如,.5 代替 0.5-.5px 代替 -0.5px)。
  • 十六进制值应该全部小写,例如,#fff。在扫描文档时,小写字符易于分辨,因为他们的形式更易于区分。
  • 尽量使用简写形式的十六进制值,例如,用 #fff 代替 #ffffff
  • 为选择器中的属性添加双引号,例如,input[type="text"]只有在某些情况下是可选的,但是,为了代码的一致性,建议都加上双引号。
  • 避免为 0 值指定单位,例如,用 margin: 0; 代替 margin: 0px;
/* 不好的 CSS */
.selector, .selector-secondary, .selector[type=text] {
  padding:15px;
  margin:0px 0px 15px;
  background-color:rgba(0, 0, 0, 0.5);
  box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF
}

/* 好的 CSS */
.selector,
.selector-secondary,
.selector[type="text"] {
  padding: 15px;
  margin-bottom: 15px;
  background-color: rgba(0,0,0,.5);
  box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;
}
声明顺序

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

  1. 定位
  2. 盒模型
  3. 文字和排版
  4. 可视化效果
  5. 其他

由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。

其他属性只是影响组件的 内部 或者是不影响前两组属性,因此排在后面。

参考如下配置:

.declaration-order {
  /* 定位 */
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 100;

  /* 盒模型 */
  display: block;
  float: right;
  width: 100px;
  height: 100px;

  /* 文字和排版 */
  font: normal 13px "Helvetica Neue", sans-serif;
  line-height: 1.5;
  color: #333;
  text-align: center;

  /* 可视化 */
  background-color: #f5f5f5;
  border: 1px solid #e5e5e5;
  border-radius: 3px;

  /* 其他 */
  opacity: 1;
}
单行规则声明

对于只包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行。对于带有多条声明的样式,还是应当将声明分为多行。

这样做的关键因素是为了错误检测 – 例如,CSS 校验器指出在 183 行有语法错误。如果是单行单条声明,你就不会忽略这个错误;如果是单行多条声明的话,你就要仔细分析避免漏掉错误了。

/* 单个属性定义在一行*/
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }

/* 多个声明,每个声明独占一行 */
.sprite {
  display: inline-block;
  width: 16px;
  height: 15px;
  background-image: url("../img/sprite.png");
}
.icon           { background-position: 0 0; }
.icon-home      { background-position: 0 -20px; }
.icon-account   { background-position: 0 -40px; }
简写形式的属性声明

在需要显示地设置所有值的情况下,应当尽量限制使用简写形式的属性声明。常被滥用的简写属性如下:

  • padding
  • margin
  • font
  • background
  • border
  • border-radius

大部分情况下,我们不需要为简写形式的属性声明指定所有值。例如,HTML 的标题元素只需要设置上、下边距(margin)的值,因此,在必要的时候,只需覆盖这两个值就可以了。0 值表示对浏览器默认值或以前指定的值的覆盖。

过多地使用属性的简写形式会导致代码出现不必要的覆盖和意外的副作用。

/* 反例 */
.element {
  margin: 0 0 10px;
  background: red;
  background: url("image.jpg");
  border-radius: 3px 3px 0 0;
}

/* 正例 */
.element {
  margin-bottom: 10px;
  background-color: red;
  background-image: url("image.jpg");
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
}
注释

代码是由人编写并维护的。请确保你的代码能够自描述、注释良好并且易于他人理解。好的代码注释能够传达上下文关系和代码目的。不要简单地重申组件或 class 名称。

对于较长的注释,务必书写完整的句子;对于一般性注解,可以书写简洁的短语。

/* 反例 */
/* Modal header */
.modal-header {
  ...
}

/* 正例 */
/* Wrapping element for .modal-title and .modal-close */
.modal-header {
  ...
}
class 命名
  • class 名称中只能出现小写字符和破折号(dashe)(不是下划线,也不是驼峰命名法)。破折号应当用于相关 class 的命名(类似于命名空间)(例如,.btn.btn-danger)。
  • 避免过度任意的简写。.btn 代表 button,但是 .s 不能表达任何意思。
  • class 名称应当尽可能短,并且意义明确。
  • 使用有意义的名称。使用有组织的或目的明确的名称,不要使用表现形式(presentational)的名称。
  • 基于最近的父 class 或基本(base) class 作为新 class 的前缀。
  • 使用 .js-* class 来标识行为(与样式相对),并且不要将这些 class 包含到 CSS 文件中。
/* 反例 */
.t { ... }
.red { ... }
.header { ... }

/* 正例 */
.tweet { ... }
.important { ... }
.tweet-header { ... }
选择器
  • 对于通用元素使用 class ,这样利于渲染性能的优化。
  • 对于经常出现的组件,避免使用属性选择器(例如,[class^="..."])。浏览器的性能会受到这些因素的影响。
  • 选择器要尽可能短,并且尽量限制组成选择器的元素个数,建议不要超过 3 。
  • 只有在必要的时候才将 class 限制在最近的父元素内(也就是后代选择器)(例如,不使用带前缀的 class 时 – 前缀类似于命名空间)。
/* 反例 */
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }

/* 正例 */
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }

DIV+CSS布局实例

易购商城后台管理系统,商品模块主页:

HTML网页结构设计(main.html)
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" href="css/reset.css">
        <link rel="stylesheet" href="css/main.css">
        <!-- 引入字体图片插件 -->
        <link rel="stylesheet" href="asserts/font-awesome-4.7.0/css/font-awesome.min.css">
    </head>
    <body>
        <div class="header">
            <div class="logo">
                <img src="img/logo.png" alt="易购商城后台管理">
            </div>
            <div class="title"><h1>易购商城后台管理系统</h1></div>
            <div class="user-info">
                <div class="user-wrap">					
                    <span id="account">管理员:softeem</span>
                    &nbsp;
                    <a class="exit" href="#">
                        注销
                    </a>
                </div>
            </div>
        </div>
        <div class="main">
            <div class="left">	
                <ul class="menu">
                    <li>
                        <a href="#">商品管理</a>
                        <ul class="submenu">
                            <li><a href="">商品信息</a></li>
                            <li><a href="">商品分类</a></li>
                            <li><a href="">品牌管理</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">订单管理</a>
                        <ul class="submenu">
                            <li><a href="">订单列表</a></li>
                            <li><a href="">退货处理</a></li>
                            <li><a href="">销售统计</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">系统设置</a>
                        <ul class="submenu">
                            <li><a href="">管理员列表</a></li>
                            <li><a href="">修改密码</a></li>
                            <li><a href="">安全退出</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
            <div class="right">
                <div class="link-nav">
                    <ul>
                        <li><a href="#">商品管理</a></li>
                        <li>商品信息</li>
                    </ul>
                </div>
                <!-- 搜索表单组 start -->
                <div class="form-wrap">
                    <div class="search-title">
                        <i class="fa fa-search"></i>
                        <span>商品搜索</span>
                    </div>
                    <!-- 搜索条件表单 -->
                    <form class="form-inline" action="">
                        <div class="input-group">
                            <i class="fa fa-list"></i>
                            <input class="form-control" type="text" placeholder="商品名">
                        </div>
                        <div class="input-group">
                            <i class="fa fa-battery"></i>
                            <input class="form-control" type="text" placeholder="商品品牌">
                        </div>
                        <div class="input-group">
                            <i class="fa fa-list"></i>
                            <select class="form-control" name="" id="">
                                <option value="">手机</option>
                                <option value="">电脑</option>
                                <option value="">家用电器</option>
                            </select>
                        </div>
                        <div class="input-group">
                            <i class="fa fa-star"></i>
                            <select class="form-control" name="" id="">
                                <option value="">上架</option>
                                <option value="">下架</option>
                            </select>
                        </div>

                        <button class="btn btn-primary">搜索</button>

                        <button class="btn btn-danger">重置</button>
                    </form>
                </div>
                <!-- 搜索表单组 end -->
                <!-- 按钮组 start -->
                <div class="btn-group">
                    <button class="btn btn-primary">添加商品</button>
                </div>
                <!-- 按钮组 end -->
                <!-- 数据列表 start -->
                <div class="data-wrap">
                    <table class="tab">
                        <tr>
                            <th>序号</th>
                            <th>图片</th>
                            <th>商品名</th>
                            <th>单价</th>
                            <th>库存</th>
                            <th>分类</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        <tr>
                            <td>001</td>
                            <td><img class="goods-icon" src="img/img2.jpg" alt="米兔故事机"></td>
                            <td>米兔故事机</td>
                            <td>¥199</td>
                            <td>无限</td>
                            <td>家用电器</td>
                            <td>上架中</td>
                            <td>
                                <button class="btn btn-primary btn-sm">编辑</button>
                                <button class="btn btn-danger btn-sm">下架</button>
                            </td>
                        </tr>
                        <tr>
                            <td>001</td>
                            <td><img class="goods-icon" src="img/img2.jpg" alt="米兔故事机"></td>
                            <td>米兔故事机</td>
                            <td>¥199</td>
                            <td>无限</td>
                            <td>家用电器</td>
                            <td>上架中</td>
                            <td>
                                <button class="btn btn-primary btn-sm">编辑</button>
                                <button class="btn btn-danger btn-sm">下架</button>
                            </td>
                        </tr>
                        <tr>
                            <td>001</td>
                            <td><img class="goods-icon" src="img/img2.jpg" alt="米兔故事机"></td>
                            <td>米兔故事机</td>
                            <td>¥199</td>
                            <td>无限</td>
                            <td>家用电器</td>
                            <td>上架中</td>
                            <td>
                                <button class="btn btn-primary btn-sm">编辑</button>
                                <button class="btn btn-danger btn-sm">下架</button>
                            </td>
                        </tr>
                        <tr>
                            <td>001</td>
                            <td><img class="goods-icon" src="img/img2.jpg" alt="米兔故事机"></td>
                            <td>米兔故事机</td>
                            <td>¥199</td>
                            <td>无限</td>
                            <td>家用电器</td>
                            <td>上架中</td>
                            <td>
                                <button class="btn btn-primary btn-sm">编辑</button>
                                <button class="btn btn-danger btn-sm">下架</button>
                            </td>
                        </tr>
                        <tr>
                            <td>001</td>
                            <td><img class="goods-icon" src="img/img2.jpg" alt="米兔故事机"></td>
                            <td>米兔故事机</td>
                            <td>¥199</td>
                            <td>无限</td>
                            <td>家用电器</td>
                            <td>上架中</td>
                            <td>
                                <button class="btn btn-primary btn-sm">编辑</button>
                                <button class="btn btn-danger btn-sm">下架</button>
                            </td>
                        </tr>
                    </table>
                    <!-- 分页组件start -->
                    <ul class="page text-center">
                        <li><a href="">&lt;&lt;</a></li>
                        <li><a href="">1</a></li>
                        <li><a class="active" href="">2</a></li>
                        <li><a href="">3</a></li>
                        <li><a href="">&gt;&gt;</a></li>
                    </ul>
                    <!-- 分页组件 end -->
                </div>
                <!-- 数据列表 end -->
            </div>
        </div>
    </body>
</html>

####核心样式文件 (main.css)

/*界面整体框架结构*/
body{
	background: #34495E;
}
div {
	box-sizing: border-box;
}

.header {
	position:relative;
	width:100%;
	height: 80px;
	background: #333;
}
.main {
	position: relative;
	width:100%;
	min-height: 600px;
}

.header,
.main {
	min-width: 1000px;
}

.left,
.right {
	position:absolute;
	top:0;
	min-height: 600px;
}
.left{
	left:0;
	width:20%;
}
.right{
	padding:5px;
	left:20%;
	right: 0;
	width:80%;
	background: #eee;
}

/*头部样式*/
.header > div {
	float:left;
	height: 100%;
}
.logo {
	width:20%;
}
.title {
	width:60%;
	text-align: center;
	line-height: 80px;
	color:#fff;
}
.user-info{
	width:20%;
}
.logo img{
	margin:9px auto;
	height: 62px;
	width: 172px;
}
.user-wrap{
	position: absolute;
	right: 20px;
	bottom: 20px;
}

#account,
.exit{
	font-size:12pt;
	color:#ddd;
}

.exit{
	text-decoration: none;
	color:#bbb;
}
.exit:hover{
	color:#f00;
}

/*左侧菜单*/
.left li a{
	display: block;
	height: 40px;
	line-height: 40px;
	text-decoration: none;
	text-align: center;
}
/*设置一级菜单样式*/
.menu > li > a{
	height: 50px;
	line-height: 50px;
	font-size: 16px;
	color:#ddd;
	background: #2c3e50;
}
.submenu > li > a{
	font-size:14px;
	color:#eee;
	background: #34495e;
	transition:all .8s;/*过渡*/
}
/*设置二级菜单项的底部边框(除最后一个元素外)*/
.submenu > li:not(:last-child) {
	border-bottom:1px solid rgba(255,255,255,.1);
}

.submenu > li > a:hover{
	color: #222;
	background: #ddd;
}

/*右侧内容*/
.link-nav>ul>li{
	display: inline-block;
	margin-left:5px;
	font-size: 14px;
}
.link-nav a{
	display: inline-block;
}
.link-nav>ul>li:not(:last-child)::after { 
	content: "   >";
}

.form-wrap,
.data-wrap{
	margin:10px;
	padding:10px;
	border:1px solid rgba(0,0,0,0.2);
	overflow: hidden;
}

.input-group{
	width:200px;
	height:30px;
	border:1px solid rgba(0,0,0,.5);
	background:#fff;
}

.input-group>i{
	float:left;
	width:28px;
	height:28px;
	text-align: center;
	line-height: 28px;
}
.form-control {
	float:left;
	padding:0 5px;
	height: 28px;
	width: 170px;
	border:0;
	box-sizing: border-box;
}
.form-wrap>form {margin-top:10px;}

.form-inline > div,
.btn{
	float:left;
	margin: 10px;
}

.btn{
	padding:0 20px;
	height:30px;
	background:#ddd;
	border:1px solid rgba(0,0,0,0.1);
	border-radius: 5px;/*圆角边框*/
}

.btn-primary{
	color:#eee;
	background: #3498db;
	border:1px solid #3498db;
}

.btn-danger{
	color:#eee;
	background: #e74c3c;
	border:1px solid #e74c3c;
}

.btn-primary:hover{
	background: #2980b9;
	border-color: #2980b9;
}
.btn-danger:hover{
	background: #c0392b;
	border-color: #c0392b;
}
/*数据列表样式*/
.data-wrap{
	clear:both;/*清除浮动*/
}

.tab {
	width:100%;
	border-collapse: collapse;
}

th {
	color:#eee;
	background: #34495E;
}

th,
td {
	padding:8px;
	text-align: center;
	/* border-bottom: 1px solid rgba(0,0,0,.5); */
	/* border: 1px solid rgba(0,0,0,.5); */
}

/*选中所有除了第一行外的tr的偶数行*/
.tab tr:not(:first-child):nth-child(2n){
	background: #ddd;
}
.tab tr:not(:first-child):hover{
	background: rgba(0,0,0,.3);
}
.goods-icon{
	margin:0 auto;
	width: 60px;
	height: 60px;
	border:1px solid rgba(0,0,0,.1);
}

.btn-sm{
	font-size: 12px;
	padding:0 10px;
}

.tab td:nth-child(8){
	display: flex;
	align-items: center;
	justify-content: center;
}

/*分页组件的样式*/
.page{
	margin-top:20px;
}
.page > li{
	display: inline-block;
	width:30px;
	height:30px;
	border-radius: 5px;
}
.page > li > a {
	display: block;
	text-align: center;
	line-height: 30px;
	color:#3498db;
	text-decoration: none;
	border: 1px solid #3498db;
}

.text-left{text-align: left;}
.text-right{text-align: right;}
.text-center{text-align: center;}

.active,
.page > li > a:hover{
	color:#eee !important;
	background:#3498db;
	border-color: #3498db;
}
重置样式文件(reset.css)
*{
	margin:0;
	padding:0;
}
a{
	font-size: 12px;
	text-decoration: 0;
	color:#222;
}
a,
input,
button,
select,
textarea{
	outline: none;
}
ul,ol,li{
	list-style: none;
}
h1,h2,h3,h4,h5,h6,b,strong{
	font-weight: 100;
}
img{
	display: block;
	border: 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值