1

Ok, so I have this CSS code that changes the color according to the monetary value:

    td[data-monetary-amount]:after,
    td[data-monetary-amount^="+"] {
      content: attr(data-monetary-amount);
      color: green;
    }
    
    td[data-monetary-amount^="-"]:after {
      color: red;
    }

and HTML:

    <table border="1">
      <tr>
        <td data-monetary-amount="+23"></td>
      </tr>
      <tr>
        <td data-monetary-amount="-20"></td>
      </tr>
    </table>

The above code works great.

What I want to do is to add text characters: ↑ and ↓ according to the + and -.

These characters should be added in front of "+" "-".

So the output should be:

↑+23

↓-20

I have tried:

    td[data-monetary-amount]:after,
    td[data-monetary-amount^=" (+"],
    td[data-monetary-amount^="+"] {
      content: attr(data-monetary-amount);
      color: green;
      content: "↑";
    }

But it didn't work.

I know I'm making a mistake somewhere.

Need help.

3 Answers 3

4

You can use the pseudo :before and :after selector. Check out the below solution.

td[data-monetary-amount]:after,
    td[data-monetary-amount^="+"] {
      content: attr(data-monetary-amount);
      color: green;
    }
    td[data-monetary-amount^="+"]:before{
    content: '↑';
    }
    td[data-monetary-amount^="-"]:after {
      color: red;
    }
    td[data-monetary-amount^="-"]:before{
    content: '↓';
    color: red;
    }
<table border="1">
      <tr>
        <td data-monetary-amount="+23"></td>
      </tr>
      <tr>
        <td data-monetary-amount="-20"></td>
      </tr>
    </table>

If you got your answer, give me +1 and select my answer.

Sign up to request clarification or add additional context in comments.

Comments

1

Check out the pseudo before selector:

https://developer.mozilla.org/en-US/docs/Web/CSS/::before

You can use it to change both styling and content, with content being how you insert those characters (e.g. using their html codes).

1 Comment

No problem, if this was it, please consider marking it as the correct answer so that future searchers may also benefit.
1

Here is absolute solution

// for arrow up

td[data-monetary-amount]:after,
td[data-monetary-amount^=" (+"],
td[data-monetary-amount^="+"] {
   content: "\2191 "attr(data-monetary-amount);
   color: green;
}


// for arrow down

td[data-monetary-amount]:after,
td[data-monetary-amount^=" (-"],
td[data-monetary-amount^="-"] {
   content: "\2193 "attr(data-monetary-amount);
   color: red;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.