The :lang() selector matches elements in a given language — using the HTML lang attribute and CSS prefix rules. Standard CSS since jQuery 1.9; official demo styles USA and Spain blocks.
01
Syntax
:lang(en)
02
lang attr
BCP 47 codes
03
Prefix
en → en-US
04
Official
USA / Spain
05
[lang=]
Exact only
06
CSS
Standard
Fundamentals
Introduction
Multilingual sites mark content with lang="en", lang="fr", or regional tags like lang="en-US". The jQuery :lang() pseudo-class selects every element whose language matches a code you pass — ideal for styling, QA checks, or scripting locale-specific behavior.
jQuery added support for :lang() in version 1.9, following the W3C CSS specification. A selector like div:lang(en) matches elements whose language is en or starts with en- (such as en-US), but not unrelated values like english.
Concept
Understanding the :lang() Selector
Language matching follows CSS prefix rules:
lang="en" + :lang(en) → matches.
lang="en-US" + :lang(en) → matches (starts with en-).
lang="en-US" + :lang(en-us) → matches when codes align.
lang="english" + :lang(en) → no match (not a valid prefix pattern).
Child elements can inherit language from a parent lang attribute.
💡
Beginner Tip
Use short codes like :lang(en) to catch all English variants. Use full tags like :lang(en-us) when you only want United States English blocks, as in the official demo.
Language codes vs exact attributes vs visible text.
:lang(en)
div:lang(en)
Prefix match
[lang=]
[lang="en"]
Exact value
[lang|=]
[lang|="en"]
en or en-*
:contains
:contains("Hi")
Text substring
Hands-On
Examples Gallery
Example 1 follows the official jQuery USA/Spain color demo. Examples 2–5 cover short vs regional codes, attribute comparison, scoped articles, and inherited language on nested divs.
📚 Official jQuery Demo
Color div blocks by en-us and es-es language tags.
Example 1 — Official Demo: USA and Spain themes
Official jQuery demo — add usa and spain classes to matching language divs with nested color rules.
span inside div lang="ja" → italic
span inside English div → normal
How It Works
CSS language rules consider ancestor lang values — descendants participate in the matched language context.
Applications
🚀 Common Use Cases
Locale styling — theme blocks with div:lang(en-us) as in the official demo.
Multilingual QA — count article:lang(fr) sections on a page.
Typography — different fonts for :lang(ja) vs :lang(en).
Accessibility audits — verify lang attributes exist on translated content.
CMS output — hook scripts to language-tagged regions only.
CSS parity — same :lang() rules in stylesheets and jQuery.
🧠 How jQuery Evaluates :lang()
1
Resolve language
Determine each element’s language from lang attributes and ancestor context.
Lang
2
Compare code
Match if equal to the argument or starts with argument + hyphen (e.g. en vs en-US).
Prefix
3
Filter candidates
Apply tag prefix if present — div:lang(en) limits to div elements.
Scope
4
✓
Return collection
Chain .addClass(), .css(), or read .length.
Important
📝 Notes
Available since jQuery 1.9 — standard CSS pseudo-class.
Language codes are case-insensitive in HTML — use lowercase in selectors for consistency.
:lang(en) matches en and en-* variants, not arbitrary strings.
Differs from [lang="en"] — exact attribute match only on that element.
Related attribute selector: [lang|="en"] for prefix on the attribute itself.
Always set lang on multilingual pages for accessibility and correct :lang() behavior.
Compatibility
Browser Support
The :lang() pseudo-class is standard CSS and works in jQuery 1.9+ and native querySelectorAll(":lang(en)") in modern browsers. Behavior follows the W3C CSS specification for language matching.
✓ jQuery 1.9+ · standard CSS
jQuery :lang() Selector
Match elements by language code — en matches en-US via prefix rules.
100%Universal
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:lang()Standard
Bottom line: Set lang attributes on content — required for accessibility and :lang() matching.
Wrap Up
Conclusion
The :lang() selector matches elements whose language equals the supplied code or starts with that code plus a hyphen. The official demo adds USA and Spain theme classes with div:lang(en-us) and div:lang(es-es).
Use short codes for broad locale groups, distinguish :lang() from exact [lang="…"] attributes, and mark up multilingual HTML with proper lang values.
Mirror :lang() rules in CSS stylesheets when possible
❌ Don’t
Confuse :lang() with :contains() — language vs text
Expect lang="english" to match :lang(en)
Assume [lang="en"] matches lang="en-US"
Omit lang attributes on translated content
Use made-up codes — stick to BCP 47 tags like en-us
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :lang()
Language codes with prefix matching.
5
Core concepts
🌐01
:lang()
Language
API
en-02
Prefix
en → en-US
Rule
CSS03
Standard
Native too
Tip
demo04
Official
usa/spain
Demo
[lang]05
Exact
Differs
Compare
❓ Frequently Asked Questions
:lang(language) selects elements whose language matches the supplied language code. $("div:lang(en)") matches elements with lang="en" or lang="en-US" (code plus hyphen suffix), but not unrelated codes like lang="english". Available since jQuery 1.9.
[lang="en"] matches only an exact lang attribute value on that element. :lang(en) also matches lang="en-US", lang="en-GB", and elements that inherit language from an ancestor with a matching lang value per CSS rules.
Yes. :lang() is defined in the W3C CSS specification and works in jQuery 1.9+ and native querySelectorAll(":lang(en)") in modern browsers.
For HTML, jQuery/CSS determine language from the lang attribute on the element or ancestors, and possibly from meta elements or HTTP headers — as described in the official jQuery documentation.
Official jQuery demo runs $("div:lang(en-us)").addClass("usa") and $("div:lang(es-es)").addClass("spain") to color USA and Spain content blocks red/white/blue and red/yellow styling.
Yes. :lang(en) matches both lang="en" and lang="en-US" because en-US starts with en followed by a hyphen — prefix matching built into :lang(). Use :lang(en-us) when you need the specific regional tag only.
Did you know?
The attribute selector [lang|="en"] matches lang="en" and values like lang="en-US" on the attribute itself — similar prefix logic to :lang(en), but :lang() also follows CSS rules for language inheritance from ancestors.