HTML Lang Attribute

While designing a website or even a single web page, it is very important to set up an appropriate language for it that the target audience can conveniently understand. For example, if you are creating a web form to target a French audience, then it will be of no use to create that form in Spanish. Rather, that form will be useful only if it is created in French. Therefore, in today’s article, we will be talking about the “lang” attribute of HTML.

Purpose of the Lang Attribute in HTML:

The purpose of the “lang” attribute in HTML is to specify the language of the entire document or of a particular section or element of the document. The most commonly used “lang” attributes in HTML are “en”, “es”, “fr”, etc. for English, Spanish, and French languages, respectively.

How can we use the Lang Attribute in HTML?

To use the “lang” attribute in HTML, you have to go through the following two examples:

Example # 1: Setting a Single Language for the Entire Document:

In this example, we will teach you how you can set a single language for your entire document in HTML, i.e., a unified language for all the elements used within your HTML script. To do this, you have to take a look at the HTML script shown below:

In this example, we have set the “lang” attribute to “en” at the beginning of our HTML script with the “html” tag. Doing this will set the language of this whole script to English. It means that no matter which element you use within this script, it will have the very same language, i.e., English in this case. After doing that, we simply used a header tag and a paragraph tag for defining a heading and writing a statement within our HTML script.

After executing this HTML script, the following web page appeared on our browser:

Example # 2: Using Multiple Languages within a Single Document:

In this example, we will show you how you can have different languages for the different elements used within an HTML script. For that, you will have to understand the HTML script shown in the image below:

In this example, we have not mentioned any language with the “html” tag because we did not want a unified language for the entire document. Then, we have defined a heading for conveying the purpose of this script to the reader. After that, we used the “lang” attribute with the “p” tag followed by an English statement since we kept the value of this attribute as “en”. Then, we used another paragraph tag, and this time, we used the “lang” attribute with it the value “fr” which is followed by a French statement.

When we executed this HTML script, our statements in both the languages, i.e., English and French, appeared on the web page as shown in the following image:


This article revolved around the “lang” attribute of HTML. By using this attribute, you can define the language of an entire document or of a particular element of an HTML script. To make you understand this, we shared with you two different examples in which we talked about changing the language of an entire document as well as different elements of an HTML script. By following this guide, you will be able to use the “lang” attribute in HTML.

About the author

Ayesha Sajid

Ayesha Sajid has secured a Bachelor's degree in Computer Sciences and a Master's degree in Information Security. She is a technical content writer by profession who has around four years of experience in working with Windows and different flavors of the Linux operating system. She also has a keen interest in exploring the latest technology trends.