What DOCTYPE is recommended for my HTML output for

2020-06-18 03:42发布

问题:

I noticed that Visual Studio defaults the DOCTYPE to XHTML 1.0 Transitional. This seems okay, but I think that's more of a standard for "generation 6" browsers. We're now in gen 7 and 8 browsers, and I'm wondering what DOCTYPE I should be putting in my HTML.

On a related note: Is there a way to add other DOCTYPEs to the HTML validation in Visual Studio 2008? Tools > Options > Text Editor > HTML > Validation

回答1:

<!doctype html>

is the way to go. This works fine in all decent browsers, including IE6 (not that it is a decent one though). Also see http://hsivonen.iki.fi/doctype/ for more background info and a browser behaviour table.

You could also consider XHTML strict, but why would you massage clean HTML into a XML format? It is only interesting if you want to parse/generate/validate HTML using some XML tool, which often isn't the case in real world. Google also just uses <!doctype html> and Stackoverflow uses nicely HTML strict.



回答2:

Note to anyone else reading this thread looking for answers: I've just discovered that the declaration for HTML 5 is simply <!DOCTYPE HTML> nothing fancy there, really.

Also, to add HTML5 validation to Visual Studio 2008 I found this article. It's working pretty well so far.

Other things to note: Visual Studio adds xmlns="http://www.w3.org/1999/xhtml" to your <html> tag, and you probably don't want/need that in there if you're going for HTML5.

Thanks guys for pointing me in the right direction.



回答3:

First of all you probably want to avoid the Transitional Doctype for new content. Transitional is intended for legacy content that needs to be thoroughly altered before confirming to the strict DTDs but this isn't an issue for new documents.

Furthermore, at least in my experience XHTML generates more trouble than it's beneficial. Unless you require XHTML for some things (such as allowing XML parsers to read your site [but even then chances are that it doesn't validate and therefore is unsuitable for that]) I'd recommend sticking to HTML 4 Strict. Also XHTML needs special attention with IE, even in IE 8.



回答4:

Personally I'd go for either XHTML 1.0 Strict or HTML 4.01 Strict.

Unless you're literally "transitioning" from using older versions of HTML, it doesn't make much sense to use the transitional doctype.

XHTML 1.1 is also an option however you'll need to ensure you're serving your document with a application/xhtml+xml MIME type.

HTML 5 is still very new but could be an option if you're putting out something cutting edge that you only expect to work in the most up-to-date browsers.

The schemas for Visual Studio are usually kept in:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\schemas\html

I beleive you can add new ones in here as needed. I'll post more details on this as I find it.



回答5:

You should be using a Strict doctype. Whether that's HTML 4.01 Strict or XHTML 1.0 Strict is up to you. Lately I've personally been using the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">

I really like the way RDFa works. It bolts onto HTML more cleanly than microformats by defining its own attributes rather than overloading the class and title attributes. But because RDFa is still not really consumable like microformats are, I'm using both alongside each other.