HTML (HyperText Markup Language) 是构建网页的基础语言。这份详尽的速查表涵盖了 HTML5 的所有主要元素、属性和技术,适合从初学者到高级开发者的所有用户参考使用。
一、HTML 文档基础结构
1.1 基本文档模板
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文档标题</title>
<!-- 其他元信息和链接 -->
</head>
<body>
<!-- 页面内容 -->
</body>
</html>
1.2 文档类型声明
<!DOCTYPE html>: HTML5 文档类型声明<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">: HTML 4.01 严格型<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">: XHTML 1.0 严格型
二、头部元素 (Head Elements)
2.1 元数据标签
<meta charset="UTF-8"> <!-- 字符编码 -->
<meta name="description" content="页面描述"> <!-- 页面描述 -->
<meta name="keywords" content="关键词1, 关键词2"> <!-- 关键词 -->
<meta name="author" content="作者名"> <!-- 作者信息 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 视口设置 -->
<meta http-equiv="refresh" content="30"> <!-- 30秒后刷新 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- IE兼容模式 -->
2.2 链接和资源
<link rel="stylesheet" href="styles.css"> <!-- 外部样式表 -->
<link rel="icon" href="favicon.ico" type="image/x-icon"> <!-- 网站图标 -->
<link rel="apple-touch-icon" href="icon.png"> <!-- iOS主屏幕图标 -->
<link rel="canonical" href="https://example.com/page"> <!-- 规范URL -->
<link rel="alternate" hreflang="en" href="https://example.com/en"> <!-- 多语言版本 -->
2.3 脚本
<script src="script.js"></script> <!-- 外部脚本 -->
<script src="script.js" async></script> <!-- 异步加载脚本 -->
<script src="script.js" defer></script> <!-- 延迟执行脚本 -->
<script>/* 内联脚本 */</script>
<noscript>您的浏览器不支持JavaScript时的内容</noscript>
三、文本内容与结构
3.1 标题与段落
<h1>一级标题</h1> <!-- 最重要的标题 -->
<h2>二级标题</h2>
<h3>三级标题</h3>
<h4>四级标题</h4>
<h5>五级标题</h5>
<h6>六级标题</h6>
<p>这是一个段落。</p>
<p>另一个段落。</p>
3.2 文本格式化
<strong>重要文本(语义化)</strong>
<b>粗体文本(视觉化)</b>
<em>强调文本(语义化)</em>
<i>斜体文本(视觉化)</i>
<u>下划线文本</u>
<s>删除线文本</s>
<del>被删除的内容</del>
<ins>插入的内容</ins>
<mark>高亮文本</mark>
<small>小号文本</small>
<sup>上标</sup>
<sub>下标</sub>
<abbr title="World Wide Web">WWW</abbr> <!-- 缩写 -->
<cite>引用标题</cite>
<q>短引用</q>
<blockquote cite="source.html">长引用</blockquote>
<code>代码片段</code>
<kbd>键盘输入</kbd>
<samp>程序输出</samp>
<var>变量</var>
<time datetime="2023-10-05">10月5日</time>
3.3 列表
<!-- 无序列表 -->
<ul>
<li>项目一</li>
<li>项目二</li>
</ul>
<!-- 有序列表 -->
<ol>
<li>第一项</li>
<li>第二项</li>
</ol>
<!-- 自定义列表 -->
<dl>
<dt>术语</dt>
<dd>定义</dd>
<dt>HTML</dt>
<dd>超文本标记语言</dd>
</dl>
3.4 分隔与换行
<p>第一段<br>换行</p>
<hr> <!-- 水平分隔线 -->
<pre>预先格式化的文本,
保留空格和换行</pre>
四、超链接与导航
4.1 基本链接
<a href="https://example.com">外部链接</a>
<a href="page.html">内部链接</a>
<a href="#section-id">锚点链接</a>
<a href="mailto:email@example.com">邮件链接</a>
<a href="tel:+123456789">电话链接</a>
<a href="javascript:void(0);">JavaScript链接</a>
4.2 链接属性
<a href="page.html" target="_blank">新窗口打开</a>
<a href="doc.pdf" download>下载文件</a>
<a href="page.html" rel="nofollow">不传递权重</a>
<a href="en/page.html" hreflang="en">英文版</a>
4.3 导航区域
<nav>
<ul>
<li><a href="/">首页</a></li>
<li><a href="/about">关于</a></li>
</ul>
</nav>
五、多媒体内容
5.1 图像
<img src="/frontend/html//frontend/html//frontend/html/image.jpg" alt="图片描述" width="300" height="200">
<img src="image.webp" alt="现代格式" loading="lazy"> <!-- 延迟加载 -->
<img srcset="/frontend/html/small.jpg 480w, /frontend/html/medium.jpg 1024w, /frontend/html/large.jpg 1600w"
sizes="(max-width: 600px) 480px, (max-width: 1200px) 1024px, 1600px"
src="/frontend/html/medium.jpg" alt="响应式图片">
<picture>
<source media="(min-width: 800px)" srcset="/frontend/html/large.jpg">
<source media="(min-width: 400px)" srcset="/frontend/html/medium.jpg">
<img src="/frontend/html/small.jpg" alt="响应式图片">
</picture>
5.2 音频与视频
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
您的浏览器不支持音频元素。
</audio>
<video width="320" height="240" controls poster="preview.jpg">
<source src="movie.mp4" type="video/mp4">
<source src="movie.webm" type="video/webm">
您的浏览器不支持视频元素。
</video>
<!-- 字幕轨道 -->
<video>
<track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
<track src="subtitles_zh.vtt" kind="subtitles" srclang="zh" label="中文">
</video>
5.3 嵌入内容
<iframe src="page.html" title="内嵌页面" width="400" height="300"></iframe>
<iframe src="https://maps.example.com" allowfullscreen></iframe>
<embed src="flash.swf" type="application/x-shockwave-flash" width="400" height="300">
<object data="pdf.pdf" type="application/pdf" width="400" height="300">
<param name="autoplay" value="true">
</object>
六、表格
6.1 基本表格结构
<table>
<caption>表格标题</caption>
<thead>
<tr>
<th>标题1</th>
<th>标题2</th>
</tr>
</thead>
<tbody>
<tr>
<td>数据1</td>
<td>数据2</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>页脚1</td>
<td>页脚2</td>
</tr>
</tfoot>
</table>
6.2 表格高级特性
<table>
<colgroup>
<col span="2" style="background-color: #f2f2f2;">
<col style="background-color: #ddd;">
</colgroup>
<tr>
<th rowspan="2">跨行标题</th>
<th colspan="2">跨列标题</th>
</tr>
<tr>
<td headers="col1">数据</td>
<td headers="col2">数据</td>
</tr>
</table>
七、表单与用户输入
7.1 基本表单结构
<form action="/submit" method="post" enctype="multipart/form-data" novalidate>
<fieldset>
<legend>表单组标题</legend>
<!-- 表单控件 -->
</fieldset>
</form>
7.2 输入控件
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required placeholder="请输入姓名">
<input type="password" name="pwd" minlength="8">
<input type="email" name="email" multiple> <!-- 多个邮箱 -->
<input type="url" name="website">
<input type="tel" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<input type="number" name="age" min="18" max="99" step="1">
<input type="range" name="volume" min="0" max="100" step="5">
<input type="date" name="birthday" min="1900-01-01" max="2023-12-31">
<input type="time" name="meeting" min="09:00" max="18:00">
<input type="datetime-local" name="event">
<input type="month" name="month">
<input type="week" name="week">
<input type="color" name="favcolor">
<input type="file" name="avatar" accept="image/png, image/jpeg">
<input type="hidden" name="token" value="abc123">
<input type="search" name="q" results="5" autosave="unique-id">
<input type="checkbox" id="subscribe" name="subscribe" checked>
<label for="subscribe">订阅</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">男</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">女</label>
7.3 其他表单元素
<select name="country">
<option value="">--请选择--</option>
<option value="cn">中国</option>
<option value="us">美国</option>
</select>
<select name="cars" multiple size="4">
<optgroup label="德国车">
<option value="bmw">宝马</option>
</optgroup>
</select>
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
</datalist>
<input list="browsers" name="browser">
<textarea name="message" rows="4" cols="50" wrap="hard"></textarea>
<button type="submit">提交</button>
<button type="reset">重置</button>
<button type="button">普通按钮</button>
<output name="result" for="a b">0</output>
<meter value="2" min="0" max="10">2 out of 10</meter>
<progress value="70" max="100">70%</progress>
7.4 表单属性
<input name="username" autocomplete="username" autofocus>
<input name="password" autocomplete="current-password" disabled>
<input name="email" readonly value="user@example.com">
<input type="text" pattern="[A-Za-z]{3}" title="三个字母">
<input type="text" inputmode="numeric"> <!-- 移动端键盘类型 -->
八、语义化结构元素
<header>页眉</header>
<main>主要内容</main>
<article>独立内容</article>
<section>文档章节</section>
<aside>侧边内容</aside>
<footer>页脚</footer>
<address>联系信息</address>
<figure>
<img src="/frontend/html//frontend/html//frontend/html/image.jpg" alt="描述">
<figcaption>图片说明</figcaption>
</figure>
<details>
<summary>更多信息</summary>
<p>详细内容...</p>
</details>
<dialog open>对话框</dialog>
九、交互元素
<details>
<summary>点击展开</summary>
<p>隐藏的内容</p>
</details>
<dialog open>
<p>这是一个对话框</p>
<button onclick="this.parentElement.close()">关闭</button>
</dialog>
<template id="product-template">
<div class="product">
<h2></h2>
<p></p>
</div>
</template>
<slot name="content">默认内容</slot> <!-- Web Components使用 -->
十、全局属性
<div id="unique-id" class="class1 class2" style="color: red;"></div>
<p hidden>隐藏内容</p>
<div contenteditable="true">可编辑内容</div>
<span draggable="true" data-id="123">可拖动元素</span>
<div lang="zh" dir="ltr">中文内容</div>
<div tabindex="0" title="提示信息">可获得焦点元素</div>
<div spellcheck="true">拼写检查</div>
<div translate="no">不翻译</div>
<div accesskey="s" contextmenu="menu-id">快捷键访问</div>
十一、元字符与特殊符号
<!-- 空格 -->
< <!-- < -->
> <!-- > -->
& <!-- & -->
© <!-- © -->
® <!-- ® -->
™ <!-- ™ -->
€ <!-- € -->
¥ <!-- ¥ -->
£ <!-- £ -->
¢ <!-- ¢ -->
° <!-- ° -->
× <!-- × -->
÷ <!-- ÷ -->
± <!-- ± -->
½ <!-- ½ -->
♥ <!-- ♥ -->
♠ <!-- ♠ -->
♣ <!-- ♣ -->
♦ <!-- ♦ -->
十二、HTML5 API 相关元素
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(0, 0, 150, 75);
</script>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" fill="yellow" />
</svg>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mi>a</mi><mn>2</mn></msup>
<mo>+</mo>
<msup><mi>b</mi><mn>2</mn></msup>
<mo>=</mo>
<msup><mi>c</mi><mn>2</mn></msup>
</mrow>
</math>
十三、性能优化相关
<!-- 预加载关键资源 -->
<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>
<!-- DNS预连接 -->
<link rel="preconnect" href="https://cdn.example.com">
<!-- 预获取 -->
<link rel="prefetch" href="next-page.html">
<!-- 预渲染 -->
<link rel="prerender" href="next-page.html">
<!-- 懒加载图片 -->
<img src="placeholder.jpg" data-src="/frontend/html//frontend/html//frontend/html/image.jpg" loading="lazy" alt="...">
<!-- 响应式图片 -->
<picture>
<source srcset="/frontend/html/small.jpg" media="(max-width: 600px)">
<img src="/frontend/html/large.jpg" alt="...">
</picture>
十四、无障碍访问 (ARIA)
<button aria-label="关闭">X</button>
<div role="navigation" aria-label="主菜单">...</div>
<div aria-hidden="true">不读给屏幕阅读器的内容</div>
<div aria-live="polite">动态更新的内容</div>
<input aria-invalid="true" aria-describedby="error-msg">
<span id="error-msg" role="alert">错误信息</span>
十五、国际化
<html lang="zh-CN">
<!-- 或 -->
<html lang="en-US">
<!-- 双向文本 -->
<p dir="ltr">从左到右文本</p>
<p dir="rtl">从右到左文本</p>
<bdo dir="rtl">反转文本方向</bdo>
十六、不推荐使用的元素(避免使用)
<font face="Arial" size="3" color="red"> <!-- 使用CSS代替 -->
<center> <!-- 使用CSS代替 -->
<strike> <!-- 使用<del>或<s>代替 -->
<big> <!-- 使用CSS代替 -->
<small> <!-- 仅在旁注使用 -->
<marquee> <!-- 使用JavaScript动画 -->
<blink> <!-- 已废弃 -->
<frame> <!-- 使用<iframe>代替 -->
<frameset> <!-- 已废弃 -->
<noframes> <!-- 已废弃 -->
<applet> <!-- 使用<object>代替 -->
<basefont> <!-- 使用CSS代替 -->
<acronym> <!-- 使用<abbr>代替 -->
<tt> <!-- 使用<code>或CSS代替 -->
十七、HTML验证
使用W3C验证服务检查HTML有效性:
https://validator.w3.org/
十八、浏览器兼容性
参考Can I use网站检查HTML5特性兼容性:
https://caniuse.com/
十九、HTML与SEO最佳实践
- 使用语义化HTML5元素
- 为图片添加alt属性
- 使用规范的标题结构(h1-h6)
- 为链接使用描述性文本
- 使用规范的URL结构
- 添加meta描述和关键词
- 使用结构化数据(microdata, RDFa, JSON-LD)
- 确保移动设备友好
- 使用规范的HTML文档结构
二十、HTML5新特性概览
- 语义化元素(header, footer, article等)
- 表单增强(新input类型, 验证API)
- 多媒体支持(audio, video元素)
- Canvas绘图API
- SVG集成
- Web存储(localStorage, sessionStorage)
- Web Workers(后台脚本)
- 地理位置API
- WebSocket(全双工通信)
- 拖放API
- History API(无刷新导航)
- 应用缓存(已废弃, 改用Service Workers)
- Web Components(自定义元素, Shadow DOM)
这份HTML速查表涵盖了HTML5的主要方面,可以作为日常开发的参考。随着Web标准的演进,建议定期查阅MDN Web文档或W3C规范以获取最新信息。