Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS [attribute*=value] Selector

Posted in CSS Tutorial
Updated on Oct 27, 2024
By Mari Selvan
πŸ‘οΈ 7 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
CSS [attribute*=value] Selector

Photo Credit to CodeToFun

πŸ™‹ Introduction

The CSS [attribute*=value] selector is used to target elements that contain a specific substring within an attribute value.

This powerful attribute selector allows you to match elements based on partial matches within the attribute, making it particularly useful for styling elements dynamically based on data attributes, class names, or other custom attributes.

πŸ’‘ Syntax

The signature of the [attribute*=value] Selector is as follows:

Syntax
Copied
Copy To Clipboard
[element[attribute*="value"]] {
    /* CSS properties */
}

In the syntax, element refers to the element type, attribute refers to the attribute you want to target, and value is the substring you are matching within that attribute’s value.

πŸ“ Example

Here is a practical example of how to use the [attribute*=value] selector in CSS.

☠️ HTML

HTML
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS [attribute*=value] Selector Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <a href="https://example.com/about" class="nav-link">About Us</a>
    <a href="https://example.com/contact" class="nav-link">Contact Us</a>
    <a href="https://example.com/blog" class="nav-link">Blog</a>
</body>
</html>

🎨 CSS

CSS
Copied
Copy To Clipboard
/* Target all anchor tags with 'https' in their href attribute */
a[href*="https"] {
    color: blue;
    font-weight: bold;
}

/* Specific styling for URLs containing 'about' */
a[href*="about"] {
    text-decoration: underline;
}

In this example:

  • The selector a[href*="https"] applies styles to all anchor (<a>) elements with an href attribute containing "https".
  • The second rule, a[href*="about"], further targets anchor elements whose href contains the substring "about".

πŸ’¬ Usage Tips

  • The [attribute*=value] selector is case-sensitive by default, meaning that it distinguishes between uppercase and lowercase letters.
  • Use this selector when you need to style elements based on part of an attribute value, such as targeting links with specific URLs, images with particular file extensions, or elements with class names that follow a pattern.
  • It works well with custom data attributes. For example, you can select elements whose data- attributes contain certain values.

⚠️ Common Pitfalls

  • Performance Considerations: Using attribute selectors with wildcards like *= can lead to performance issues if overused, especially on large DOM structures, as the browser needs to evaluate the substring for every matching element.
  • Case Sensitivity: Attribute values are case-sensitive, so be mindful of the capitalization in your attribute values when using this selector.
  • Specificity: The specificity of an attribute selector is relatively low, meaning it can be easily overridden by other, more specific selectors. Make sure to combine it with element or class selectors when needed.

πŸŽ‰ Conclusion

The CSS [attribute*=value] selector offers a versatile way to target elements based on partial matches within attribute values. This makes it a powerful tool for styling dynamic content, improving your ability to handle complex scenarios where attributes contain specific substrings. When used judiciously, it enhances your control over element styling and enables you to write more flexible, adaptable CSS rules.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy