I'm attempting to create a dynamic background image change in CSS, based on the text content within an element. Here's the context:
I have a table with a "status" column, where each cell in this column contains various text content, such as "Available" and "Expired"
My goal is to assign a background image to each cell in this column, matching its respective content.
Essentially, I'm looking for a CSS solution that can achieve the same dynamic transition effect seen with linear gradients. Here's an example:
span {
width: fit-content;
display: inline-flex;
align-items: center;
background:
linear-gradient(rgb(247, 117, 110) 0 0) 0/calc(3.5rem - 100%) 100%,
linear-gradient(hsl(123, 90%, 40%) 0 0) 0/calc(4rem - 100%) 100%;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<span>Available</span>
<span>Expired</span>
<span>Available</span>
<span>Expired</span>
However, instead of using a linear gradient, I want to dynamically change the background image based on the content within each cell. I've attempted a CSS approach, but it defaults to using the first image.
span {
width: fit-content;
display: inline-flex;
align-items: center;
background:
url(https://cdn-icons-png.flaticon.com/512/5268/5268742.png) 0/calc(3.5rem - 100%) 100%,
url(https://cdn-icons-png.flaticon.com/512/5268/5268923.png) 0/calc(4rem - 100%) 100%;
background-size: cover;
-webkit-text-fill-color: transparent;
}
<span>Available</span>
<span>Expired</span>
<span>Available</span>
<span>Expired</span>
Is there a pure CSS solution that can achieve this dynamic background image change based on the cell's content?
I'm employing a pre-existing component for which I lack access to its HTML code, and I am unable to make alterations. Thus, my sole option is to manipulate it using CSS
Don't mind the height of the images; they're just for testing.