博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码规范
阅读量:7137 次
发布时间:2019-06-28

本文共 6684 字,大约阅读时间需要 22 分钟。

HTML 规范

HTML文件必须加上 DOCTYPE 声明,并统一使用 HTML5 的文档声明:

 

页面语言LANG,考虑浏览器和操作系统的兼容性,目前仍然使用 zh-CN 属性值

 

CHARSET,HTML-5 中默认的字符编码是 UTF-8,请尽量统一写成标准的 “UTF-8”

类型属性

//不需要为 CSS、JS 指定类型属性,HTML5 中默认已包含//推荐
//不推荐

元素属性

//元素属性值使用双引号语法//元素属性值可以写上的都写上

特殊字符引用

//在 HTML 中不能使用小于号 “<” 和大于号 “>”特殊字符,浏览器会将它们作为标签解析,使用字符实体//推荐more >>

纯数字输入框,使用 type="tel"而不是type="number"

文件模版

HTML5标准模版

HTML5标准模版

移动端

移动端HTML模版

PC端

PC端HTML模版

WebApp Meta

Viewport Meta Tag

Apple-Specific Meta Tag Keys

apple-mobile-web-app-capable 设置 WebApp 是否进入全屏模式,该设置需要添加到主屏幕才生效

apple-mobile-web-app-status-bar-style 为 webapp 设置状态栏样式

format-detection 自动识别页面中有可能是电话格式的数字

图片引入

代码规范

css 设置

  • 样式文件必须写上 @charset 规则,并且一定要在样式文件的第一行首个字符位置开始写,编码名用 “UTF-8”

  • 样式文件编码声明 @charset 语句下面注明页面名称、作者、创建日期等信息

  • 字符 @charset “”; 都用小写字母,不能出现转义符,编码名允许大小混写

  • 考虑到在使用“UTF-8”编码情况下 BOM 对代码的污染和编码显示的问题,在可控范围下,坚决不使用 BOM。

@charset "UTF-8";/** * @desc File Info * @author Author Name * @date 2017-7-10 */.demo{}

css 代码风格

//统一使用展开格式书写样式,避免紧凑格式//不使用 ID 选择器//左括号与类名之间一个空格,冒号与属性值之间一个空格//取值不要带有不必要的 0.demo {    display: block;    width: 50px;    color: rgba(255,255,255,.5);}

css 属性书写顺序

  • 布局定位属性:display / position / float / clear / visibility / overflow

  • 自身属性:width / height / margin / padding / border / background

  • 文本属性:color / font / text-decoration / text-align / vertical-align /

    white- space / break-word

  • 其他属性(CSS3):content / cursor / border-radius / box-shadow /

    text-shadow / background:linear-gradient …

//示例.demo {    display: block;    position: relative;    float: left;    width: 100px;    height: 100px;    margin: 0 10px;    padding: 20px 0;    font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;    color: #333;    background: rgba(0,0,0,.5);    -webkit-border-radius: 10px;    -moz-border-radius: 10px;    -o-border-radius: 10px;    -ms-border-radius: 10px;    border-radius: 10px;}

CSS3 浏览器私有前缀写法

.demo {    -webkit-border-radius: 10px;    -moz-border-radius: 10px;    -o-border-radius: 10px;    -ms-border-radius: 10px;    border-radius: 10px;}

重置样式

移动端

* { -webkit-tap-highlight-color: transparent; outline: 0; margin: 0; padding: 0; vertical-align: baseline; }body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td { margin: 0; padding: 0; vertical-align: baseline; }img { border: 0 none; vertical-align: top; }i, em { font-style: normal; }ol, ul { list-style: none; }input, select, button, h1, h2, h3, h4, h5, h6 { font-size: 100%; font-family: inherit; }table { border-collapse: collapse; border-spacing: 0; }a { text-decoration: none; color: #666; }body { margin: 0 auto; min-width: 320px; max-width: 640px; height: 100%; font-size: 14px; fon-family: -apple-system,Helvetica,sans-serif; line-height: 1.5; color: #666; -webkit-text-size-adjust: 100% !important; text-size-adjust: 100% !important; }input[type="text"], textarea { -webkit-appearance: none; -moz-appearance: none; appearance: none; }

PC端

html, body, div, h1, h2, h3, h4, h5, h6, p, dl, dt, dd, ol, ul, li, fieldset, form, label, input, legend, table, caption, tbody, tfoot, thead, tr, th, td, textarea, article, aside, audio, canvas, figure, footer, header, mark, menu, nav, section, time, video { margin: 0; padding: 0; }h1, h2, h3, h4, h5, h6 { font-size: 100%; font-weight: normal }article, aside, dialog, figure, footer, header, hgroup, nav, section, blockquote { display: block; }ul, ol { list-style: none; }img { border: 0 none; vertical-align: top; }blockquote, q { quotes: none; }blockquote:before, blockquote:after, q:before, q:after { content: none; }table { border-collapse: collapse; border-spacing: 0; }strong, em, i { font-style: normal; font-weight: normal; }ins { text-decoration: underline; }del { text-decoration: line-through; }mark { background: none; }input::-ms-clear { display: none !important; }body { font: 12px/1.5 \5FAE\8F6F\96C5\9ED1, \5B8B\4F53, "Microsoft Yahei","微软雅黑",Arial,sans-self; background: #fff; }a { text-decoration: none; color: #333; }a:hover { text-decoration: underline; }

媒体查询

常用查询语句

body {    background-color: pink;}@media screen and (max-width: 960px) {    body {        background-color: darkgoldenrod;    }}@media screen and (max-width: 480px) {    body {        background-color: lightgreen;    }}/* 横屏 */@media all and (orientation :landscape) {} /* 竖屏 */@media all and (orientation :portrait) {}/* 设备宽度大于 320px 小于 640px */@media all and (min-width:320px) and (max-width:640px) {    }/* 设备像素比为 2 */@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) {    }

移动端常用私有属性

-webkit-scrollbar 用于对拥有overflow属性的区域 自定义滚动条的样式

//隐藏滚动条.scroll::-webkit-scrollbar {    width: 0;    height: 0;}

-webkit-touch-callout

- none:系统默认菜单被禁用 - inherit:系统默认菜单不被禁用当你触摸并按住触摸目标时候,禁止或显示系统默认菜单。在iOS上,当你触摸并按住触摸的目标,比如一个链接,Safari浏览器将显示链接有关的系统默认菜单,这个属性可以让你禁用系统默认菜单。

-webkit-user-select 定义用户是否能选中元素内容

HTML/CSS文件命名

确保文件命名总是以字母开头而不是数字,且字母一律小写,以下划线连接且不带其他标点符号

demo.htmldemo_list.htmldemo_detail.html
demo.cssdemo_list.cssdemo_detail.css

ClassName命名

ClassName的命名应该尽量精短、明确,必须以字母开头命名,且全部字母为小写,单词之间统一使用下划线 “_” 连接

ClassName    含义about    关于account    账户arrow    箭头图标article    文章aside    边栏audio    音频avatar    头像bg,background    背景bar    栏(工具类)branding    品牌化crumb,breadcrumbs    面包屑btn,button    按钮caption    标题,说明category    分类chart    图表clearfix    清除浮动close    关闭col,column    列comment    评论community    社区container    容器content    内容copyright    版权current    当前态,选中态default    默认description    描述details    细节disabled    不可用entry    文章,博文error    错误even    偶数,常用于多行列表或表格中fail    失败(提示)feature    专题fewer    收起field    用于表单的输入区域figure    图filter    筛选first    第一个,常用于列表中footer    页脚forum    论坛gallery    画廊group    模块,清除浮动header    页头help    帮助hide    隐藏hightlight    高亮home    主页icon    图标info,information    信息last    最后一个,常用于列表中links    链接login    登录logout    退出logo    标志main    主体menu    菜单meta    作者、更新时间等信息栏,一般位于标题之下module    模块more    更多(展开)msg,message    消息nav,navigation    导航next    下一页nub    小块odd    奇数,常用于多行列表或表格中off    鼠标离开on    鼠标移过output    输出pagination    分页pop,popup    弹窗preview    预览previous    上一页primary    主要progress    进度条promotion    促销rcommd,recommendations    推荐reg,register    注册save    保存search    搜索secondary    次要section    区块selected    已选share    分享show    显示sidebar    边栏,侧栏slide    幻灯片,图片切换sort    排序sub    次级的,子级的submit    提交subscribe    订阅subtitle    副标题success    成功(提示)summary    摘要tab    标签页table    表格txt,text    文本thumbnail    缩略图time    时间tips    提示title    标题video    视频wrap    容器,包,一般用于最外层wrapper    容器,包,一般用于最外层
ad、banner、gg、guanggao 等有机会和广告挂勾的字眠不建议直接用来做ClassName,因为有些浏览器插件(Chrome的广告拦截插件等)会直接过滤这些类名

转载地址:http://dwtrl.baihongyu.com/

你可能感兴趣的文章
mongoDB - 安装
查看>>
xp安装maven
查看>>
(转)sscanf函数的用法
查看>>
30个高质量的免费jquery滑块PSD文件
查看>>
hdu1686(kmp)
查看>>
【web前端面试题整理05】做几道前端面试题休息休息吧
查看>>
SQL查询的几种方式
查看>>
悉尼农历节精彩贺岁活动准备就绪
查看>>
Facebook拟投资10亿美元在弗吉尼亚州打造数据中心
查看>>
图片基础知识梳理(1) ImageView 的 ScaleType 属性解析
查看>>
理解索引:MySQL执行计划详细介绍
查看>>
滑动冲突
查看>>
Ubuntu Linux 中虚拟主机的配置 - 搭配 Nginx
查看>>
1024程序员节最新福利之2018最全Android资料集合
查看>>
源码阅读:SDWebImage(七)——SDWebImageImageIOCoder
查看>>
App 启动优化 之 背景知识
查看>>
检测不同服务器上代码差别的shell的脚本
查看>>
JavaScript的循环方式(1)
查看>>
扎克伯格扎心,传谷歌搜索将接受审查重新进入中国
查看>>
Confluence 6 添加或删除用户组
查看>>