Home  >  Article  >  Web Front-end  >  Can You Style Specific Words Inside Anchor Links with CSS Alone?

Can You Style Specific Words Inside Anchor Links with CSS Alone?

DDD
DDDOriginal
2024-11-17 14:50:02113browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn