How to color all spans containing a given string of text?

Things I have tried

According to html - How to select a span with a text inside a div using CSS? - Stack Overflow there is no way to select by text in a span.

One of the comments in that thread though said " You can’t select elements with a specific text inside via CSS. What you can do though is to via jQuery look for that element and then add a class to it, so that you then can select it via CSS.

$("span:contains('I am special')").addClass('hide');

And then in CSS

.hide{
    display:none;
}

What I’m trying to do

I’m trying to select a <span> with a certain content, like <span>I'm special</span> so I can color its background or otherwise make it stand out.

I’m looking for something like

.span[text="I am special"] { color=red !important; }

My question would be, can I do something like this with Obsidian styles? I know very little JQuery beyond limited examples from a class a long time ago.

Only with css i dont think it is possible, only in especifics cases, the obsidian transform markdown in html when is in view mode so somethimes contents like links can be especified by the values that it contains.
Why:
In a link is obligatory to have a href so you can especify in the css by using the value in href.

But if the compiled html version does not have a point to be specified is not possible to select one alone.

I understand tha for pure CSS this is impossible, as per the quoted post I provided, but I was wondering if there were a way to use javascript somehow. Maybe I’m leaving the land of sanity here. :slight_smile:
I guess what I’m really trying to do is color a string of text that I use a lot. I don’t really care how. Right now I use a hotkey with Shell Commands on a selected string to wrap it in html. Not elegant but it works.

I belive is possible to do that with plugins(not certain), try searching or if you have time try doing it youself.

Why i think is probaly possible:

Exemple:

If you make ‘type’ apears in the configuration of the plugin.
A file with a type of ‘news’ when created a link will be displayed like this:

<a href="folder/subfolder/file" data-link-type="news">Note Number 10</a>
1 Like

So it is probaly possible to do that with javascript.

1 Like

Excellent idea, I’ve been considering it.

You can use the Dynamic Highlights plugin (obsidian://show-plugin?id=obsidian-dynamic-highlights | https://github.com/nothingislost/obsidian-dynamic-highlights) for that.

1 Like

Thanks I even have that plugin installed and wasn’t using it! I didn’t realize it also does static highlighting! Gold. It does exactly what I want.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.