Media type / text
text/html
HTML documents. The browser parses the markup, runs any script it contains, and treats the response as same-origin with whatever served it.
Served inline: Rendered inline, this type executes script in the origin that served it. Never serve untrusted content under it from an origin that holds sessions — use a separate origin, or force a download with Content-Disposition: attachment.
Browser behaviour
Executes in the page
Charset
charset required
Compression
Compress in transit
Send it like this
Content-Type: text/html; charset=utf-8Send `; charset=utf-8`. Without it the receiver falls back to its own default — for some text types that default is us-ascii, and any non-ASCII character then renders wrong.
Handling verdict
Rendered inline, this type executes script in the origin that served it. Never serve untrusted content under it from an origin that holds sessions — use a separate origin, or force a download with Content-Disposition: attachment.
Send `; charset=utf-8`. Without it the receiver falls back to its own default — for some text types that default is us-ascii, and any non-ASCII character then renders wrong.
The payload is text-like or otherwise repetitive, so gzip or Brotli removes real bytes. Enable it at the server or CDN.
Registered with IANA through a public review process, so the name is stable and every implementation can rely on it meaning the same thing.
Anatomy of the name
RFC 6838Top-level type
text
Text
Subtype
html
Registered in the standards tree.
Structured syntax
none
No suffix, so the payload format is defined entirely by the subtype itself.
Parameters
charset
Beyond the charset rule above, this type defines no parameters of its own.
What trips people up
2 notes- Never serve user-uploaded content as text/html from your application origin — it is a stored XSS with extra steps.
- The charset parameter beats an in-document <meta charset>, so a mismatched header silently wins.
Response headers
Content-Type: text/html; charset=utf-8
X-Content-Type-Options: nosniff
Content-Disposition: inline
Content-Security-Policy: sandbox; default-src 'none'
Vary: Accept-Encodingnosniff stops the browser second-guessing the type you declared, which is what makes the rest of this reliable. The sandbox directive is the belt-and-braces option when the content is not fully under your control.
Server configuration
extension mappingnginx
types {
text/html html htm shtml;
}Apache
AddType text/html .html .htm .shtml
AddCharset UTF-8 .html .htm .shtmlCaddy
@type path *.html *.htm *.shtml
header @type Content-Type "text/html; charset=utf-8"