23

I was wondering what's the significance of using the "lang" attribute and how that differs from using the meta "Content-Language" tag?

Consider the following code:

<html lang="en">
    <head>
        <meta http-equiv="Content-Language" content="en-US">
    </head>...

My assumption is that the browser is reading the meta tag's value, but the DOM is concerned with the "lang" attribute. Is this correct? Are there any nuances I'm unaware of?

3 Answers 3

19

The lang attribute (on the HTML element) specifies the language for the document (unless overridden with another lang attribute which can change the language for a section of the document).

The Content-Language HTTP header specifies the language of the intended audience. This is not the same as the language the document is actually written in. For example, part of a French language course could consist of a page written in French, but Content-Language would be en as it was intended for English speakers learning French.

From the spec:

The Content-Language entity-header field describes the natural language(s) of the intended audience for the enclosed entity. Note that this might not be equivalent to all the languages used within the entity-body.

Meta HTTP-equiv is the poor man's HTTP header. It has all the meaning of the real HTTP header, but less respect (and support).

As a rule of thumb, Content-Language is of more interest to search engines and the lang attribute is of more interest to screen readers.

4
  • Curiously, I just discovered the http-equiv for content-language to have more browser support than the proper HTTP header! Only Firefox gets this right: artdent.homelinux.net/~josh/language.html
    – Josh Lee
    Dec 14, 2010 at 6:58
  • I think I'd class that as a bug in Firefox. It shouldn't take the Content-Language as specifying the document language.
    – Quentin
    Dec 14, 2010 at 7:01
  • According to HTML4 (and 5), it should.
    – Josh Lee
    Dec 14, 2010 at 7:03
  • I can only find reference to it around white space handling, but it does imply that Content-Language should be used to determine the document language. This is one of those unclear and vaguely contradictory parts of the specifications that can cause problems. Easily avoided though — always use the lang attribute and the issue goes away.
    – Quentin
    Dec 14, 2010 at 7:07
13

HTML5 update: meta http-equiv="Content-Language" is obsolete, and the lang attribute can be used on all elements.

4

They mean the same thing — setting the language of the content in question — however the lang attribute has a higher precedence. See 8.1.2 Inheritance of language codes. They handle different use cases — the lang attribute can be set <i lang=la>exempla gratis</i> on an individual element, while the Content-Language header can be configured globally by the server to apply to a whole set of documents.

Your examples show two equivalent ways to set the language of the html element, but since the lang attribute takes precedence, the value will be "en" and not "en-US".

2
  • Even though both answers are helpful, I think this answer is the most succinct, so I will award this with the green checkmark. Thanks to both jleedev and David Dorward! Dec 14, 2010 at 8:54
  • They don't mean the same thing, Quentin's answer explains the difference Sep 27, 2013 at 9:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.