跳到正文
Skillpedia
网站与界面(前端)75k · #2819

doctype

它能做什么

这项技能检查网页是否以正确的 HTML5 doctype 行开头。适合希望在各个浏览器中显示一致的开发者。

适用场景

  • ✓当页面在每个浏览器中的显示都不同时。
  • ✓当你新建页面或模板时。
  • ✓当框架生成的输出可能缺少声明时。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/doctype/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/doctype/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“你能检查一下我的页面是否有正确的 HTML5 doctype 吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会检查页面原始 HTML 的第一行。如果声明缺失、过时或不在首位,它会建议正确的一行。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Use the HTML5 doctype

Without the HTML5 doctype, browsers fall back to Quirks Mode, which emulates the rendering behavior of pre-standards browsers from the late 1990s. In Quirks Mode, the CSS box model changes (padding adds to element width), vertical centering breaks, and many modern HTML5 APIs may behave unexpectedly. This causes cross-browser rendering inconsistencies that are difficult to debug and may produce inaccessible layouts.

Quick Reference

  • Every HTML document must begin with <!DOCTYPE html> as its very first line
  • Without a doctype, browsers enter Quirks Mode — a legacy compatibility mode with inconsistent CSS and JavaScript behavior
  • The HTML5 doctype is case-insensitive but <!DOCTYPE html> (uppercase DOCTYPE) is the conventional form
  • No other content, not even whitespace or BOM characters, should appear before the doctype
  • Frameworks like Next.js, Nuxt, and Remix automatically include the doctype — verify in rendered HTML, not source templates

Check

Check the very first line of the HTML document source (not the DOM — the raw HTTP response). The document must begin with <!DOCTYPE html> (case-insensitive). Flag if: (1) the doctype is missing entirely; (2) an old HTML4 or XHTML doctype is used (contains PUBLIC and a DTD URL); (3) the doctype is not the absolute first content (whitespace or BOM before it triggers some browsers to enter Quirks Mode); (4) the doctype has incorrect syntax.

Fix

Add <!DOCTYPE html> as the absolute first line of the HTML document, before the <html> tag. Remove any old HTML4 or XHTML doctypes. Ensure no whitespace, comments, or BOM markers precede the doctype. For framework projects: verify in the root HTML template file, and confirm in the rendered HTML by viewing page source in the browser.

Explain

The DOCTYPE declaration tells the browser which version of HTML the document uses. <!DOCTYPE html> activates Standards Mode (also called Full Standards Mode) in all modern browsers, ensuring consistent rendering behavior. Without it, browsers enter Quirks Mode to emulate old IE behavior — a compatibility mode that changes the box model, float clearing, table rendering, and other CSS behaviors. The HTML5 doctype was intentionally made simple (just <!DOCTYPE html>) to replace the verbose HTML4/XHTML doctypes that required a full DTD URL.

Code Review

Review templates, server-rendered HTML, and shared components that output markup related to Use the HTML5 doctype. Flag exact elements, attributes, and routes where the rendered HTML violates the rule.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/html/doctype

常见问题

没有 doctype 会怎样?

浏览器会切换到怪异模式(quirks mode),这是一种旧的兼容模式,布局和脚本可能会出现无法预测的行为。

应该在哪里检查?

这项技能说明,应检查浏览器接收到的原始 HTML,例如框架中的根模板。

它之前可以有其他内容吗?

不可以。这项技能说明,声明之前不应有任何内容,哪怕是空白字符。

详情

作者的原始描述

Use when applies to all HTML documents. Always check the very first line of the raw HTML source (view-source: or DevTools > Network > Response). In SSR frameworks, check the root HTML template (e.g., _document.tsx in Next.js, index.html in Vite/Vue, application.html.erb in Rails). The doctype is not a tag — it is a declaration and does not need a closing tag.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2819
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能