:lang()

Term side of the Card.
Topic: Pseudo Classes
Unit title: :lang()
Categories: Pseudo-class
Explanation side of the Card

Explanation: The pseudo-class that applies styles to elements based on the language of the content, as specified by the `lang` attribute. It helps in adapting styles for different languages, like adjusting font styles or text directions for better readability.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 3.1.
Availability: 

Example

Wrongcss
[lang='de'] q {
  quotes: '„' '“';
}
Rightcss
q:lang(de) {
  quotes: '„' '“';
}

:lang(de) also matches 'de-AT' and elements that inherit German from an ancestor.

Common mistakes

  • Using [lang='en'], which misses child elements and values like 'en-US'.
  • Styling by language when the lang attribute is missing, so :lang() matches nothing.

Key points

  • Language styles use :lang(xx), which matches inherited languages, not [lang='xx'].
  • The page sets lang on <html> and on any passage in another language.
  • Language-specific quotes use :lang() with the quotes property.

Interview questions

What is the difference between :lang() and [lang]?

[lang] checks the attribute on the element itself. :lang() checks the element's language, including inherited ones and subtags.

Does :lang(en) match en-US?

Yes. :lang(en) matches 'en' and any subtag like 'en-US' or 'en-GB'.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy