Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Is it possible via CSS 3 to set the color of text in an element using the text content?

Is it possible via CSS 3 to set the color of text in an element using the text content?

Problem

Okay, so this is more of a question that has lots of solutions that are not CSS, but I'm looking for doing this more from a theoretical perspective. I have an application for it, but its not worth coding it out in any other way.

The (Fun) Question

How do you Color the text of an Element using the text of the element? I have an element, all on it's own, which will contain a hex value for a color, and I want the text to be that color, but I want to do it only using CSS (likely only can be done using CSS 3).

Sample HTML

#0000FF

So, I've tried to use the attr() with no success, but I'm not sure I'm using the right contents (I've tried text, textContent, and innerText to no avail). Doesn't need to be cross-browser, but just a way to accomplish it.

Problem courtesy of: Ktash

Solution

Currently, there is no way to use CSS to access an element's text content, not even with the CSS3 modules available today.

Regarding this:

So, I've tried to use the attr() with no success, but I'm not sure I'm using the right contents (I've tried text, textContent, and innerText to no avail). Doesn't need to be cross-browser, but just a way to accomplish it.

attr() only looks at element attributes (foo="bar"). Since text content isn't an attribute of an HTML element (despite being a member of the corresponding DOM object), you can't query for it using that function.

There isn't a similar function for accessing an element's text content.

Solution courtesy of: BoltClock

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Is it possible via CSS 3 to set the color of text in an element using the text content?

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×