What is meta charset in HTML?

Can anyone explain this HTML5 code to me?

<meta charset="utf-8">

What is it for and why is this standard used?

Author: bfavaretto, 2014-04-05

3 answers

According to specification of the HTML - , element <meta>, which should be always to be the <head>, "represents various kinds of metadata that cannot be represented with all elements of in the title, base, link, style or ) script" free translation of{[the 26th]}. Examples of this metadata are content summary, keywords, indications to search robots, among others.

It can have an attribute content, and should have an attribute name, http-equiv or charset (and never more than one of these three). In case there is a charset, it is used to indicate the character encoding format used in the document.

In addition to the 128 basic ASCII characters, the same graphic symbol can be internally encoded in different ways. This applies, for example, to all accented characters of Portuguese. If an HTML file is saved with the encoding Latin 1 (ISO 8859-1, or Windows 1252, which is similar), the character ã uses only one byte to be stored. In UTF-8, the same character uses 2 bytes, with different values than the byte used in Latin 1. So if you have the browser display with one encoding and the document is being served with another, the special characters break.

It is important to remember that the element <meta> should not be the main method to be used to indicate to the browser which charset is used. The preferred method is that the server sends an HTTP header with this information. The use of <meta> is a second line of defense against problems of encoding – highly recommended, do not leave your HTML without it (see for example the case mentioned by Miguel Angelo: an HTML can be opened directly, and not sent by a server, and in this case there would be no header indicating the charset used, were it not for the presence of this element of HTML).

 13
Author: bfavaretto, 2014-04-05 23:08:42

This serves to indicate what is the encoding of the html file served.

In this case it is indicating the encoding utf-8, which is a default defined by Unicode:

Unicode UTF-8

Unicode UTF-8 is an encoding format that has a variable character size, ranging from 1 to 4 bytes. The most common characters are mapped to 1-byte codes, other less common ones, like most accented characters, have 2 bytes.

Meta Tag

The HTML meta tag allows you to indicate meta information, that is, information about the document itself.

How does mata charset work?

This tag, used with charset serves to indicate the encoding of the document itself. Note that there is a certain inconsistency in this statement, then comes the question: How could it be possible to read the encoding of the document, from within the document itself? after all, knowing the encoding of the document beforehand is what allows you to read the file.

Answer: most encodings are very similar to the main characters , so in most encodings it is possible to read parts of the file, considering the ASCII encoding. This is how the browser can read, until it finds the <meta charset="utf-8"> tag... when this it turns out, it resumes reading the file, considering the encoding it just found inside the HTML itself.

Two ways to indicate charset in the form of metadata

There are two ways to indicate the charset of the HTML document, with meta-information:

Form recommended by HTML5 standard:

<meta charset="utf-8">

Or so:

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

These two forms are equivalent. In both cases these tags should appear within the head tag of the document HTML... and the sooner, the better, because when the tag is found, the reading of the file will have to be restarted.

Reference

Why this does it exist?

Why is there such a way to indicate the encoding of the HTML document? Is there no longer, in the HTTP protocol, a way to indicate the charset using a header in the response?

This is not just for warranty. It turns out that an HTML document served through the HTTP protocol could be saved by the receiver, independently of the headers with which the file was served.

There is no restriction in HTML as to the protocol that should be used to serve the document, so the meta tag with charset, for me is almost a must: it allows the document to remain coherent, even if the protocol/storage does not support indicating the charset of the file.

 6
Author: Miguel Angelo, 2017-05-23 12:37:27

This tag meta charset= "utf-8" is used to show the browser the type of encoding that will be used on your website

 0
Author: helton, 2014-04-07 00:38:01