Forum Moderators: open

Message Too Old, No Replies

Doc types

Does it have to be declared on Web pages?

         

jayell

6:28 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



Can someone explain the purpose of the doctype. Is it just for validation? And what happens if it isn't on your pages. I had a problem that was solved by removing it from the pages. Is that OK?

Thanks
Jayell

StupidScript

6:42 pm on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The DOCTYPE declaration tells the browser which set of standards you want your code to be interpreted as. It points to a globally-available resource that the browser can import to help figure out what to do with what it is reading. i.e. What is "<p>" supposed to look like when the page is rendered?

Modern browsers make assumptions about your code, figuring it to be one of the "usual" standard types, like "HTML 4.0 Transitional" or "HTML 3.2 Final".

If you use a "document type definition" that is different from the "usual" bunch, then a DTD is essential to the proper rendering of your pages. i.e. what XML schema you are using, or where to find your customized DTD (Yes, you can write your own!).

You'll notice that attempting validate a page without a DTD results in the validator making an assumption about which DTD to use in order to attempt its validation.

In fact, the only code required for a "proper" HTML page is the <title> tag. But that's certainly a far cry from a "well-formed" HTML document, which should include at a minimum all of the "proper" tags, like the DTD, <html>, <head>, and <body> tags.

problem that was solved by removing it from the pages

The problem wasn't solved, you just made it harder to find by removing the DTD. It seems like the DTD you were using did not include instructions for how to handle some of the code you were using.

An illustration of the use of a modern XHTML DTD can be found here [validator.w3.org], at the World Wide Web Consortium [w3.org], which is the standards organization responsible for maintaining all of the "standard" DTDs.

encyclo

6:51 pm on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jayell, you should read up about Quirks mode versus Standards mode [webmasterworld.com] which will explain why your design changed when you removed the doctype. The basic situation is that the doctype is read by modern browsers to decide whether the page is modern and respects the standards, or whether it is a legacy page which will need to be parsed to take into account the display bugs which existed in older browsers.

When building new pages, it is best practice to use a full doctype in order to trigger the standards compliance mode, and validate your pages to avoid errors. This will help you create pages which are more compatible with newer technlogy and which display according to the declared standards.

jayell

11:32 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



Looks like I have LOTS of reading to do .. thanks for the input! This is something I've never known much about .. obviously.

Jayell

nerdgerl

7:13 am on Dec 22, 2004 (gmt 0)

10+ Year Member



And, no it doesn't have to be declare. But, ever since I started using DTDs and Character Encodings, my pages have rendered properly in browsers they didn't used to.

g1smd

6:54 pm on Dec 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you are unsure which DOCTYPE to use, then you can get away with a minimum of:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Next, run your pages through [validator.w3.org...] and fix all the errors found.

yngwin

4:02 pm on Dec 30, 2004 (gmt 0)

10+ Year Member



That is NOT a valid DTD. Choose one from this list: [w3.org...]

pageoneresults

4:06 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

A shortened DOCTYPE such as that posted by g1smd has been used by quite a few over the years. If I recall, there were some bugs in various browsers when using a full DOCTYPE.

The suggestion to use a shortened DOCTYPE is deprecated in favor of using a full version which I've posted an example of above.

Shortened DOCTYPEs force IE into Quirks Mode. And, if you view the documentation for Mozilla, they have what is called Almost Standards Mode [mozilla.org] which occurs when using a shortened DOCTYPE.

yngwin

6:36 pm on Dec 30, 2004 (gmt 0)

10+ Year Member



All the more reason not to use shortened doctype declarations. We want standards!

encyclo

7:01 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hesitate to correct pageoneresults's comments, but just to clarify about the different Mozilla/Firefox rendering modes.

The "half doctype"

Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

This will trigger Quirks Mode. This doctype should only be used for editing or repairing legacy documents, as it allows you to validate more easily whilst not triggering standards mode.

Full doctypes (HTML 4.01 and XHTML 1.0 Transitional)

Examples:

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

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

These will trigger Almost Standards Mode. The difference between this and full standards mode is very small, and to do with the handling of certain styles with

<table>
s. In most cases, you will see no difference between this mode and the full standards mode.

Full doctypes (HTML 4.01 Strict and XHTML 1.0 Strict)

Examples:

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

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

These will trigger the full Standards Mode.

In short, if you are building a page using tables for layout (not for tabular data), then you may prefer one of the full but Transitional doctypes. In other cases, you can use either Strict of Transitional as required.

IE6 and Opera both have only two modes, so the full transitional doctypes trigger standards mode in both cases.

pageoneresults

7:28 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I hesitate to correct pageoneresults's comments.

Please don't! I greatly appreciate someone keeping me in check. ;)

Great explanation. And, you did it with style.

bedlam

2:54 am on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

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

These will trigger Almost Standards Mode. The difference between this and full standards mode is very small, and to do with the handling of certain styles with <table>s. In most cases, you will see no difference between this mode and the full standards mode.

Almost... :)

Though it's true that many of the differences between xhtml 1.0 and html 4.01 transitional doctypes and standards-mode doctypes have to do with tables, it would be more accurate to say that the transitional doctypes retain many (most?) of the presentational attributes used in earlier html versions. These include attributes used in tables such as 'valign', but also include 'bgcolor' (can be used on <body> element), 'align', etc. 'target' attributes are also still acceptable on links in both doctypes.

But you're right of course; the differences between transitional/strict are pretty small...

-B

encyclo

1:23 pm on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the transitional doctypes retain many (most?) of the presentational attributes used in earlier html versions

Almost... ;) This is quite right, but that wasn't what I was referring to. The guide above is not talking about the differences between the DTDs themselves, merely the difference between the rendering modes in Mozilla/Firefox when encountering different doctypes.

As far as the browser is concerned, the actual DTD referenced in the doctype is irrelevant as the browser never reads or uses it: all browsers use their own custom DTD for rendering whatever the doctype, and the doctype switching merely affects the default display.