CSS Treehouse Club: CSS My First Web Page What's New in Your HTML

If the webpage is written in more than one languages is it still useful to add language attributes?

(Or perhaps i've misunderstood the usage of language attributes, does it specifies the language on its web page or the language used for coding?)

2 Answers

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 46,736 Points

The lang attribute specifies the spoken language that the element (or in the case of this video where lang is specified on the html tag, the page) is in. For example, this page is in English on my machine, so the html tag might look like this:

<html lang="en">
<!-- Page code here -->
</html>

Let's say this page was also in Swedish. The Swedish version of this page's html tag might look like this:

<html lang="sv">
<!-- Page code here -->
</html>

HTML uses the ISO 639-1 standard to specify language codes, and here's a full list of all possible codes

Thanks Hulet, but say if I have both Swedish and English on the same website. I only have to declare the primary language right?

Michael Hulet
Michael Hulet
Treehouse Moderator 46,736 Points

Every tag supports the lang attribute, so you could declare the default language on the html tag and declare that specific tags on a page are in Swedish, like this:

<html lang="en">
<head>
    <title>Example Page</title>
</head>
<body>
    <p>This is in English, and the browser knows it because the html tag says so</p>
    <!-- Notice the lang attribute on the next tag -->
    <p lang="sv">Det här är på svenska, och webbläsaren vet det eftersom taggen har sagt det</p>
    <p> This tag is in English and still inherits the lang attribute from the html tag because the lang attribute is inherited by all of a tag's children. If I made another tag within the Swedish paragraph above, all those tags would be understood to be in Swedish</p>
</body>
</html>

Thank you, thats very helpful.