Welcome to the digital home of Keefe Tang

This is where I keep my opinions & things I refer all in one place. If you have any questions feel free to drop me a line, if you don’t, how about subscribing to my feed.

What Is doctype?

You always hear it—always start with strict doctype—but what is doctype? doctype is short for Doc­u­ment Type Def­i­nition or mostly re­ferred to as dtd and it appears at the very be­ginning of the (x)html doc­u­ment in or­der to identi­fy the con­tent of the doc­u­ment as conform­ing to the partic­ular dtd spec­ification.

dtd pro­vides a list of the el­e­ments, attributes, com­ments, notes, and entities con­tained in the doc­u­ment. It also indicates their relation­ship to one an­oth­er with­in the doc­u­ment. In oth­er words, a dtd is the grammar of an xml or html doc­u­ment, it tells applications like your brows­er or on­line code val­idator what type of markup you’ve coded into the page.

Syntax

Be­low is the doctype for xhtml 1.0 Strict & the syntax for doctype for compari­son.

 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 
  <!DOCTYPE [Top Element] [Availability] "[Registration]//[Organization]//[Type] [Label]//[Language]" "[URL]"> 
  1. [Top Element] – Indicates the top lev­el el­e­ment type de­clared in the dtd.
  2. [Availability] – Indicates if the identi­fi­er is a publicly ac­cessible object (PUBLIC) or a system resource (SYSTEM) such as a local file or url. (x)html dtds are spec­i­fied by PUBLIC identi­fiers.
  3. [Registration] – Indicated by ei­ther a “+” or “-”. A plus symbol indicates that the orga­ni­zation name that fol­lows is iso-reg­is­tered. A mi­nus sign indicates the orga­ni­zation name is not reg­is­tered. The ietf & w3c are not reg­is­tered iso orga­ni­zations and thus use a “-”.
  4. [Organization] – This is the “Owner­ID”—a unique label indicating the name of the entity or orga­ni­zation responsible for the cre­ation and/or main­tenance of the artifact (dtd, etc.) be­ing ref­er­enced by the doctype. The ietf & w3c are the two orig­inating orga­ni­zations of the of­ficial (x)html dtds.
  5. [Type] – This is the “Public Text Class”—the type of object be­ing ref­er­enced. There are many differ­ent keywords pos­sible here, but in the case of an (x)html dtd, it is “dtd”.
  6. [Label] – This is the “Public Text De­scription”—a unique de­scriptive name for the public text that is be­ing ref­er­enced. If the public text changes for any rea­son, a new Public Text De­scription string should be cre­ated for it.
  7. [Language] – This is the “Public Text Language”—the nat­ural language encoding system used in the cre­ation of the ref­er­enced object. It is written as an iso 639 language code. (x)html dtds are usu­ally written in En­glish—“EN”.
  8. [URL] – This is the optional explicit url to the dtd be­ing ref­er­enced.

why specify a doctype?

Because it de­fines which ver­sion of (x)html your doc­u­ment is actually us­ing, and this is a crit­ical piece of information needed by browsers or oth­er tools pro­cess­ing the doc­u­ment. Tools such as the markup val­idator needs it to check the syntax of your (x)html or they won’t know what doc­u­ment it is checking.

Most importantly, modern browsers need that to determine the type of doc­u­ment it is dealing with. When a brows­er don’t know what doc­u­ment it is dealing with, it will fallback to quirks mode, browsers will at­tempt to guess at which language the doc­u­ment was written in and pages will be rendered much slower com­pared to the standard mode and of­ten in error.

doctype list

The list of doctype are much more than what we normally use, w3c have the full list of doctype avail­able to use. Some of the ones we normally use are.

html 4.01

Strict, Transitional & Frameset.

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

xhtml 1.0

Strict, Transitional & Frameset.

 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 

xhtml 1.1

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

html 5

HTML 5’s doctype have a much simpler syntax.

 
  <!DOCTYPE html>