jQuery :lang() Selector

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Standard CSS · jQuery 1.9+

What You’ll Learn

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

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.

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.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":lang(language)" )

// typical usage:
$( "div:lang(en)" )
$( "p:lang(fr)" )
$( "article:lang(de)" )

// official demo — regional tags:
$( "div:lang(en-us)" ).addClass( "usa" );
$( "div:lang(es-es)" ).addClass( "spain" );

// exact attribute (different rules):
$( "[lang='en']" )

Parameters

  • language — a language code (BCP 47 style), e.g. en, en-us, es-es.

Return value

  • A jQuery object containing every element whose language matches the code or valid prefix rules.
  • Empty collection when nothing matches in scope.

Official jQuery API example

jQuery
$( "div:lang(en-us)" ).addClass( "usa" );
$( "div:lang(es-es)" ).addClass( "spain" );

⚡ Quick Reference

Markup:lang(en)[lang="en"]
lang="en"YesYes
lang="en-US"YesNo
lang="english"NoNo
Inherited from parentYes (CSS rules)No on child alone
Native CSSYesYes

📋 :lang() vs [lang], [lang|=], and :contains()

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

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.

jQuery
$( "div:lang(en-us)" ).addClass( "usa" );
$( "div:lang(es-es)" ).addClass( "spain" );
Try It Yourself

How It Works

Regional tags en-us and es-es match exactly or by prefix rules — nested divs inherit the parent language context for styling.

Example 2 — Short code: :lang(en) vs :lang(en-us)

Short language codes match regional variants; specific codes are narrower.

jQuery
console.log( ":lang(en) →", $( "div:lang(en)" ).length );
console.log( ":lang(en-us) →", $( "div:lang(en-us)" ).length );
Try It Yourself

How It Works

Official docs: :lang(en) matches lang="en" and lang="en-US" because en-US starts with en and a hyphen.

📈 Practical Patterns

Attribute comparison, scoped content, inheritance.

Example 3 — :lang(fr) vs [lang="fr"]

Exact attribute selector misses regional variants that :lang() includes.

jQuery
console.log( ":lang(fr) →", $( "p:lang(fr)" ).length );
console.log( '[lang="fr"] →', $( 'p[lang="fr"]' ).length );
Try It Yourself

How It Works

[lang|="fr"] is the attribute analogue for prefix matching — related to but not identical to every :lang() inheritance rule.

Example 4 — Scoped: article:lang(de)

Style German articles without affecting English sections on the same page.

jQuery
$( "article:lang(de)" ).css( "borderLeft", "4px solid #2563eb" );
Try It Yourself

How It Works

Combine tag + :lang() to target semantic blocks in one locale — common in multilingual CMS output.

Example 5 — Nested inheritance on divs

Child divs without their own lang attribute still match when inside a :lang() parent.

jQuery
$( "div:lang(ja) span" ).css( "fontStyle", "italic" );
Try It Yourself

How It Works

CSS language rules consider ancestor lang values — descendants participate in the matched language context.

🚀 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.

📝 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:lang() Standard

Bottom line: Set lang attributes on content — required for accessibility and :lang() matching.

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.

💡 Best Practices

✅ Do

  • Set lang on every localized section
  • Use :lang(en) for all English variants
  • Use regional tags when styling one country only
  • Combine with element tags — article:lang(fr)
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :lang()

Language codes with prefix matching.

5
Core concepts
en- 02

Prefix

en → en-US

Rule
CSS 03

Standard

Native too

Tip
demo 04

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.

Continue to :contains() Selector

After language-based :lang() filters, learn how :contains() matches visible text substrings.

:contains() selector tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful