Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an XML declaration necessary in an HTML document?

Tags:

html

xml

doctype

At some point in the dim and murky past, I did a lot of reading and a lot of research and decided that the "best" way to begin an HTML document was:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">

And ever since then I have copy-and-pasted this into the beginning of every HTML document without giving it any further thought.

However, HTML5 is here now and I need to begin following modern practices, which means that I need to replace this outdated header with something new.

Obviously, the second line will become:

<!DOCTYPE html>

But what about the first line? The "<?xml" declaration; do I still need it? Honestly, from what I've been reading today, I'm not sure I've ever needed it. (I'm not sure what research I did back then that led me to think that I did.)

And the third line? I know I need "<html>" but do I need anything else that's on that line?

Also, is there anything that I need to add? Can I simply begin by documents with:

<!DOCTYPE html>
<html>

Is this bare-bones couple of lines the new best practice? Or should I be adding anything else?

like image 957
Ravenswd Avatar asked Apr 08 '26 14:04

Ravenswd


2 Answers

The XML declaration only has meaning in XML documents. This includes XHTML, which in HTML5 is little more than an XML serialization of the HTML standard.

But HTML itself is not XML. So the XML declaration is meaningless in an HTML document and there is no reason whatsoever to include it. It's not even a matter of best practice.

HTML5 permits the use of certain holdovers from XHTML such as the xmlns attribute and the self-closing syntax /> to make it easier to port "XHTML" documents to HTML5. But if you're starting fresh, and you are writing HTML (which is the case for maybe 90% of authors), they should be left out. Only when you're authoring XHTML do you need to continue following the XML rules.

The two-liner you present is the correct way to begin an HTML document that conforms to modern standards. (The <html> start tag is optional, but unless you're Google it's in your best interest to leave that in, and you need it anyway if you intend to specify any attributes at all.)

like image 93
BoltClock Avatar answered Apr 10 '26 02:04

BoltClock


try looking here

The XML declaration is only required if the page is not being served as UTF-8 (or UTF-16), but it can be useful to include it so that developers, testers, or translation production managers can visually check the encoding of a document by looking at the source. Since a polyglot document must be in UTF-8, you don't need to, and indeed must not, use the XML declaration. On the other hand, if the file is to be read as HTML you will need to declare the encoding using a meta element, the byte-order mark or the HTTP header.

Since a declaration in a meta element will only be recognized by an HTML parser, if you use the approach with the content attribute its value should start with text/html;.

like image 39
Angel ofDemons Avatar answered Apr 10 '26 03:04

Angel ofDemons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!