Why do we need the meta content type tag in HTML h

2019-02-02 10:44发布

问题:

Why do we need to include the meta content type tag in HTML head like this?

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

回答1:

Whenever the webpage is been opened locally (from disk file system), the text/html part will instruct the webbrowser of which type the document is so that it knows how to parse it and the charset=UTF-8 will instruct the webbrowser which character encoding should be used to display the characters on the webpage so that it won't use the platform default encoding (which may not necessarily be the right one which would thus potentially end up as mojibake).

Important detail is that this HTML meta header isn't been used when the page is served over the web by HTTP. Instead, the Content-Type one in the HTTP response header will be used. So if this lacks the charset=UTF-8 part while the content is actually decoded as UTF-8, then it will likely go mojibake. You can determine the Content-Type header with for example Firebug, in the Net panel.



回答2:

Meta elements are typically used to specify page description, keywords, author of the document, last modified, and other metadata.

The meta tag you have posted, will instruct the browser to have text/html type of document with character set or language set to UTF-8.

Used to declare the character set.

The Meta Content Type tag is used to declare the character set of a website. It has become recommended to always use the Meta Content Type tag even if you use a DTD declaration above the Header. If you fail to do so, it may cause display problems. For instance, the document may use UTF-8 punctuation characters but is displayed in ISO or ASCII character sets. There are other benefits to using the Meta Content Type tag. Simply subscribe to our SEO Revolution Newsletter (nominal fee membership) to get the entire scoop of what the Meta Content Type tag can do for your site

See this for more info:

  • Meta Tags Explained


回答3:

So that the browser would know how to decode the page - for example: depending on the language, the end-result may differ a lot.