@hint/hint-meta-charset-utf-8
hint for best practices related to the meta charset
Last updated 5 years ago by molant .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @hint/hint-meta-charset-utf-8 
SYNC missed versions from official npm registry.

Use charset utf-8 (meta-charset-utf-8)

meta-charset-utf-8 checks if the page explicitly declares the character encoding as utf-8 using a meta tag early in the document.

Why is this important?

The character encoding should be specified for every HTML page, either by using the charset parameter on the Content-Type HTTP response header (e.g.: Content-Type: text/html; charset=utf-8) and/or using the charset meta tag in the file.

Sending the Content-Type HTTP header is in general ok, but it’s usually a good idea to also add the charset meta tag because:

  • Server configurations might change (or servers might not send the charset parameter on the Content-Type HTTP response header).
  • The page might be saved locally, in which case the HTTP header will not be present when viewing the page.

One should always choose utf-8 as the encoding and convert any content in legacy encodings to utf-8.

As for the charset meta tag, always use <meta charset="utf-8"> as:

  • It's backwards compatible and works in all known browsers, so it should always be used over the old <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">.

  • The charset value should be utf-8, not any other values such as utf8. Using utf8, for example, is a common mistake, and even though it is valid nowadays as the specifications and browsers now alias utf8 to utf-8, that wasn’t the case in the past, so things might break in some older browsers. The same may be true for other agents (non-browsers) that may scan/get the content and may not have the alias.

  • It needs to be inside the <head> element and within the first 1024 bytes of the HTML, as some browsers only look at those bytes before choosing an encoding.

    Moreover, it is recommended that the meta tag be the first thing in the <head>. This ensures it is before any content that could be controlled by an attacker, such as a <title> element, thus avoiding potential encoding-related security issues (such as the one in old IE).

What does the hint check?

The hint checks if <meta charset="utf-8"> is specified as the first thing in the <head>.

Examples that trigger the hint

The character encoding is not specified in <html>:

<!doctype html>
<html lang="en">
    <head>
        <title>example</title>
        ...
    </head>
    <body>...</body>
</html>

The character encoding is specified using the meta http-equiv:

<!doctype html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>example</title>
        ...
    </head>
    <body>...</body>
</html>

The charset value is not utf-8:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf8">
        <title>example</title>
        ...
    </head>
    <body>...</body>
</html>

The meta charset is not the first thing in <head>:

<!doctype html>
<html lang="en">
    <head>
        <title>example</title>
        <meta charset="utf8">
        ...
    </head>
    <body>...</body>
</html>

Examples that pass the hint

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>example</title>
        ...
    </head>
    <body>...</body>
</html>

How to use this hint?

This package is installed automatically by webhint:

npm install hint --save-dev

To use it, activate it via the .hintrc configuration file:

{
    "connector": {...},
    "formatters": [...],
    "hints": {
        "meta-charset-utf-8": "error"
    },
    "parsers": [...],
    ...
}

Note: The recommended way of running webhint is as a devDependency of your project.

Further Reading

Current Tags

  • 4.0.23                                ...           latest (3 months ago)

54 Versions

  • 4.0.23                                ...           3 months ago
  • 4.0.22                                ...           a year ago
  • 4.0.21                                ...           a year ago
  • 4.0.20                                ...           2 years ago
  • 4.0.19                                ...           2 years ago
  • 4.0.18                                ...           2 years ago
  • 4.0.17                                ...           2 years ago
  • 4.0.16                                ...           2 years ago
  • 4.0.15                                ...           2 years ago
  • 4.0.14                                ...           2 years ago
  • 4.0.13                                ...           3 years ago
  • 4.0.12                                ...           3 years ago
  • 4.0.11                                ...           3 years ago
  • 4.0.10                                ...           3 years ago
  • 4.0.9                                ...           3 years ago
  • 4.0.8                                ...           3 years ago
  • 4.0.7                                ...           3 years ago
  • 4.0.6                                ...           3 years ago
  • 4.0.5                                ...           4 years ago
  • 4.0.4                                ...           4 years ago
  • 4.0.3                                ...           4 years ago
  • 4.0.2                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 3.3.3                                ...           5 years ago
  • 3.3.2                                ...           5 years ago
  • 3.3.1                                ...           5 years ago
  • 3.3.0                                ...           5 years ago
  • 3.2.11                                ...           5 years ago
  • 3.2.10                                ...           5 years ago
  • 3.2.9                                ...           5 years ago
  • 3.2.8                                ...           5 years ago
  • 3.2.7                                ...           5 years ago
  • 3.2.6                                ...           5 years ago
  • 3.2.5                                ...           5 years ago
  • 3.2.4                                ...           5 years ago
  • 3.2.3                                ...           5 years ago
  • 3.2.2                                ...           5 years ago
  • 3.2.1                                ...           5 years ago
  • 3.2.0                                ...           5 years ago
  • 3.1.2                                ...           5 years ago
  • 3.1.1                                ...           6 years ago
  • 3.1.0                                ...           6 years ago
  • 3.0.1                                ...           6 years ago
  • 3.0.0                                ...           6 years ago
  • 2.1.1                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.4                                ...           6 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 1.0.0-beta.0                                ...           6 years ago
Maintainers (2)
Downloads
Total 8
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 5
Dependencies (1)
Dev Dependencies (14)

© 2010 - cnpmjs.org x YWFE | Home | YWFE