Turn Negative Values in a table Red (4 posts)

  1. Lucas
    Posted 2 years ago #

    Here is part of a table i'm working on.....


    is there some code i can add in order to make any negative values red?



  2. vtxyzzy
    Posted 2 years ago #

    One way to do it is to use a function to format your numbers and wrap them with a '<span class="negative">', then use CSS to style the negative class. Here is a sample function that could go in your functions.php or earlier in your code:

    function format_neg($val) {
       if ( intval($val) < 0 ) {
          $formatted = '<span class="negative">' . number_format($val) . '</span>';
       } else {
          $formatted = number_format($val);
       return $formatted;

    Then you would call that function instead of number_format().

  3. Lucas
    Posted 2 years ago #

    Sorry for the delay, thanks for your reply vtxyzzy.
    If I add that function to my header.php file, would that mean every negative number on my site would become red?
    ps. i'm a total novice so i'm also not exactly sure how to 'use CSS to style the negative class' :S

  4. vtxyzzy
    Posted 2 years ago #

    Negative numbers would be red only where you called the function, like this:


    The function should go in your functions.php file, but be careful. If you cause an error, you cannot access your site normally, and you must use ftp to edit functions.php.

    As for the styling, add this to the end of your style.css:

    .negative { color: red; }

Topic Closed

This topic has been closed to new replies.

About this Topic