The attribute CSS selector follows the pattern:
[atributo operador valor i]
Where:
atributo
: is the HTML attribute of the element
operador
: is the operator used to compare valor
and the content of the attribute in the element. Operators can be:
=
: when valor
is exactly the same the content of the attribute;
a[href="#"]{}
/* Casa com todos os links que `href` sejam exatemente "#" */
~=
: when the content of the attribute is a separated by whitespace list and valor
is a element of this list;
button[class~="is-small"]{}
/* Casa com:
<button class="button is-small is-primary"></button>
<button class="button is-active is-small"></button>
<button class="is-small button"></button>
<button class="is-small"></button>
NÃO casa com:
<button class="is-small-extra"></button>
<button class="really-is-small"></button>
*/
|=
: when the content of the attribute is exactly the same at the valor
or begins with valor
followed by a hyphen;
html[lang|="en"]{}
/* Casa com:
<html lang="en"></html>
<html lang="en-US"></html>
<html lang="en-GB"></html>
*/
^=
: when the content of the attribute begins with valor
;
a[href^="#"]{}
/* Casa com:
<a href="#">
<a href="#top">
<a href="#modal">
*/
$=
: when the content of the attribute ends with valor
;
a[href$=".html"]{}
/* Casa com:
<a href="/index.html">
<a href="/header.html">
*/
*=
: when the content of the attribute contains valor
as substring;
a[href*=".htm"]{}
/* Casa com:
<a href="/index.html">
<a href="/page.htm">
<a href="/abc.htmdef/index.cgi">
*/
valor
: is a string that will be compared to the contents of the attribute
i
: is a flag optional that indicates whether the comparison will be ascii case insensitive (only characters between a
and z
unstressed will be case insensitive).
So to marry any link whose href
end with .html
you would do:
a[href$=".html"]
But it won’t work with .Html
, .HTML
, .HtMl
, etc...
For this just add the flag i
:
a[href$=".html" i]
The above example will match correctly with the possible variations of high cash.
Vale remembers that the rule:
a[href$=".abc" i]
Will marry .ABC
, .AbC
, but won’t work with .Ábc
, or .äBc
for being
ascii case insensitive.
[EDIT] The flag s
is also part of the spec and, unlike i
, force the comparison to be ascii case sensitive.
Specs
After a good search for several versions and Drafts I ended up discovering that the flag i
in the attribute selector is part of the draft of the selector module, that being a work in progress, its support is not yet complete.
However, with the exception of Internet Explorer, Microsoft Edge and Opera Mini, modern browsers already support this Feature, as shown in Can I Use.
Seeing the version 3 of the CSS selectors we have:
Attribute values must be CSS Identifiers or strings. The case-sensitivity of attribute Names and values in selectors depends on the Document language.
Which in free translation means:
Attribute values must be CSS identifiers or strings. O case-sensitivity of attribute names and values in selectors depend on the document language.
As our document is HTML, in the Case-Sensitivity and Comparison of Strings version 5.2 we have:
Except Where otherwise stated, string comparisons must be performed in a case-sensitive Manner.
In free translation:
Unless otherwise specified, string comparisons must be done case-sensitive.
And since there is no specification in the current spec, that would explain why case-sensitive by default in browsers.
Code
a {
display: block;
padding: 10px;
transition: all ease-out .2s;
font-size: 1.5rem;
}
a:hover {
transform: scale(1.2);
}
[href*="html" i] {
color: green;
}
<a href="pagina.HTML">link terminando em .HTML</a>
<a href="pagina.html">link terminando em .html</a>
<a href="pagina.Html">link terminando em .Html</a>
Once again an excellent question and once again learned something new researching about. :)
– fernandosavio