5

I've following HTML5 document :

<!DOCTYPE html>
<html>
    <head> </head>
    <body>
        <p>Beträge: 20€</p>
    </body>
</html>

The output of above cod is as below :

Beträge: 20€

The I tried below HTML5 code :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <p>Beträge: 20€</p>
    </body>
</html>

The above code gave me the following output as I was expecting :

Beträge: 20€

As per my knowledge, the default character encoding for HTML5 is UTF-8. It's default means it should not be specified explicitly inside <meta> tag.

So, in my first code snippet I skipped the code <meta charset="UTF-8"> But I got some weird unexpected result.

Then, I tried by adding the code <meta charset="UTF-8"> in between <head> pair of tags and it worked perfectly fine and I got the expected result.

So, my question is since the default character encoding in HTML5 has been set to UTF-8 why it's not working if it's not been specified explicitly?

Why there is a need to specify the character encoding "UTF-8" in an HTML5 document?

2
  • 1
  • @ricky3350 : The linked question does not answer my question satisfactorily. According to the answer of the question whose link you provided it's saying that the character encoding has to be specified in an HTML5 web-page some or the other way. Then, why they say UTF-8 is the default character encoding in HTML5. As per my understanding, the things which are default don't need to be specified explicit, rather they are considered to be readily available. Then why this is not the case with character encoding in HTML5 document? So, please remove the Duplicate mark on my question.Thanks
    – user10318083
    Sep 16, 2018 at 6:14

2 Answers 2

6

This answer relied on some now obsolete documentation, see jon1000's answer for update. (thanks @blazee for pointing this out in the comments) I'll leave this answer here, because the part about how the string "Beträge: 20€" is mutating in various encodings still seems accurate.


The HTTP1.1 specifies that the browsers should treat all text as ISO-8859-1, unless told otherwise (this referenced RFC-2616, but it was superseded later, see @jon1000's answer):

When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1"

At the same time, HTML5 specifies that

If the transport layer specifies an encoding, and it is supported, return that encoding with the confidence certain, and abort these steps.

So, HTTP1.1 defaults to ISO-8859-1, and overrides everything else.

If you encode

Beträge: 20€

with UTF-8, and then decode it as ISO-8859-1, you get exactly the garbled output:

Beträge: 20â¬

as the following code snippet demonstrates (Java, doesn't really matter):

new String("Beträge: 20€".getBytes("utf-8"), "iso-8859-1")
// result: Beträge: 20â¬

The browser actually does warn you about it. E.g. Firefox displays the following warning in the console:

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

To obtain the correct output, you have to manually override the ISO-8859-1 by UTF-8 (in case of Firefox, it's under View -> Text Encoding -> Unicode (instead of "Western")).


So, to conclude: I don't see where it even says that "the default character encoding for HTML5 is UTF-8". All it says seems to be:

Authors are encouraged to use UTF-8. Conformance checkers may advise authors against using legacy encodings.

1
  • 1
    This answer is wrong in this part: "So, HTTP1.1 defaults to ISO-8859-1, and overrides everything else." The algorithm is more complex and depends on locale, as provided in @jon1000's answer.
    – blazee
    Feb 15 at 15:47
5

Because the statement "the default character encoding for HTML5 is UTF-8" is wrong. The statement is distributed by websites like this. But as Marcel Dopita writes at Don’t be fooled by w3schools, UTF-8 is not the default HTML5 charset, it is wrong and in fact the W3C recommendation has a "suggested default encoding" of Windows-1252 for English locales.

It is sometimes stated that "HTTP/1.1 defaults to ISO-8859-1". This was true in the 1999 standard (RFC 2616), but in the 2014 version (RFCs 7230-7329) the default charset has been removed, and so the default behaviour is now just specified by the HTML5 recommendation. Also, even if the transport layer does specify "iso-8859-1", it is not a supported encoding in HTML5 and the encoding specification says it should be treated as a label for Windows-1252.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.