One way to specify the encoding of an HTML document is by sending the appropriate headers. However, a fallback approach is to declare the encoding inline via a meta tag. For example:
<!DOCTYPE html>
<html>
<head>
<title>Foo bar</title>
<meta charset="utf-8" />
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
But to read the document and determine the encoding, must one not already know the encoding?
As long as no non-ASCII characters appear before that
<meta>tag, the browser can assume that it’s ASCII or UTF8, and it will read correctly until that point.This is why that
<meta>tag should be before the<title>.If it’s UTF16, the browser can figure that out by trying to read characters like
<.