DEV Community

Cover image for Mastering CSS Attribute Selectors for Fine-grained Styling
Matt Miller
Matt Miller

Posted on • Edited on

2

Mastering CSS Attribute Selectors for Fine-grained Styling

CSS attribute selectors provide powerful mechanisms for targeting elements based on the presence or specific values of HTML attributes. Let's explore the syntax, usage, and examples of attribute selectors to understand how they can be leveraged to apply precise styling to elements.

Syntax:

  • Matches elements with the attribute attr set:

    [attr]

  • Matches elements with the attribute attr set to exactly value:

    [attr=value]

  • Matches elements with the attribute attr containing a whitespace-separated list where one of the values is exactly value:

    [attr~=value]

  • Matches elements with the attribute attr set to exactly value or starting with value-, typically used for language subcode matches:

    [attr|=value]

  • Matches elements with the attribute attr whose value is prefixed by value:

    [attr^=value]

  • Matches elements with the attribute attr whose value is suffixed by value:

    [attr$=value]

  • Matches elements with the attribute attr whose value contains at least one occurrence of value:

    [attr*=value]

  • Adds a case-insensitive modifier i to compare attribute values:

    [attr operator value i]

  • Adds a case-sensitive modifier s to compare attribute values (experimental):

    [attr operator value s]

Usage:

  • Select elements based on specific attributes or attribute values.
  • Apply styling based on attribute conditions, such as prefixes, suffixes, or containment.
  • Utilize case-insensitive matching for attribute values.

Examples:

  1. Styling internal links:
  • Background color for internal links starting with #:

    a[href^="#"]

  • Background color for links containing "example" in the URL:

    a[href*="example"]

  • Cyan color for links containing "insensitive" regardless of capitalization:

    a[href*="insensitive" i]

  1. Targeting specific link patterns:
  • Red color for links ending with ".org":

    a[href$=".org"]

  • Green color for links starting with "https://" and ending with ".org":

    a[href^="https://"][href$=".org"]

Simple instances

Conclusion:
CSS attribute selectors offer versatile ways to target elements based on their attributes and attribute values, allowing for precise styling and customization. By mastering attribute selectors, developers can enhance the visual appeal and functionality of web pages, providing users with a richer and more engaging experience. With their flexible syntax and wide range of applications, attribute selectors are indispensable tools in the CSS toolkit for creating modern and dynamic web designs.


Enjoying the content? If you'd like to support my work and keep the ideas flowing, consider buying me a coffee! Your support means the world to me!

Buy Me A Coffee

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay