Home > Article > Web Front-end > Can You Style Specific Words Inside Anchor Links with CSS Alone?
Styling Anchors Based on Content
Q: Is it possible to apply unique styling to specific words contained within anchor links using CSS alone?
A: Unfortunately, no. The proposed CSS3 selector :contains is not part of the current Working Draft of CSS3 Selectors. Therefore, JavaScript is necessary to achieve desired results.
For instance, the following JavaScript code can be utilized:
Array.from(document.links).forEach(link => { if (/\bSpecificWord\b/i.test(link.innerHTML)) { link.style.color = 'red'; } });
HTML example:
<a href="#">SpecificWord</a> <a href="#">OtherWords</a>
The above is the detailed content of Can You Style Specific Words Inside Anchor Links with CSS Alone?. For more information, please follow other related articles on the PHP Chinese website!