Understanding Doctype Declarations

Doctype Declarations are mandatory on all HTML websites.  A doctype is a pre-defined string that is used to tell a client’s web browser (Internet Explorer, Firefox, Chrome, etc.) what type of code will be contained in the file.

By understanding the type of code, the web browser is able to use the correct rendering engine and display the page as intended.

To understand a doctype, one must first understand the different types of HTML code.  HTML is a coding specification used to define the elements of a webpage, including tables, images, and paragraphs.  Over the years, there have been several different specifications of HTML that were made into standards (typically 1.0, 2.0, 3.0).

The 2 major current standards are HTML 4.01 and XHTML 1.0.  XHTML is very similar to HTML, but adds a few rules to help organize the code better.  The advantages of XHTML include:

  • Elements must be properly nested:  <i><b>Test</i></b> is allowed in HTML, but in XHTML would be:  <i><b>Test</b></i>
  • Elements must always be closed, including elements with only one tag: HTML is <hr>, but in XHTML:  <hr />
  • Elements must be defined in lowercase.  HTML allows <H1>, but in XHTML it must be <h1>
  • Elements must be nested and rooted together.  This goes hand-in-hand with item #1, but goes on to require that all text is contained in an element.

Now that you understand XHTML and HTML, you must choose which one you want to use to write your code in.  I, personally, prefer XHTML due to the cleaner look to the code.

Also, you must choose between Transitional and Strict.  Transitional allows you to use elements and rules from older versions of HTML/XHTML, while Strict forces you to follow the rules of the current specification exactly.  For example, Transitional allows one to use styling attributes of certain HTML tags, while Strict enforces the separation of styling (CSS) and structure (HTML).

Transitional is typically only used when a new specification is released and a large website must be slowly updated to meet all of the rules for Strict.  Therefore, I highly recommend forcing yourself to use the Strict version of whichever specification you choose.

Now that you understand your choices, you can select a doctype declaration here:  http://www.w3.org/QA/2002/04/valid-dtd-list.html

There is a template that can be used or you can simply copy and paste the appropriate doctype into your existing HTML code.  By doing so, you will make your website compatible with more browsers and eliminate common validation errors (http://validator.w3.org).

~Nick Marnik

~ by nkmhockey on February 6, 2011.

%d bloggers like this: