Usage of CSS [attribute] Selector

The CSS [attribute] selector is used to select elements that contain a specific attribute, regardless of the attribute's value. This is useful for styling elements based on the presence of attributes like alt, target, title, or any custom attributes.

Syntax

[attribute] {
    /* CSS properties */
}

Example: Selecting Images with Alt Attributes

The following example applies a border to all images that have an alt attribute −

<!DOCTYPE html>
<html>
<head>
<style>
    img[alt] {
        border: 3px solid orange;
        border-radius: 5px;
    }
    
    img {
        margin: 10px;
        display: block;
    }
</style>
</head>
<body>
    <h3>Image without alt attribute:</h3>
    <img src="/css/images/logo.png" height="100" width="100">
    
    <h3>Image with alt attribute:</h3>
    <img src="/css/images/logo.png" height="100" width="100" alt="TutorialsPoint Logo">
</body>
</html>
The first image appears without any border. The second image appears with an orange border because it has an alt attribute.

Example: Selecting Links with Target Attributes

This example styles all links that have a target attribute −

<!DOCTYPE html>
<html>
<head>
<style>
    a[target] {
        background-color: yellow;
        padding: 5px;
        text-decoration: none;
        border-radius: 3px;
    }
    
    a {
        display: block;
        margin: 10px 0;
        color: blue;
    }
</style>
</head>
<body>
    <a href="/html/index.htm">Normal Link</a>
    <a href="/css/index.htm" target="_blank">Link with Target</a>
    <a href="/javascript/index.htm" target="_self">Another Link with Target</a>
</body>
</html>
The first link appears as a normal blue link. The second and third links have yellow backgrounds because they contain target attributes.

Conclusion

The [attribute] selector is perfect for targeting elements based on attribute presence rather than specific values. This makes it ideal for highlighting elements with accessibility attributes or special behaviors.

Updated on: 2026-03-15T12:30:55+05:30

319 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements