Skip to content
loading...
Discussion
markdown guide
 

It is Doctype Declaration and is used to tell browser which standard should be used to parse the page.

Currently, this isn't so important (but still good to have), because most browsers and websites support and use HTML5. But in the past, there were many versions of HTML: HTML4, XHTML1, also with various variants, so it was important to tell browser how the page is written.

Additionally, it can be used with SVG images and MathML to tell browser how to parse it.

 

Not to mention that HTML4 and XHTML1 were more strict standards than what browsers would accept before, breaking some functionality that used to work (or work most of the time). Having a valid doctype (HTML4 or XHTML in different flavours, or HTML5 which is way simpler) tells the browser to follow those standards. This is called "standards mode".
If you omit the doctype, or if you provide an invalid doctype or one declaring HTML 3.2, the browser would (and probably will still) render your webpage using "quirks mode". In this mode, the browser renders the webpage close to how older browsers would render it. There are many tiny differences, causing a layout that worked in one browser to sometimes break in another browser that did follow the standard. Therefore, the reasoning was, to use the doctype to differentiate between those modes: if you know about the standard, you declare a doctype, and the browser will use standards mode to render your webpage. If your webpage is built for older browsers, it won't have a doctype, and the browser will do its best not to break it by following the old layout rules in quirks mode.

Now, Calin, this shift towards standards mode happened before you were born, so it's ancient history in web terms. Just be glad you don't have to use this monstrosity anymore: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 
 

Ok, but itsn't:

<! any text>

just a single line comment?

 

Comments are <!-- any text -->. But most browsers will also treat <! any text> as comment, although it isn't official syntax. From HTML5 standard:

Comments must have the following format:

  • The string <!--
  • Optionally, text, with the additional restriction that the text must not start with the string >, nor start with the string ->, nor contain the strings <!--, -->, or --!>, nor end with the string <!-.
  • The string -->

However, when there is a tag which qualifies a few conditions, browser will treat it as doctype declaration. Also from HTML5 standard:

A DOCTYPE must consist of the following components, in this order:

  • A string that is an ASCII case-insensitive match for the string <!DOCTYPE.
  • One or more space characters.
  • A string that is an ASCII case-insensitive match for the string html.
  • Optionally, a DOCTYPE legacy string.
  • Zero or more space characters.
  • A U+003E GREATER-THAN SIGN character (>).

Thanks, very simple, and informative, thanks for the help understand, so a summary would be, and IF I understood correctly;

<!My text>

is as much of a comment as

<!DOCTYPE html>

is doctype declaration?

Classic DEV Post from Aug 6 '19

What to put in your portfolio as a beginner web dev

PDS OWNER CALIN (Calin Baenen) profile image
I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.