Role of CSS:lang Selector

The CSS :lang selector allows you to apply specific styles to elements based on their language attribute. This pseudo-class selector targets elements that have a specific lang attribute value, making it useful for multilingual websites where different languages may need different styling.

Syntax

:lang(language-code) {
    /* CSS properties */
}

Example: Styling French Text

The following example applies a green background to all paragraph elements with French language attribute −

<!DOCTYPE html>
<html>
<head>
<style>
    p:lang(fr) {
        background-color: lightgreen;
        padding: 10px;
        border-left: 3px solid green;
    }
    p:lang(es) {
        background-color: lightyellow;
        padding: 10px;
        border-left: 3px solid orange;
    }
</style>
</head>
<body>
    <p>This is my country</p>
    <p lang="fr">C'est mon pays</p>
    <p lang="es">Este es mi país</p>
    <p>English text without lang attribute</p>
</body>
</html>
The French paragraph displays with a light green background and green left border, while the Spanish paragraph shows with a light yellow background and orange left border. The other paragraphs remain unstyled.

Example: Font Styling for Different Languages

You can also apply different fonts and text properties based on language −

<!DOCTYPE html>
<html>
<head>
<style>
    p:lang(ar) {
        direction: rtl;
        font-family: 'Arial', sans-serif;
        font-size: 18px;
        text-align: right;
    }
    p:lang(zh) {
        font-family: 'SimSun', serif;
        font-size: 16px;
        line-height: 1.6;
    }
</style>
</head>
<body>
    <p>English text</p>
    <p lang="ar">???? ??????</p>
    <p lang="zh">????</p>
</body>
</html>
The Arabic text displays right-to-left with appropriate font styling, while the Chinese text uses a serif font with increased line height. The English text remains with default styling.

Conclusion

The :lang selector is essential for creating accessible multilingual websites. It enables language-specific styling based on the lang attribute, helping to improve readability and user experience across different languages.

Updated on: 2026-03-15T12:20:19+05:30

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements