CSS selectors allow selecting elements by any attribute value such as class
, id
, href
etc. This means we can extract any HTML elements based on attribute value with CSS selectors.
While class
and id
have special notation shortcuts - .
and #
respectively - any attribute can be selected using the attribute selector ([attribute]
) which supports multiple operators:
- The
=
match can be used for exact equality e.g.[attr=match]
:
- The
~=
turns the attribute into an array of space-separated values and checks whether it contains the match (similar to.class
and#id
matches):
- The
|=
checks for exact equality except ignores hypen suffixes (e.g.-language
):
- The
^=
checks whether the attribute starts with the match:
- The
$=
checks whether the attribute ends with the match:
- The
*=
checks whether the attribute contains the match:
Finally, to make all of these matches case insensitive add i
before the closing bracket, e.g. [attr=match i]