w3resource

lang and dir attribute of HTML

lang and dir attributes of HTML specify language and text direction of an element.

HTML lang attribute

1. lang attribute of HTML sets the language of an element in HTML document. Usage of lang attribute helps the browser to display the text in the desired language.

2. The value of the lang attribute is the language code specified by the W3C to be used with HTML.

Syntax

<ElementName lang="language_code">text content</ElementName> 

Where ElementName is any HTML element name except applet, base, basefont, br, frame, frameset, iframe, param, script.

Example of using lang attribute

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example lang attribute - HTML tutorial | w3resource</title>
</head>
<body>
<p lang="en-US">This paragraph is in English.</p>
<p lang="es">Este apartado está en español</p>
</body>
</html>

Result

html lang attribute

View this example in a separate browser window

Example of lang attribute

HTML dir attribute

Description

1. dir attribute of HTML sets the direction of the text within an element in HTML document.

2. The value of the dir attribute is either ltr (i.e. left to right) or rtl (i.e. right to left).

HTML dir attribute supports All elements but APPLET, BASE, BASEFONT, BDO, BR, FRAME, FRAMESET, IFRAME, PARAM, SCRIPT

Example of using dir attribute

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example dir attribute - HTML tutorial | w3resource</title>
</head>
<body>
<p dir="ltr" >This text is in left ot right direction.</p>
<p dir="rtl">טקסט זה בשטחים השמאלי בכיוון הנכון</p>
</body>
</html> 

Result

This text is in left to right direction.

טקסט זה בשטחים השמאלי בכיוון הנכון

View this example in a separate browser window

Example of dir attribute

Note

If you are using a plain text editor like notepad to write right to left text using dir="rtl", you must set the encoding style to UNICODE while saving the file.

Previous: HTML label attribute
Next: HTML longdesc attribute