HTML parser ของ browser ทนทาน (forgiving) มาก สามารถ render หน้าเว็บได้แม้ข้าม <html>, <head>, <body> ไป โดยต้องมี <!DOCTYPE html> อยู่เสมอ
ตัวอย่าง Minimal Valid HTML5
<!DOCTYPE html> <title>Title of the page</title> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <p>This is a paragraph.</p>
สิ่งที่ Browser ทำอัตโนมัติ (DOM Construction)
- สร้าง
<html>ครอบทั้งหมด - สร้าง
<head>ย้าย<title>,<meta>,<style>,<link>เข้าไป - สร้าง
<body>ครอบเนื้อหาที่เหลือ - แก้ไขโครงสร้างที่ผิด (misnested tags)
ข้อควรระวัง
- ไม่แนะนำสำหรับ Production - ควรเขียนโครงสร้างครบถ้วน
- บาง JavaScript/CSS selectors คาดหวังโครงสร้างเต็ม (เช่น
html > body) - SEO และ Accessibility อาจได้รับผลกระทบ
- Validation tool อาจแจ้ง warning
เมื่อไหร่ใช้ได้
- Prototyping / Testing ระหว่างพัฒนา
- Code snippets / Examples
- Email HTML (บาง client ต้องการ minimal)
- HTML fragments ที่ inject ผ่าน JS
โครงสร้างมาตรฐานที่แนะนำ
<!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Page Title</title> </head> <body> <!-- Content --> </body> </html>