The Wayback Machine - https://web.archive.org/web/20250124122727/https://www.geeksforgeeks.org/css-attribute-selector/
Open In App

CSS Attribute Selector

Last Updated : 04 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS attribute Selector allows you to select elements based on the presence, value, or specific characteristics of their attributes. They are particularly useful for dynamic or structured content where attributes play a key role, such as in forms or data tables.

Types of CSS Attribute Selectors

1. [attribute] Selector

This selector matches all elements with the specified attribute, regardless of its value.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Styles all elements with a 'placeholder' attribute */
        [placeholder] {
            border: 2px solid blue;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <input type="text" placeholder="Enter your name">
    <input type="password" placeholder="Enter your password">
</body>
</html>
<!--Driver Code Ends }-->

2. [attribute=”value”] Selector

This selector matches elements with an attribute whose value is exactly equal to the specified value.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Styles the button with type="submit" */
        [type="submit"] {
            background-color: green;
            color: white;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <button type="submit">Submit</button>
    <button type="reset">Reset</button>
</body>
</html>
<!--Driver Code Ends }-->

3. [attribute~=”value”] Selector

This selector matches elements where the attribute’s value is a space-separated list of words and one of the words matches the specified value.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Styles elements where class includes 'green' */
        [class~="green"] {
            border: 3px solid red;
        }
        .box {
            height: 100px;
            width: 100px;
            position: relative;
            background-color: green;
        }
        .container {
            display: flex;
            height: 500px;
            width: 100vw;
            gap: 30px;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <div class="container">
        <div class="box large green"></div>
        <div class="box small red"></div>
    </div>
</body>
</html>
<!--Driver Code Ends }-->

4. [attribute|=”value”] Selector

This selector matches elements whose attribute value is either exactly value or starts with value followed by a hyphen (-). It is often used for language attributes.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Styles elements with lang="en-US" or lang="en-GB" */
        [lang|="en"] {
            color: blue;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <p lang="en-US">Hello</p>
    <p lang="en-GB">Hello</p>
    <p lang="fr">Bonjour</p>
</body>
</html>
<!--Driver Code Ends }-->

5. [attribute^=”value”] Selector

This selector matches elements whose attribute value starts with the specified value.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Styles links with href starting with 'https' */
        [href^="https"] {
            text-decoration: underline;
            color: darkgreen;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <a href="https://example.com">Visit Example</a>
    <br>
    <a href="mailto:contact@example.com">Email Us</a>
</body>
</html>
<!--Driver Code Ends }-->

6. [attribute$=”value”] Selector

This selector matches elements whose attribute value ends with the specified value.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Selects all elements whose class ends with "box" */
        [class$="box"] {
            border: 2px solid blue;
            padding: 10px;
            background-color: lightgray;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <div class="small-box">This is a small box</div>
    <div class="large-box">This is a large box</div>
    <div class="circle">This is not a box</div>
</body>
</html>
<!--Driver Code Ends }-->

7. [attribute=”value”] Selector*

This selector matches elements whose attribute value contains the specified value anywhere within it.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Selects all div elements where class contains "box" */
        div[class*="box"] {
            background-color: lightgreen;
            color: darkgreen;
            padding: 20px;
            border: 2px solid green;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <div class="small-box">This is a small box</div>
    <div class="large-box">This is a large box</div>
    <div class="circle">This is not a box</div>
</body>
</html>
<!--Driver Code Ends }-->

8. Combining Attribute Selectors

You can combine multiple attribute selectors to refine your targeting.

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        /* Styles inputs that are both type="text" and data-required="true" */
        [type="text"][data-required="true"] {
            background-color: green;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <input type="text" data-required="true">
    <input type="password" data-required="true">
</body>
</html>
<!--Driver Code Ends }-->

Practical Use Cases

CSS Attribute Selector – FAQ’s

What are attribute selectors in CSS, and why are they used?

Attribute selectors in CSS allow developers to apply styles to HTML elements based on the presence or value of attributes. They provide a way to target elements more specifically without relying on class or ID selectors.

How do attribute selectors differ from class and ID selectors?

Unlike class and ID selectors, which are based on class names or unique identifiers, attribute selectors target elements based on their attributes, such as type, name, or href. This allows for more granular styling based on the attributes of elements rather than their classes or IDs, enabling styles to be applied to a wider range of elements with similar attributes.

Can attribute selectors match partial attribute values?

Yes, attribute selectors can match elements based on partial values of an attribute. For instance, they can target elements where the attribute contains, begins with, or ends with a specific value, providing greater flexibility when styling elements with similar but not identical attribute values.

What are the potential limitations of using attribute selectors?

One limitation is that attribute selectors can be less performant in large documents compared to class or ID selectors, especially when used with complex patterns. Additionally, they may not be as widely supported in very old browsers, although most modern browsers now support them.

Can attribute selectors be combined with other selectors?

Yes, attribute selectors can be combined with other types of selectors, such as class selectors, pseudo-classes, and pseudo-elements. This allows for more complex and targeted styling, enabling you to apply styles to elements that match both an attribute condition and another criterion, like being a certain state or position in the document.



Next Article

Similar Reads

three90RightbarBannerImg