lang global attribute helps define the language of an element: the language that non-editable elements are written in, or the language that the editable elements should be written in by the user. The attribute contains a single “language tag” in the format defined in Tags for Identifying Languages (BCP47) .

默认值 lang is 未知 , therefore it is recommended to always specify this attribute with the appropriate value.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

If the attribute value is the 空字符串 ( lang="" ), the language is set to 未知 ; if the language tag is not valid according to BCP47, it is set to 无效 .

Language tag syntax

The full BCP47 syntax is in-depth enough to mark extremely specific language dialects, but most usage is much simpler.

A language tag is made of hyphen-separated language subtags , where each subtag indicates a certain property of the language. The 3 most common subtags are:

Language subtag
Required. A 2-or-3-character code that defines the basic language, typically written in all lowercase. For example, the language code for English is en , and the code for Badeshi is bdz .
Script subtag
Optional. This subtag defines the writing system used for the language, and is always 4 characters long, with the first letter capitalized. For example, French-in-Braille is fr-Brai and ja-Kana is Japanese written with the Katakana alphabet. If the language is written in a highly typical way, like English in the Latin alphabet, there is no need to use this subtag.
Region subtag
Optional. This subtag defines a dialect of the base language from a particular location, and is either 2 letters in ALLCAPS matching a country code, or 3 numbers matching a non-country area. For example, es-ES is for Spanish as spoken in Spain, and es-013 is Spanish as spoken in Central America. “International Spanish” would just be es .

The script subtag precedes the region subtag if both are present — ru-Cyrl-BY is Russian, written in the Cyrillic alphabet, as spoken in Belarus.

To find the correct subtag codes for a language, try the Language Subtag Lookup .

Even if the lang attribute is set, it may not be taken into account, as the xml:lang attribute has priority.

For the CSS pseudo-class :lang , two invalid language names are different if their names are different. So while :lang(es) matches both lang="es-ES" and lang="es-419" , :lang(xyzzy) would not match lang="xyzzy-Zorp!" .

可访问性

WCAG Success Criterion 3.1.1 requires that a page language is specified in a way which may be 'programmatically determined' (i.e. via the lang 属性)。

WCAG Sucesss criterion 3.1.2 requires that pages with parts in different languages have the languages of those parts specified too. Again, the lang attribute is the correct mechanism for this.

The purpose of these requirements is primarily to allow assistive technologies such as screen readers to invoke the correct pronunciation.

For example, the language menu on this site (MDN) includes a lang attribute for each entry:

 <div class="dropdown-container language-menu">
	<button id="header-language-menu" type="button" class="dropdown-menu-label" aria-haspopup="true" aria-owns="language-menu" aria-label="Current language is English. Choose your preferred language.">English
		<span class="dropdown-arrow-down" aria-hidden="true">▼</span>
	</button>
	<ul id="language-menu" class="dropdown-menu-items right show" aria-expanded="true" role="menu">
		<li lang="ca" role="menuitem">
			<a href="/ca/docs/Web/HTML/Global_attributes/lang" title="Catalan">
				<bdi>Català</bdi>
			</a>
		</li>
		<li lang="de" role="menuitem">
			<a href="/de/docs/Web/HTML/Globale_Attribute/lang" title="German">
				<bdi>Deutsch</bdi>
			</a>
		</li>
		<li lang="es" role="menuitem">
			<a href="/es/docs/Web/HTML/Atributos_Globales/lang" title="Spanish">
				<bdi>Español</bdi>
			</a>
		</li>
		<li lang="fr" role="menuitem">
			<a href="/fr/docs/Web/HTML/Attributs_universels/lang" title="French">
				<bdi>Français</bdi>
			</a>
		</li>
		<li lang="ja" role="menuitem">
			<a href="/ja/docs/Web/HTML/Global_attributes/lang" title="Japanese">
				<bdi>日本語</bdi>
			</a>
		</li>
		<li lang="ko" role="menuitem">
			<a href="/ko/docs/Web/HTML/Global_attributes/lang" title="Korean">
				<bdi>한국어</bdi>
			</a>
		</li>
		<li lang="pt-BR" role="menuitem">
			<a href="/pt-BR/docs/Web/HTML/Global_attributes/lang" title="Portuguese (Brazilian)">
				<bdi>Português (do Brasil)</bdi>
			</a>
		</li>
		<li lang="ru" role="menuitem">
			<a href="/ru/docs/Web/HTML/Global_attributes/lang" title="Russian">
				<bdi>Русский</bdi>
			</a>
		</li>
		<li lang="uk" role="menuitem">
			<a href="/uk/docs/Web/HTML/%D0%97%D0%B0%D0%B3%D0%B0%D0%BB%D1%8C%D0%BD%D1%96_%D0%B0%D1%82%D1%80%D0%B8%D0%B1%D1%83%D1%82%D0%B8/lang" title="Ukrainian">
				<bdi>Українська</bdi>
			</a>
		</li>
		<li lang="zh-CN" role="menuitem">
			<a href="/zh-CN/docs/Web/HTML/Global_attributes/lang" title="Chinese (Simplified)">
				<bdi>中文 (简体)</bdi>
			</a>
		</li>
		<li>
			<a href="https://wiki.developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang$locales" id="translations-add">Add a translation</a>
		</li>
	</ul>
