1

My question is simple and that is "is it compulsory to write <!DOCTYPE html> on every html document or optional?".

I know that some very simple html documents show correct result without doctype declaration. But I want to know is there a situation where it is necessary to declare it.

Tayyab Yasin
  • 37
  • 1
  • 10
  • yes, you don't need to declare anything more, only doctype html for the browsers know about yout html file – Matheus Barem Oct 18 '18 at 17:26
  • then why doctype declaration is part of HTML5. what is the purpose of using it? – Tayyab Yasin Oct 18 '18 at 17:33
  • The declaration is an instruction to the web browser about what version of HTML the page is written in. And is not a tag – Matheus Barem Oct 18 '18 at 17:40
  • @Matheus - No it isn't. The declaration is just a mode switch between quirks, almost standards, and standard modes. Browsers do not care about the version of HTML on the page, just like they don't care about the version of JavaScript or the version of CSS in their respective documents. – Alohci Oct 19 '18 at 00:03
  • Can u elaborate why we may wish to switch between standard and non-standard mode. If we write html according to standards then we should write doctype declaration otherwise not. Am I right? @Alohci – Tayyab Yasin Oct 19 '18 at 05:58
  • 1
    Quirks mode is a large set of behavioural deviations from the standard designed to allow browsers to process old web pages similar to how they would have been handled at the time they were written and tested. i.e. the deviations replicate the bugs of old browsers. It's best to avoid exploiting those bugs in new pages, so you always want the doctype in place. This is true even if you are not concerned about writing HTML according to the standards. – Alohci Oct 19 '18 at 06:16
  • In addition to what @Alohci said: when using Quirks Mode, different browsers have different quirks! So if you care about cross-browser-compatibility, avoid it. – Mr Lister Oct 19 '18 at 09:43
  • Possible duplicate of [What is the functionality of !DOCTYPE?](https://stackoverflow.com/q/1818587/1016716) (Note, not dupehammering, because there are many more duplicates to choose from and I'm not sure this is the best.) – Mr Lister Oct 19 '18 at 09:49
  • @Alohci https://www.w3schools.com/tags/tag_doctype.asp – Matheus Barem Oct 19 '18 at 12:40
  • @Matheus - Indeed. That's why you should be suspicious of everything you read on w3schools. It is notorious for poor quality information. Although it's not as bad as it used to be, it still has plenty of basic mistakes like that. You should seek out better sources, for example [MDN](https://developer.mozilla.org/en-US/docs/Glossary/Doctype) – Alohci Oct 19 '18 at 14:08

0 Answers0