Use charset `utf-8` (2024)

meta-charset-utf-8 checks if the page explicitly declares thecharacter 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, eitherby using the charset parameter on the Content-Type HTTP responseheader (e.g.: Content-Type: text/html; charset=utf-8) and/or usingthe charset meta tag in the file.

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

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

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

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

What does the hint check?

The hint checks if <meta charset="utf-8"> is specified as the firstthing 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 ofyour project.

Further Reading

Use charset `utf-8` (2024)
Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6129

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.