</div>
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'lang' in that specification.
实时标准 No change from latest snapshot, HTML 5.1
HTML 5.1
The definition of 'lang' in that specification.
推荐 快照 HTML 实时标准 , no change from HTML5
HTML5
The definition of 'lang' in that specification.
推荐 快照 HTML 实时标准 , behavior with xml:lang and language determination algorithm defined. It also is a true global attribute.
HTML 4.01 Specification
The definition of 'lang' in that specification.
推荐 Supported on all elements but <applet> , <base> , <basefont> , <br> , <frame> , <frameset> , <iframe> , <param> ,和 <script> .

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
lang Chrome 完整支持 Yes Edge 完整支持 12 Firefox 完整支持 Yes IE 完整支持 Yes Opera 完整支持 Yes Safari 完整支持 Yes WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 Yes Opera Android 完整支持 Yes Safari iOS 完整支持 Yes Samsung Internet Android 完整支持 Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. HTML
  2. 教程:
  3. HTML 基础
  4. HTML 介绍
    1. Introduction to HTML overview
    2. Getting started with HTML
    3. What's in the head? Metadata in HTML
    4. HTML text fundamentals
    5. Creating hyperlinks
    6. Advanced text formatting
    7. Document and website structure
    8. Debugging HTML
    9. Assessment: Marking up a letter
    10. Assessment: Structuring a page of content
  5. 多媒体和嵌入
    1. Multimedia and embedding overview
    2. Images in HTML
    3. Video and audio content
    4. From object to iframe — other embedding technologies
    5. Adding vector graphics to the Web
    6. Responsive images
    7. Assessment: Mozilla splash page
  6. 参考:
  7. HTML 元素
    1. <a>
    2. <abbr>
    3. <acronym>
    4. <address>
    5. <applet>
    6. <area>
    7. <article>
    8. <aside>
    9. <audio>
    10. <b>
    11. <base>
    12. <basefont>
    13. <bdi>
    14. <bdo>
    15. <bgsound>
    16. <big>
    17. <blink>
    18. <blockquote>
    19. <body>
    20. <br>
    21. <button>
    22. <canvas>
    23. <caption>
    24. <center>
    25. <cite>
    26. <code>
    27. <col>
    28. <colgroup>
    29. <command>
    30. <content>
    31. <data>
    32. <datalist>
    33. <dd>
    34. <del>
    35. <details>
    36. <dfn>
    37. <dialog>
    38. <dir>
    39. <div>
    40. <dl>
    41. <dt>
    42. <element>
    43. <em>
    44. <embed>
    45. <fieldset>
    46. <figcaption>
    47. <figure>
    48. <font>
    49. <footer>
    50. <form>
    51. <frame>
    52. <frameset>
    53. <h1>
    54. <head>
    55. <header>
    56. <hgroup>
    57. <hr>
    58. <html>
    59. <i>
    60. <iframe>
    61. <image>
    62. <img>
    63. <input>
    64. <ins>
    65. <isindex>
    66. <kbd>
    67. <keygen>
    68. <label>
    69. <legend>
    70. <li>
    71. <link>
    72. <listing>
    73. <main>
    74. <map>
    75. <mark>
    76. <marquee>
    77. <menu>
    78. <menuitem>
    79. <meta>
    80. <meter>
    81. <multicol>
    82. <nav>
    83. <nextid>
    84. <nobr>
    85. <noembed>
    86. <noframes>
    87. <noscript>
    88. <object>
    89. <ol>
    90. <optgroup>
    91. <option>
    92. <output>
    93. <p>
    94. <param>
    95. <picture>
    96. <plaintext>
    97. <pre>
    98. <progress>
    99. <q>
    100. <rb>
    101. <rp>
    102. <rt>
    103. <rtc>
    104. <ruby>
    105. <s>
    106. <samp>
    107. <script>
    108. <section>
    109. <select>
    110. <shadow>
    111. <slot>
    112. <small>
    113. <source>
    114. <spacer>
    115. <span>
    116. <strike>
    117. <strong>
    118. <style>
    119. <sub>
    120. <summary>
    121. <sup>
    122. <table>
    123. <tbody>
    124. <td>
    125. <template>
    126. <textarea>
    127. <tfoot>
    128. <th>
    129. <thead>
    130. <time>
    131. <title>
    132. <tr>
    133. <track>
    134. <tt>
    135. <u>
    136. <ul>
    137. <var>
    138. <video>
    139. <wbr>
    140. <xmp>
  8. 全局属性
    1. accesskey
    2. autocapitalize
    3. class
    4. contenteditable
    5. contextmenu
    6. data-*
    7. dir
    8. draggable
    9. dropzone
    10. hidden
    11. id
    12. inputmode
    13. is
    14. itemid
    15. itemprop
    16. itemref
    17. itemscope
    18. itemtype
    19. lang
    20. part
    21. slot
    22. spellcheck
    23. style
    24. tabindex
    25. title
    26. translate
    27. x-ms-acceleratorkey
    28. x-ms-format-detection
  9. <input> 类型
    1. <input type="button">
    2. <input type="checkbox">
    3. <input type="color">
    4. <input type="date">
    5. <input type="datetime">
    6. <input type="datetime-local">
    7. <input type="email">
    8. <input type="file">
    9. <input type="hidden">
    10. <input type="image">
    11. <input type="month">
    12. <input type="number">
    13. <input type="password">
    14. <input type="radio">
    15. <input type="range">
    16. <input type="reset">
    17. <input type="search">
    18. <input type="submit">
    19. <input type="tel">
    20. <input type="text">
    21. <input type="time">
    22. <input type="url">
    23. <input type="week">
  10. 文档编制:
  11. 有用清单
  12. All pages index
  13. Pages tagged "HTML"
  14. 贡献
    1. HTML 文档状态
    2. MDN 工程