HTML(5) 代码规范

这是一个段落。


运行实例 »

点击 "运行实例" 按钮查看在线实例

不推荐省略 和 标签。

元素是文档的根元素,用于描述页面的语言:

<!DOCTYPE html>
<html lang="zh">

声明语言是为了方便屏幕阅读器及搜索引擎。

省略 或 在 DOM 和 XML 软件中会崩溃。

省略 在旧版浏览器 (IE9)会发生错误。


省略 ?

在标准 HTML5 中, 标签是可以省略的。

默认情况下,浏览器会将 之前的内容添加到一个默认的 元素上。

实例




牛骨文(niuguwen.cn)


段落 1。

段落 2。


运行实例 »

点击 "运行实例" 按钮查看在线实例

Note现在省略 head 标签还不推荐使用。

元数据

HTML5 中 元素是必须的,标题名描述了页面的主题:</p><div class="example"><div class="example_code"><span class="highLT"><</span><span class="highELE">title</span><span class="highGT">></span>牛骨文学习网<span class="highLT"><</span><span class="highELE">/title</span><span class="highGT">></span></div></div><p>标题和语言可以让搜索引擎很快了解你页面的主题:</p><div class="example"><div class="example_code"><span class="highLT"><</span><span class="highELE">!DOCTYPE</span> <span class="highATT">html</span><span class="highGT">></span><br/><span class="highLT"><</span><span class="highELE">html</span>    <span class="highATT">lang=</span><span class="highVAL">"zh"</span><span class="highGT">></span><br/><span class="highLT"><</span><span class="highELE">head</span><span class="highGT">></span><br/>  <span class="highLT"><</span><span class="highELE">meta</span> <span class="highATT">charset=</span><span class="highVAL">"UTF-8"</span><span class="highGT">></span><br/>  <span class="highLT"><</span><span class="highELE">title</span><span class="highGT">></span>牛骨文学习网<span class="highLT"><</span><span class="highELE">/title</span><span class="highGT">></span><br/><span class="highLT"><</span><span class="highELE">/head</span><span class="highGT">></span></div></div><hr/><h2>HTML 注释</h2><p>注释可以写在 <!-- 和 --> 中:</p><div class="example"><div class="example_code"><span class="highCOM"><!-- 这是注释 --></span></div></div><p>比较长的评论可以在 <!-- 和 --> 中分行写:</p><div class="example"><div class="example_code"><span class="highCOM"><!-- <br/>  这是一个较长评论。 这是  一个较长评论。这是一个较长评论。<br/>  这是  一个较长评论 这是一个较长评论。 这是  一个较长评论。<br/>--></span></div></div><p>长评论第一个字符缩进两个空格,更易于阅读。</p><hr/><h2>样式表</h2><p>样式表使用简洁的语法格式 ( type 属性不是必须的):</p><div class="example"><div class="example_code"><span class="highLT"><</span><span class="highELE">link</span> <span class="highATT">rel=</span><span class="highVAL">"stylesheet"</span> <span class="highATT">href=</span><span class="highVAL">"styles.css?4.11.31"</span><span class="highGT">></span></div></div><p>短的规则可以写成一行:</p><div class="example"><div class="example_code"><span class="highELE">p.into </span>{<span class="highATT">font-family:</span><span class="highVAL"> Verdana;</span> <span class="highATT">font-size:</span><span class="highVAL"> 16em;</span>}</div></div><p>长的规则可以写成多行:</p><div class="example"><div class="example_code"><span class="highELE">body </span>{<br/>  <span class="highATT">background-color:</span><span class="highVAL"> lightgrey;</span><br/>  <span class="highATT">font-family:</span><span class="highVAL"> "Arial Black", Helvetica, sans-serif;</span><br/>  <span class="highATT">font-size:</span><span class="highVAL"> 16em;</span><br/>  <span class="highATT">color:</span><span class="highVAL"> black;</span><br/>}</div></div><ul class=" list-paddingleft-2"><li><p>将左花括号与选择器放在同一行。</p></li><li><p>左花括号与选择器间添加以空格。</p></li><li><p>使用两个空格来缩进。</p></li><li><p>冒号与属性值之间添加已空格。</p></li><li><p>逗号和符号之后使用一个空格。</p></li><li><p>每个属性与值结尾都要使用符号。</p></li><li><p>只有属性值包含空格时才使用引号。</p></li><li><p>右花括号放在新的一行。</p></li><li><p>每行最多 80 个字符。</p></li></ul><table class="lamp"><tbody><tr class="firstRow"><th style="width:34px"><img src="/uploads/images/201712120957521134.jpg" alt="Note"/></th><td>在逗号和分号后添加空格是常用的一个规则。</td></tr></tbody></table><hr/><h2>在 HTML 中载入 JavaScript</h2><p>使用简洁的语法来载入外部的脚本文件 ( type 属性不是必须的 ):</p><div class="example"><div class="example_code"><span class="highLT"><</span><span class="highELE">script</span> <span class="highATT">src=</span><span class="highVAL">"myscript.js?4.11.31"</span><span class="highGT">></span></div></div><hr/><h2>使用 JavaScript 访问 HTML 元素</h2><p>一个糟糕的 HTML 格式可能会导致 JavaScript 执行错误。</p><p>以下两个 JavaScript 语句会输出不同结果:</p><div class="example"><h2 class="example">实例</h2><div class="example_code"><pre class="brush:html;toolbar:false"><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>牛骨文(niuguwen.cn) 段落 1。

段落 2。


运行实例 »

点击 "运行实例" 按钮查看在线实例

HTML  中 JavaScript 尽量使用相同的命名规则。

访问 JavaScript 代码规范。


使用小写文件名

大多 Web 服务器 (Apache, Unix) 对大小写敏感: london.jpg 不能通过 London.jpg 访问。

其他 Web 服务器 (Microsoft, IIS) 对大小写不敏感: london.jpg 可以通过 London.jpg 或 london.jpg 访问。

你必须保持统一的风格,我们建议统一使用小写的文件名。


文件扩展名

HTML 文件后缀可以是 .html  (或r .htm)。

CSS 文件后缀是 .css

JavaScript 文件后缀是 .js


.htm 和 .html 的区别

.htm 和 .html 的扩展名文件本质上是没有区别的。浏览器和 Web 服务器都会把它们当作 HTML 文件来处理。

区别在于:

.htm 应用在早期 DOS 系统,系统现在或者只能有三个字符。

在 Unix 系统中后缀没有特别限制,一般用 .html。

技术上区别

如果一个 URL 没有指定文件名 (如 http://www.niuguwen.cn/css/), 服务器会返回默认的文件名。通常默认文件名为 index.html, index.htm, default.html, 和 default.htm。

如果服务器只配置了 "index.html" 作为默认文件,你必须将文件命名为 "index.html", 而不是 "index.htm"。

但是,通常服务器可以设置多个默认文件,你可以根据需要设置默认文件吗。

不管怎样,HTML 完整的后缀是 ".html"。


文章导航