Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:lang() — selecting elements by content language

The :lang() pseudo-class selects elements by their content language, including language determined from an ancestor. It enables language-specific styling without adding a lang attribute to every element.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

:lang(language-range)
:lang(language-range, language-range, ...)

:lang() is a functional pseudo-class that matches an element based on its content language. The notation :lang names this pseudo-class, but is not a valid selector on its own: parentheses and an argument are required. The selector neither translates text nor detects language from words.

In HTML, language can come from an ancestor's lang attribute. Consequently, :lang(pl) can match an element without its own lang, whereas [lang|="pl"] checks only that element's attribute. A descendant's own lang can change its language relative to its ancestor.

For basic ranges, :lang(en) matches values including en and en-US, but not eng; comparison is ASCII case-insensitive. CSS2 and Selectors Level 3 describe a single argument. Selectors Level 4 adds lists, such as :lang(pl, en), and extended ranges with wildcards, such as :lang("*-Latn"). A range must be a CSS identifier or string; an asterisk must be quoted or appropriately escaped. Check support for Level 4 extensions separately in the target browser.

In the example, a inherits the language pl-PL, so it matches :lang(pl) and is green. Element b has its own lang="en", so it does not match and remains black. To check whether each element matches and see its computed color, open the browser console. The results are logged only there.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Matching inherited content language</title>
  <style>
    p { color: rgb(0, 0, 0); }
    p:lang(pl) { color: rgb(0, 128, 0); }
  </style>
</head>
<body>
  <div lang="pl-PL">
    <p id="a">Dzień dobry.</p>
    <p id="b" lang="en">Good morning.</p>
  </div>
  <script>
    for (const id of ['a', 'b']) {
      const paragraph = document.getElementById(id);
      console.log(`${id}: ${paragraph.matches(':lang(pl)')}, ${getComputedStyle(paragraph).color}`);
    }
  </script>
</body>
</html>

Result

a: true, rgb(0, 128, 0)
b: false, rgb(0, 0, 0)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources