You can even use the selector ^=
of the CSS that will apply the style based on the prefix of the value in that attribute. For example, [class^="result_itens_"]
will apply the style to all elements that the class
starts with the value result_itens_
.
[class^="result_itens_"] {
width: 100px;
height: 100px;
background-color: red;
}
<div class="result_itens_id1255454">
</div>
The problem is when the element has this class, but is not the first in the element.
[class^="result_itens_"] {
width: 100px;
height: 100px;
background-color: red;
}
<div class="foo result_itens_id1255454">
Não ficarei vermelho
</div>
For now class
begins with foo
, no longer with result_itens_
. There’s also the dial *=
that is contains a value.
[class*="result_itens_"] {
width: 100px;
height: 100px;
background-color: red;
}
<div class="foo result_itens_id1255454">
</div>
<hr>
<div class="my_result_itens_bla">
Por que fiquei vermelho?
</div>
That circumvents the problem of not being the first class in the element, but that allows generating false positives, as it will apply the style in all the elements that have, anyway, the value result_itens_
in the attribute class
.
One way - not very pretty, but to do what - is to use
[class*=" result_itens_"], [class^="result_itens_"]
(with a space beforeresult
in the case of*=
), thus greatly decreases false positives (I do not know if eliminates 100%)– hkotsubo
@hkotsubo If the value after the prefix does not matter I believe that would remove all false positives even.
– Woss