Forum Moderators: open

Message Too Old, No Replies

DOCTYPE declaration

is this really necessary?

         

SkinnyJoe

5:52 am on Apr 12, 2003 (gmt 0)

10+ Year Member



How important is it to declare a DOCTYPE?

And how do you choose the best one to use?

I wanted to validate my HTML before submitting my site to DMOZ directory, but the validator (http://validator.w3.org/) says I need a DOCTYPE statement!

Is it true that using the wrong DOCTYPE declaration can cause problems? And if so, is it better not to use one at all?

Thanks,
SkinnyJoe

grahamstewart

6:09 am on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its not absolutely neccessary, but it is definitely recommended.

What version of HTML have you written your site in?

If you have used deprecated styling tags (such as <font> and <center>) then you should probably use the transitional doctype for html4 which is..

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

but if you've written clean html and done all your styling with CSS then you'll probably want the strict html4 doctype which is

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

It goes on the very first line of your document (above the <html> tag) and it should all appear on one line.

Is it true that using the wrong DOCTYPE declaration can cause problems? And if so, is it better not to use one at all?

Using a doctype means that browsers know you have written a standards compliant page. As a result your page is more likely to look the same on a selection of different browsers.

If adding a doctype changes the look of your page, then you have been relying on the 'quirks' of that browser.

martinibuster

6:15 am on Apr 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Using a doctype means that browsers know you have written a standards compliant page.

A doctype doesn't mean that the page is standards compliant, it only indicates which standard you are trying to comply with.

Most importantly, a w3c compliant page will be compatible with future browsers because every new version is stricter and stricter.

By declaring your doctype AND validating, you are not only ensuring that your web site is forward compatible, but that it will perform the same regardless of OS (linux, mac, windows, etc).

[edited by: martinibuster at 6:20 am (utc) on April 12, 2003]

g1smd

6:19 am on Apr 12, 2003 (gmt 0)

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



Most of the time you'll probably get away with using HTML 4.01 Transitional. You can either place the !DOCTYPE declaration as the very first line of the file, or you can validate the file at [validator.w3.org...] and use the selector box to temporarily set the !DOCTYPE and Character Encoding while you validate the pages.

To satisfy the validator, browsers, and spiders, my minimum code on each page begins:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> Your Title Here </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="EN-GB">
<meta name="Keywords" content=" your, keyword, list, here ">
<meta name="Description" content=" Your Description Here. ">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
</head>

Of the en-gb part, the first two letters come from the code list in ISO 639 and the last two letters come from the code list in ISO 3166.

See also ISO 4217 for codes for representing currency, and then ISO 8601 for formats for date and time.

grahamstewart

6:23 am on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A doctype doesn't mean that the page is standards compliant, it only indicates which standard you are trying to comply with.

Correct. Apologies, I should have been more precise.

A doctype tells the browser which version of html you have attempted to use. This means the browser can make better decisions about what you were attempting to express with your markup.

grahamstewart

6:25 am on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<meta http-equiv="Content-Language" content="EN-GB">

Alternatively you can use <html lang="en-gb">

grahamstewart

6:28 am on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

Oh and you should use the FULL doctype, not this one.

The full version includes the address of the DTD (see my first reply). Using the full version puts the browser into standards mode, using the half version keeps the browser in 'quirks' mode.

g1smd

6:32 am on Apr 12, 2003 (gmt 0)

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



>> Alternatively you can use <html lang="en-gb"> <<

You can, but I read a recent post that said that less things support this particular alternative at the moment.

grahamstewart

6:41 am on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm..

Content-Language is an HTTP/1.1 feature [w3.org] so older browsers won't support it.

The w3c language task groups says..

Language codes can be (and should be ) used to indicate the language of text in HTML and XML documents. For HTML 4 , language codes are specified with the lang attribute. For XML , language codes are given in the xml:lang attribute. In both cases, language information is inherited along the document hierarchy, i.e.it has to be given only once if the whole document is in one language, and language information nests, i.e. inner attributes overwrite outer attributes.

-from [w3.org...]

Maybe doing both is the way to go?

g1smd

7:07 am on Apr 12, 2003 (gmt 0)

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



More recent threads on Content Language:
[webmasterworld.com...]
[webmasterworld.com...]

More recent threads on !DOCTYPE:
[webmasterworld.com...]

SkinnyJoe

4:43 am on Apr 13, 2003 (gmt 0)

10+ Year Member



Thanks all.

If I go with 4.01 Transitional (or 4.01 Strict), then what happens if someone views my site with an older browser? For example, a 3.01 or 2.0?

g1smd

6:18 am on Apr 13, 2003 (gmt 0)

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



Tags not understood by older browsers are usually ignored, so they will get quite a plain layout if you are doing your styles as CSS rather than the older <font> etc tags. There shouldn't be major problems most of the time, unless your page relies a lot on <div> and <span> and positioning.

I have found that modern javascript can lock up older browsers like NetScape Gold. Yeah I do know someone that was still using that up until last week.. they now have Mozilla 1.2.1 and the web is now a whole new experience for them!

.

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

>> Oh and you should use the FULL doctype, not this one. <<

>> The full version includes the address of the DTD (see my first reply). Using the full version puts the browser into standards mode, using the half version keeps the browser in 'quirks' mode. <<

Using the full version can sometimes cause problems with CSS not working as expected. There are many previous threads with examples of this.

If you write non-standard code, using tag soup, or street HTML, then you are taking a chance as to how that renders in different browsers, or whether it works at all. We all agree that you should avoid doing that.

I prefer to write compliant code that validates and will probably work in all browsers, but I use the short !DOCTYPE and leave the browser in quirks mode. By using the full !DOCTYPE unexpected problems can occur as most browsers are not fully standards compliant yet.

[edited by: g1smd at 6:48 am (utc) on April 13, 2003]

grahamstewart

6:39 am on Apr 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the full version can sometimes cause problems with CSS not working as expected.

On the contrary, using the full doctype causes the CSS to work as documented. With the short doctype the browser renders it in quirks mode and emulates all of its historical bugs and non-compliances (for example, the Microsoft IE box model bug)

Whereas the full doctype means that the page will render in a similar manner in most browsers.

g1smd

6:51 am on Apr 13, 2003 (gmt 0)

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



>> On the contrary, using the full doctype causes the CSS to work as documented. <<

Are you talking about newer browsers or old browsers?

insin

10:16 am on Apr 14, 2003 (gmt 0)

10+ Year Member



To see the effect of the doctype in action, have a look at this page in IE6:

[dti.barrysworld.net...]

The style for the main div is as follows:

.testDiv {
width: 400px;
padding-left: 100px;
border: 1px solid #000;
}

...so this div should be 400px(width) + 100px(padding) + 2px(border) = 502px wide.

The first page uses the partial doctype and is rendered in "quirks mode", which seems to use the same box model as IE5 i.e. rendered incorrectly, as the div is shown as 400px wide. For more information on this broken box model and a workaround for IE5, see:
[tantek.com...]

The second page uses the full doctype and is rendered in "standards mode" i.e. correctly.

If you want more information about the box model:
[w3.org...]

grahamstewart

10:42 am on Apr 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you talking about newer browsers or old browsers?

Well old browsers have the old bugs in them, regardless of which doctype you use! But using the full doctype means that new browsers will not attempt to emulate these old bugs - which gives you a more consistent display across the various browsers available.