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

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

DDD
Release: 2024-11-17 14:50:02
Original
173 people have browsed it

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';
  }
});
Copy after login

HTML example:

<a href="#">SpecificWord</a>
<a href="#">OtherWords</a>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template