前端代码开发规范-html篇

该博客介绍了HTML编写规范。头部方面,涉及标准模式声明、语言设置、编码、内核选择、SEO优化、移动端适配、图标与favicon设置等;主体部分强调标签闭合、class命名、属性顺序、嵌套约束等规则,还提及语义化标签的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、html 头部

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

<!DOCTYPE html>

2、使用 lang="zh-cmn-Hans" 而不是我们通常写的 lang="zh-CN" 。理由:参考:https://www.zhihu.com/question/20797118

<!-- 中文 -->

<html lang="zh-Hans">

 

<!-- 简体中文 -->

<html lang="zh-cmn-Hans">

 

<!-- 繁体中文 -->

<html lang="zh-cmn-Hant">

 

<!-- English -->

<html lang="en">

3、使用 utf-8编码

<html>

<head>

<meta charset="utf-8">

......

</head>

<body>

......

</body>

</html>

4、优先使用最新版本的IE 和 Chrome 内核

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

5、seo优化

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<!-- SEO -->

<title>Style Guide</title>

<meta name="keywords" content="your keywords">

<meta name="description" content="your description">

<meta name="author" content="author,email address">

</head>

6、为移动端设备优化,设置可见区域的宽度和初始缩放比例。

<meta name="viewport" content="width=device-width, initial-scale=1.0">

7、iOS 图标

  • apple-touch-icon 图片自动处理成圆角和高光等效果;
  • apple-touch-icon-precomposed 禁止系统自动添加效果,直接显示设计原图;

<!-- iPhone 和 iTouch,默认 57x57 像素,必须有 -->

<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-57x57-precomposed.png">

 

<!-- iPad,72x72 像素,可以没有,但推荐有 -->

<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-72x72-precomposed.png" sizes="72x72">

 

<!-- Retina iPhone 和 Retina iTouch,114x114 像素,可以没有,但推荐有 -->

<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-114x114-precomposed.png" sizes="114x114">

 

<!-- Retina iPad,144x144 像素,可以没有,但推荐有 -->

  • <link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-144x144-precomposed.png" sizes="144x144">

8、favicon

在未指定 favicon 时,大多数浏览器会请求 Web Server 根目录下的 favicon.ico 。为了保证 favicon 可访问,避免404,必须遵循以下两种方法之一:

  • 在 Web Server 根目录放置 favicon.ico 文件;
  • 使用 link 指定 favicon;

<link rel="shortcut icon" href="path/to/favicon.ico">

9、head模版

<!DOCTYPE html>

<html lang="zh-cmn-Hans">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>Style Guide</title>

<meta name="description" content="不超过150个字符">

<meta name="keywords" content="">

<meta name="author" content="name, email@gmail.com">

 

<!-- 为移动设备添加 viewport -->

<meta name="viewport" content="width=device-width, initial-scale=1.0">

 

<!-- iOS 图标 -->

<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-57x57-precomposed.png">

 

<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />

<link rel="shortcut icon" href="path/to/favicon.ico">

</head>

二、html 主体部分

1、所有的html标签要闭合,即使是空的html元素

// bad

<section>

<p>这是一个段落。

<p>这是一个段落。

</section>

// good

<section>

<p>这是一个段落。

<p>这是一个段落。

</section>

 

// bad

<meta charset="utf-8">

// good

<meta charset="utf-8"/>

2、class的命名要以功能或内容命名,不以表现形式命名。

<!-- bad -->

<div class="j-hook left contentWrapper"></div>

 

<!-- good -->

<div class="sidebar content-wrapper"></div>

3、HTML 属性应该按照特定的顺序出现以保证易读性。

  • 绑定事件
  • id
  • class
  • name
  • data-xxx
  • src, for, type, href
  • title, alt

4、html属性的定义,统一使用双引号。

5、关于嵌套应注意以下约束

语义嵌套约束

  • <li> 用于 <ul> 或 <ol> 下;
  • <dd>, <dt> 用于 <dl> 下;
  • <thead>, <tbody>, <tfoot>, <tr>, <td> 用于 <table> 下;

严格嵌套约束

  • inline-Level 元素,仅可以包含文本或其它 inline-Level 元素;
  • <a>里不可以嵌套交互式元素<a>、<button>、<select>等;
  • <p>里不可以嵌套块级元素<div>、<h1>~<h6>、<p>、<ul>/<ol>/<li>、<dl>/<dt>/<dd>、<form>等。

6、html元素的多个属性之间要有空格隔开,尤其是在富文本传输的时候。虽然即便不这样做浏览器也会帮我们优化,但是在某些库或者引擎里解析会出错。

// bad

<img src="http://test.jpg"alt="图片">

// good

<img src="img.url" alt="图片">

7、图片要加上alt属性。理由:可以在网络不好时显示图片内容,有助于seo

// bad

<img src="html5.gif">

// good

<img src="html5.gif" alt="HTML5">

8、定义好图片的尺寸,在加载时可以预留指定空间,减少闪烁。

//bad

<img src="html5.gif" alt="HTML5">

// good

<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">

9、一行代码不要写的过长,长度把握在 80个字符以内即可。atom编辑器中有一个竖线可以作为参考

10、较长的html注释可以用

<!-- 注释1 --> <!-- ./注释1 -->包裹,因为这样可以清楚的知道注释覆盖的范围

<!-- 保价slide -->

<foot-slide title="物品保价" show="{{insuranceSlide.show}}" slide-status="{{insuranceSlide.slideStatus}}" use-custom-left="{{true}}" use-custom-right="{{true}}" bind:click-cancel="clickInsuranceCancel" bind:click-confirm="clickInsuranceGiveUp" bind:click-bg="clickInsuranceCancel" style="z-index: 1300;">

<view slot="slideLeft" ><image src='https://p1.meituan.net/paotui/jsfobiyk4ja.png' mode='aspectFit' class="insureValue-close-icon"></image></view>

<view slot="slideRight">放弃保价</view>

<view slot="slideContent">

<view class="insureValue-content {{insuranceSlide.overLimit ? 'error' : ''}}">

<view class="insureValue-content__agreement mt30" >

<view wx:if="{{!insuranceSlide.checked}}" class="no_check" bindtap="bindCheck" data-val="1"></view>

<view wx:else class="checked" bindtap="bindCheck" data-val="2"></view>

<text>已阅读并同意</text>

<text style="color:#348BED" bind:tap='goWebView' data-url='{{insuranceSlide.agreementUrl}}' >{{insuranceSlide.agreementName}}</text>

</view>

<button class='submit-btn primary {{insuranceSlide.checked && insuranceSlide.inputValue && !insuranceSlide.overLimit ? "" : "disabled"}}' bindtap='handleSubmitInsurance'>提交</button>

</view>

</view>

</foot-slide>

<!-- ./保价slide -->

11、尽量少在html中书写内敛样式。理由:不利用后期维护;浏览器可以运用css样式的缓存,加快加载,减少传输字节数。

三、在适当的场景使用html语义化标签

 

标签

语义

<article>

独立的文档、页面、应用、站点

<section>

按主题将内容分组,通常会有标题

<sidebar>

表示与周围内容关系不太密切的内容,如广告

<p>

段落

<h1> <h2> <h3> ...

标题

<ul>

无序列表

<ol>

有序列表

<blockquote>

大段引用

<cite>

一般引用

<b>

为样式加粗而加粗

<strong>

为强调内容而加粗

<i>

为样式倾斜而倾斜

<em>

为强调内容而倾斜

code

代码标识

abbr

缩写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值