Navigate back to the homepage

DOCTYPE 是什麼

HTML
November 16th, 2021 · 1 min read

DOCTYPE 是什麼?

在 HTML 文件一開始第一行我們都要寫上 DOCTYPE:

1<!DOCTYPE html>

DOCTYPE 有什麼用?

DOCTYPE 是 document type 的縮寫,他不是 HTML tag,是用來聲明文件類型,告訴瀏覽器這份文件是屬於什麼文件類型。

現在的網頁基本上都使用<!DOCTYPE html> 來告知瀏覽器這是一份標準的 HTML5 文件。


標準模式(Standards mode) 與 怪異模式(Quirks mode)

標準模式: 依照現今 HTML 與 CSS 規範來渲染網頁。

怪異模式: 如果沒有宣告 DOCTYPE、宣告錯誤的 DOCTYPE、 放任何東西在 DOCTYPE 前面、會造成瀏覽器無法預期的行為,像是渲染 (render) CSS 排版時怎麼和你想要的不太一樣,稱之為瀏覽器處於怪異模式 (Quirks mode)。


HTML 文檔類型

  1. 在 HTML5 中,<!DOCTYPE>聲明很簡單:
1<!DOCTYPE html>
  1. 在較舊的文檔(HTML 4 或 XHTML)中,聲明更複雜,因為聲明必須引用 DTD(文檔類型定義)。

HTML 4.01:

1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

XHTML 1.1:

1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

其他

  1. 所有 HTML 文檔都必須以<!DOCTYPE>聲明開頭。
1<!DOCTYPE html>
2<html>
3<head>
4<title>Title of the document</title>
5</head>
6<body>
7The content of the document......
8</body>
9</html>
  1. <!DOCTYPE>聲明是不區分大小寫。
1<!DOCTYPE html>
2<!DocType html>
3<!Doctype html>
4<!doctype html>

參考資料

https://www.fooish.com/html/doctype.html https://ithelp.ithome.com.tw/articles/10236447 https://developer.mozilla.org/zh-TW/docs/Web/HTML/Quirks_Mode_and_Standards_Mode https://www.w3schools.com/tags/tag_doctype.asp

More articles from Iris Blog

Day13: Slide in on Scroll

滾動視窗到定點時動畫滑入圖片的效果。

July 7th, 2021 · 1 min read

Day12: Key Sequence Detection

按下密碼後,在頁面顯示一些驚喜的圖片。

July 5th, 2021 · 1 min read
© 2020–2021 Iris Blog
Link to $mailto:qweichew@gmail.comLink to $https://github.com/iris1114Link to $https://www.instagram.com/iris.justdoit/Link to $https://www.linkedin.com/in/iris-chew