Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS rule based on content [duplicate]

Tags:

css

I would like to apply a different style to all anchors containing a specific word. Can it be done in pure CSS? It's ok if it's CSS3-only.

like image 795
Tamás Szelei Avatar asked Nov 22 '09 00:11

Tamás Szelei


People also ask

Which CSS rule takes precedence?

The more specific the CSS selector is, the higher is the precedence of the CSS property declarations inside the CSS rule owning the selector. In general terms, the more specifically (uniquely) a CSS selector targets an HTML element, the higher is its specificity.

What is CSS specificity rule?

Specificity is the algorithm used by browsers to determine the CSS declaration that is the most relevant to an element, which in turn, determines the property value to apply to the element.

Which affects CSS specificity?

If there are two or more CSS rules that point to the same element, the selector with the highest specificity value will "win", and its style declaration will be applied to that HTML element. Think of specificity as a score/rank that determines which style declaration are ultimately applied to an element.


1 Answers

No. :contains was once proposed but is not in the current Working Draft of CSS3 Selectors.

You would need some JavaScript, for example:

for (var i= document.links.length; i-->0;)     if (/\bSpecificWord\b/i.test(document.links[i].innerHTML)         document.links[i].style.color= 'red'; 
like image 122
bobince Avatar answered Oct 08 '22 17:10

bobince