Sheath rule
best-practices-require-meta-charset
HTML documents should have a
<meta charset> tag.
- Package
- Core
- Category
- Best Practices
- Default severity
- warning by default
- Auto-fix
- Manual fix
#Why
Without a declared charset, the browser must infer the document encoding. An incorrect encoding can corrupt text and change how the document is parsed.
The declaration must appear within the first 1,024 bytes of the document. Place
it at the top of <head> before other content.
#Examples
#Bad
<!-- Missing charset -->
<head>
<title>My Page</title>
</head>
<!-- Only legacy declaration (less clear) -->
<head>
<meta http-equiv="Content-Type" content="text/html">
</head>
<!-- HTML documents must declare UTF-8 -->
<head>
<meta charset="iso-8859-1">
</head>
#Good
<!-- Modern charset declaration (recommended) -->
<head>
<meta charset="utf-8">
<title>My Page</title>
</head>
<!-- Legacy charset declaration (still valid) -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My Page</title>
</head>
#Best Practices
- Use
<meta charset="utf-8">- it's shorter and clearer - Place it as the first element in
<head> - UTF-8 is the recommended encoding for the web
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"> <!-- First in head -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Page</title>
</head>
#Notes
- A charset declaration inside Blade control flow satisfies the rule only when every render path supplies one within the first 1024 bytes.
- Only documents with a
<head>element are checked - Either
<meta charset>or<meta http-equiv="Content-Type">with charset satisfies the requirement - A modern declaration counts only when its value is an ASCII
case-insensitive match for
utf-8. - A legacy declaration counts only when its complete
contentvalue matchestext/html;, optional HTML ASCII whitespace, andcharset=utf-8. - Dynamic charset declarations are left for runtime rather than treated as missing or duplicate static declarations.
- A missing charset is not reported when
<head>contains dynamic content such as<x-seo>,<s:se_meta>,@include,@stack,@yield, or{!! ... !!}
#References
- HTML Spec: Charset - Character encoding declaration
- MDN: meta charset - Documentation on the charset attribute
#Related Rules
- best-practices-require-doctype - DOCTYPE declaration
- best-practices-no-duplicate-in-head - No duplicate charset