How to properly use lang in HTML
Asked Answered
K

2

6

I want to ask you how to properly use lang in HTML code. Im trying to have website in 2 langs en, pl.

<html lang="en">
<html lang="pl">

Is this way correct?

Killifish answered 27/11, 2016 at 11:5 Comment(3)
you cannot use two html elementsGaddis
So, correct way will be like.. <html lang="en"> <lang="pl"> or <div> one?Killifish
developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/…Diatribe
S
2

You can't have two <html> tags in the same document. You should use one <html> tag without the lang attribute and use the lang attribute on tags that contain only one language. Here is an example:

<html>
<head>
    <!--some elements in the head-->
</head>
<body>
    <p lang="en">This is text</p>
    <p lang="fr">Ceci est du texte</p>
</body>
</html>
Sharolynsharon answered 27/11, 2016 at 11:22 Comment(0)
I
7

If the document’s primary language is in, say, Polish with large parts that are in English, then it should have <html lang="pl"> with <div lang="en"> or something around the English parts:

<html lang="pl">
…
<body>
[Polish content]
    <div lang="en">
        [English content]
    </div>
[more Polish content]
    <div lang="en">
        [more English content]
    </div>
[more Polish content]
…

Regardless the html element should if possible always have a lang value, per the HTML spec:

Authors are encouraged to specify a lang attribute on the root html element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth.

But if a document is such a mix of languages that it can’t really be seen as having a single primary language, then the html element still should have a lang attribute, but with an empty value:

Setting the attribute to the empty string indicates that the primary language is unknown.

<html lang="">
…
<body>
    <div lang="pl">
        [Polish content]
    </div>
    <div lang="en">
        [English content]
    </div>
    <div lang="pl">
        [Polish content]
    </div>
    <div lang="en">
        [English content]
    </div>
    …

For more detailed information on this, see the following W3C guides:

Inbeing answered 27/11, 2016 at 11:5 Comment(0)
S
2

You can't have two <html> tags in the same document. You should use one <html> tag without the lang attribute and use the lang attribute on tags that contain only one language. Here is an example:

<html>
<head>
    <!--some elements in the head-->
</head>
<body>
    <p lang="en">This is text</p>
    <p lang="fr">Ceci est du texte</p>
</body>
</html>
Sharolynsharon answered 27/11, 2016 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.