推荐阅读:
CSS编码规范
HTML编码规范
JavaScript编码规范
HTML 作为描述网页结构的超文本标记语言,在百度一直有着广泛的应用。本文档的目标是使 HTML 代码风格保持一致,容易被理解和被维护。
4
2
tab
解释: 对于非 HTML 标签之间的缩进,比如 script 或 style 标签内容缩进,与 script 或 style 标签的缩进同级。
示例:
<style>/* 样式内容的第一级缩进与所属的 style 标签对齐 */ul { padding: 0;}</style><ul> <li>first</li> <li>second</li></ul><script>// 脚本代码的第一级缩进与所属的 script 标签对齐require(['app'], function (app) { app.init();});</script>
120
解释:
过长的代码不容易阅读与维护。但是考虑到 HTML 的特殊性,不做硬性要求。
class
-
<!-- good --><div class="sidebar"></div><!-- bad --><div class="left"></div>
id
同一个页面中,不同的元素包含相同的 id,不符合 id 的属性含义。并且使用 document.getElementById 时可能导致难以追查的问题。
document.getElementById
<!-- good --><div id="nav"></div><!-- bad --><div id="navigation"></div><!-- good --><p class="comment"></p><!-- bad --><p class="com"></p><!-- good --><span class="author"></span><!-- bad --><span class="red"></span>
hook 脚本
不允许 class 只用于让 JavaScript 选择某些元素,class 应该具有明确的语义和样式。否则容易导致 CSS class 泛滥。
使用 id、属性选择作为 hook 是更好的方式。
name
IE 浏览器会混淆元素的 id 和 name 属性, document.getElementById 可能获得不期望的元素。所以在对元素的 id 与 name 属性的命名需要非常小心。
一个比较好的实践是,为 id 和 name 使用不同的命名法。
<input name="foo"><div id="foo"></div><script>// IE6 将显示 INPUTalert(document.getElementById('foo').tagName);</script>
<!-- good --><p>Hello StyleGuide!</p><!-- bad --><P>Hello StyleGuide!</P>
常见无需自闭合标签有 input、br、img、hr 等。
input
br
img
hr
<!-- good --><input type="text" name="title"><!-- bad --><input type="text" name="title" />
HTML5
对代码体积要求非常严苛的场景,可以例外。比如:第三方页面使用的投放系统。
<!-- good --><ul> <li>first</li> <li>second</li></ul><!-- bad --><ul> <li>first <li>second</ul>
比如 div 不得置于 p 中,tbody 必须置于 table 中。
div
p
tbody
table
详细的标签嵌套规则参见HTML DTD中的 Elements 定义部分。
Elements
下面是常见标签语义
<!-- good --><p>Esprima serves as an important <strong>building block</strong> for some JavaScript language tools.</p><!-- bad --><div>Esprima serves as an important <span class="strong">building block</span> for some JavaScript language tools.</div>
在兼容性允许的情况下应尽量保持语义正确性。对网格对齐和拉伸性有严格要求的场景允许例外,如多列复杂表单。
<!-- good --><img class="avatar" src="image.png"><!-- bad --><span class="avatar"> <img src="image.png"></span>
<!-- good --><table cellspacing="0">...</table><!-- bad --><table cellSpacing="0">...</table>
不允许使用单引号,不允许不使用引号。
<!-- good --><script src="esl.js"></script><!-- bad --><script src='esl.js'></script><script src=esl.js></script>
<input type="text" disabled><input type="checkbox" value="1" checked>
xxx-
data-
使用前缀有助于区分自定义属性和标准定义的属性。
<ol data-ui-type="Select"></ol>
doctype
DOCTYPE
<!DOCTYPE html>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
html
lang
有助于提高页面的可访问性,如:让语音合成工具确定其所应该采用的发音,令翻译工具确定其翻译语言等。
<html lang="zh-CN">
meta
head
见 HTML5 Charset能用吗 一文。
<html> <head> <meta charset="UTF-8"> ...... </head> <body> ...... </body></html>
HTML
BOM
UTF-8
UTF-8 编码具有更广泛的适应性。BOM 在使用程序或工具处理文件时可能造成不必要的干扰。
CSS
rel="stylesheet"
<link rel="stylesheet" href="page.css">
JavaScript
type
text/css 和 text/javascript 是 type 的默认值。
text/css
text/javascript
结构-样式-行为的代码分离,对于提高代码的可阅读性和维护性都有好处。
在页面渲染的过程中,新的CSS可能导致元素的样式重新计算和绘制,页面闪烁。
将 script 放在页面中间将阻断页面的渲染。出于性能方面的考虑,如非必要,请遵守此条建议。
script
<body> <!-- a lot of elements --> <script src="init-behavior.js"></script></body>
URL
使用 protocol-relative URL 引入 CSS,在 IE7/8 下,会发两次请求。是否使用 protocol-relative URL 应充分考虑页面针对的环境。
protocol-relative URL
IE7/8
<script src="//s1.bdstatic.com/cache/static/jquery-1.10.2.min_f2fb5194.js"></script>
title
charset
title 中如果包含 ASCII 之外的字符,浏览器需要知道字符编码类型才能进行解码,否则可能导致乱码。
<head> <meta charset="UTF-8"> <title>页面标题</title></head>
favicon
在未指定 favicon 时,大多数浏览器会请求 Web Server 根目录下的 favicon.ico 。为了保证 favicon 可访问,避免 404,必须遵循以下两种方法之一:
favicon.ico
link
<link rel="shortcut icon" href="path/to/favicon.ico">
viewport
viewport meta tag 可以设置可视区域的宽度和初始缩放大小,避免在移动设备上出现页面展示不正常。
比如,在页面宽度小于 980px 时,若需 iOS 设备友好,应当设置 viewport 的 width 值来适应你的页面宽度。同时因为不同移动设备分辨率不同,在设置时,应当使用 device-width 和 device-height 变量。
980px
width
device-width
device-height
另外,为了使 viewport 正常工作,在页面内容样式布局设计上也要做相应调整,如避免绝对定位等。关于 viewport 的更多介绍,可以参见 Safari Web Content Guide的介绍
src
src 取值为空,会导致部分浏览器重新加载一次当前页面,参考:https://developer.yahoo.com/performance/rules.html#emptysrc
多余的 title 影响看图体验,并且增加了页面尺寸。
alt
可以提高图片加载失败时的用户体验。
height
label
有两种方式:
for
推荐使用第一种,减少不必要的 id。如果 DOM 结构不允许直接嵌套,则应使用第二种。
<label><input type="checkbox" name="confirm" value="on"> 我已确认上述条款</label><label for="username">用户名:</label> <input type="textbox" name="username" id="username">
button
button 元素的默认 type 为 submit,如果被置于 form 元素中,点击后将导致表单提交。为显示区分其作用方便理解,必须给出 type 属性。
submit
form
<button type="submit">提交</button><button type="button">取消</button>
由于浏览器兼容性问题,使用按钮的 name 属性会带来许多难以发现的问题。具体情况可参考此文。
负责主要功能的按钮应相对靠前,以提高可访问性。如果在 CSS 中指定了 float: right 则可能导致视觉上主按钮在前,而 DOM 中主按钮靠后的情况。
float: right
<!-- good --><style>.buttons .button-group { float: right;}</style><div class="buttons"> <div class="button-group"> <button type="submit">提交</button> <button type="button">取消</button> </div></div><!-- bad --><style>.buttons button { float: right;}</style><div class="buttons"> <button type="button">取消</button> <button type="submit">提交</button></div>
当浏览器 JS 运行错误或关闭 JS 时,提交功能将无法工作。如果正确指定了 form 元素的 action 属性和表单控件的 name 属性时,提交仍可继续进行。
action
<form action="/login" method="post"> <p><input name="username" type="text" placeholder="用户名"></p> <p><input name="password" type="password" placeholder="密码"></p></form>
根据内容类型指定输入框类型,能获得能友好的输入体验。
<input type="date">
audio
video
音频应尽可能覆盖到如下格式:
视频应尽可能覆盖到如下格式:
<audio controls> <source src="audio.mp3" type="audio/mpeg"> <source src="audio.ogg" type="audio/ogg"> <object width="100" height="50" data="audio.mp3"> <embed width="100" height="50" src="audio.swf"> </object></audio><video width="100" height="50" controls> <source src="video.mp4" type="video/mp4"> <source src="video.ogg" type="video/ogg"> <object width="100" height="50" data="video.mp4"> <embed width="100" height="50" src="video.swf"> </object></video>
object
<object width="100" height="50" data="something.swf">DO NOT SUPPORT THIS TAG</object>
<!-- good -->{if $display == true}<div> <ul> {foreach $item_list as $item} <li>{$item.name}<li> {/foreach} </ul></div>{/if}<!-- bad -->{if $display == true} <div> <ul> {foreach $item_list as $item} <li>{$item.name}<li> {/foreach} </ul> </div>{/if}
<!-- good --><li class="{if $item.type_id == $current_type}focus{/if}">{ $item.type_name }</li><!-- bad --><li {if $item.type_id == $current_type} class="focus"{/if}>{ $item.type_name }</li>
<!-- good --><table> {foreach $item_list as $item_group} <tr> {foreach $item_group as $item} <td>{ $item.name }</td> {/foreach} <tr> {/foreach}</table><!-- bad --><table><tr> {foreach $item_list as $item} <td>{ $item.name }</td> {if $item@iteration is div by 5} </tr> <tr> {/if} {/foreach}</tr></table>
原文链接:http://www.cnblogs.com/Javame/p/14681543.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728