0

Possible Duplicate:
What's up, Doctype?

In my browser, I looked on the page source of my already-made html page and noticed that the

<html>

is in red font, which meant that there is an error.

I added this before it:

<!DOCTYPE html>

and it doesn't appear to be in red font now.

the problem is... The look of my page is different now from before, when I didn't have the doctype.

The question is, is it ok not to declare the doctype and still retain the style of my page?

Community
  • 1
  • 1
Sam San
  • 6,567
  • 8
  • 33
  • 51
  • It defines which version of (X)HTML your document is actually using, and this is a critical piece of information needed by some tools processing the document. See my answer for details. – Ahsan Khurshid Sep 10 '12 at 07:16
  • There is a large number of previous questions with essentially the same question, with good (and some bad) answers. Search for “doctype” to see some of them. – Jukka K. Korpela Sep 10 '12 at 12:12

5 Answers5

3

Don't write valid HTML in order to remove validation errors. Write valid HTML in order to render it consistently to all clients.

You cannot retain the style of your page, as you say, without specifying a doctype instructing user agents how to interpret the following code.

Having said that, the markup you have written may be more in line with another doctype than the one you've quoted above.

See "Choosing a Doctype"

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • 2
    w3schools reference are not recommended, See [w3fools.com](http://w3fools.com) – Ahsan Khurshid Sep 10 '12 at 07:05
  • -1 change the w3schools links to something else, and I'll give you back your upvote. – Lucas Sep 10 '12 at 07:06
  • 1
    You're right, so I won't downvote you, but your commentary is a little odd. In most cases, a valid document is also one that displays consistently. The two are not mutually exclusive. Also, why bother encouraging the use of anything other than the HTML5 doctype? – Jezen Thomas Sep 10 '12 at 07:09
  • gave your +1 back, as guaranteed. – Lucas Sep 10 '12 at 07:13
  • @JezenThomas: That would be begging the question, when a doctype is required for a valid document. My point was to not treat valid markup as an end in itself, but as a means to an end (that of consistent rendering). I realize they're pretty interchangeable, but with the exception, I am inclined to believe, that the latter attitude would not have motivated a question as the current. – David Hedlund Sep 10 '12 at 07:18
  • As for why anything other than HTML5, well, we know from the outset that the markup isn't written in HTML5 (because including the HTML5 doctype changes the rendering). I could've answered, keep the doctype, change the markup to valid HTML5, sure, but I'm not sure that would've been as instructive given the question at hand. I went with this answer instead. – David Hedlund Sep 10 '12 at 07:21
1

In some (and quite a few actually) browsers such as IE, the rendering will be drastically different (note my use of drastically) if you don't put a doctype (Quirks Mode - Wikipedia).

Under all circumstances, the best thing to do is add one every time. Get something like TextExpander to make it easier to type in.

Just like remembering to brush your teeth every morning, this is another should-be compulsory habit.

Also, consider using the HTML5 DocType (the one you used) every time.

Lucas
  • 16,930
  • 31
  • 110
  • 182
1

The standard dictates that all documents should have a Document Type Definition - doctype (Document Type) declaration. The W3 defines a list of doctypes that you can use.

As you have discovered, adding the wrong doctype has some interesting consequences. Therefore it is important to provide the right doctype based on what your document is following.

See this excellent article that goes into detail on this.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

Don't forget to add a doctype

There is not just one type of HTML, there are actually many: HTML 4.01 Strict, HTML 4.01 Transitional, XHTML 1.0 Strict, and many more. All these types of HTML are defined in their respective W3C specifications, but they are also defined in a machine-readable language specifying the legal structure, elements and attributes of a type of HTML.

Such a definition is called a "Document Type Definition", or, for short, DTD.

Why specify a doctype?

Because it defines which version of (X)HTML your document is actually using, and this is a critical piece of information needed by some tools processing the document.

For example, specifying the doctype of your document allows you to use tools such as the Markup Validator to check the syntax of your (X)HTML. Such tools won't be able to work if they do not know what kind of document you are using.

But the most important thing is that with most families of browsers, a doctype declaration will make a lot of guessing unnecessary, and will thus trigger a "standard" rendering mode.

Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51
1

Doctype defines which version of (X)HTML your document is actually using, and this is a critical piece of information needed by some tools processing the document.

http://www.w3.org/QA/Tips/Doctype

After reading above link you can decide it it OK or NOT. :)

Sujeet
  • 1,780
  • 3
  • 16
  • 33