5

Should I declare charset like this:

<meta http-equiv="content-type" content="text/html" charset="utf-8" />

or like this:

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

Or are both valid?

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
Menel
  • 177
  • 1
  • 9

3 Answers3

4

The both declarations are valid, you could always use the short equivalent version :

<meta charset="utf-8" /> 

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
3

Because you start a document with

<!DOCTYPE html>

I doubt that it's necessary to specify the content type. Instead just specify the encoding/charset:

<head>
   <meta charset="UTF-8">
</head>
2

Updated in light of @Alohci's comment.

See W3C's documentation on <meta http-equiv="...">:

encoding declaration state (http-equiv="content-type")

The encoding declaration state is just an alternative form of setting the charset attribute: it is a character encoding declaration. This state's user agent requirements are all handled by the parsing section of the specification.

For meta elements with an http-equiv attribute in the Encoding declaration state, the content attribute must have a value that is an ASCII case-insensitive match for a string that consists of: the literal string "text/html;", optionally followed by any number of space characters, followed by the literal string "charset=", followed by one of the labels of the character encoding of the character encoding declaration.

A document must not contain both a meta element with an http-equiv attribute in the encoding declaration state and a meta element with the charset attribute present. (emphasis mine)

Therefore, if you are going to use http-equiv, it must be used as <meta http-equiv="Content-Type" content="text/html; charset=utf-8">. But, that's just another way of saying <meta charset="utf-8">, so use the abbreviated form.

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • MDN is wrong. `` is not invalid or obsolete in HTML5. See http://w3c.github.io/html/document-metadata.html#statedef-http-equiv-content-type or try it in a validator. – Alohci Jul 09 '16 at 01:28
  • @Alohci Thank you for pointing that out. I corrected my answer. – Sinan Ünür Jul 09 '16 at 11:10