This is not posible with CSS alone, you would need Javascript to achieve this.
This example shows you...
#1. How to use Javascript to search your documents p tags and find strings where the 1st index of the string is set to the target string Read:
using indexOf()
.
#2. How to then set an attribute, el.setAttribute()
, on that tag that identifies that element as a Read:
element containing the string we want to affect in our CSS.
#3. How to use CSS + elements attribute to then style the target elements in the DOM as you want them styled.
const p = [...document.getElementsByTagName('p')]
-> create an
array of elements we can then loop over using tag.forEach()
tag.textContent.indexOf("Read:") === 0
-> lets find elements that
have target string at the very beginning of the tag.textContent
.
tag.setAttribute("data-read", true)
-> set the tags data-read
attribute to true
p[data-read="true"]
in CSS we style all elements that are p tags
and have the attribute <p data-read="true">
Using this method we are able to use CSS to style the tags and JS to find them.
const p = [...document.getElementsByTagName('p')]
p.forEach(tag=>tag.textContent.indexOf("Read:") === 0 ? tag.setAttribute("data-read", true) : false)
p[data-read="true"] {
background-color: lightgreen;
font-weight: bold;
color: darkgreen;
}
p[data-read="true"]:after {
content: ' *';
}
p[data-read="true"]:before {
content: '* ';
}
<div>
<p>Read: some info</p>
<p>Read: some info</p>
<p>p tag content</p>
<p>more ptag info</p>
<p>Perhaps the p content has a sentence that starts with the string and we do not want to get that one. Read: Speak: Listen:</p>
<p>Read: At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.</p>
</div>
Read: //something
` – dale landry Jun 13 '21 at 03:08