Set int color based on positive or negative value with CSS

Quick question. I want to change the color of an int based on a positive or negative value using CSS if possible.

Any ideas?

+2


a source to share


3 answers


In PHP, you can do this:

<span style="color: <?php echo ($var < 0 ? '#FF0000' : '#00FF00'); ?>">Some text</span>

      



This evaluates the $ var variable, if it is less than zero, it applies to the style of red (FF0000), otherwise green (00FF00).

+1


a source


It depends on the language you are using. In almost any language, you can set a tag class around a value using the dynamic part of the language. If the value has no tags of its own, you can use <span></span>

around the value to give it a class. then you just use dynamic code to set the class.



If you are using the Ruby language, there are some CSS selector gems you can use, but I know little about them or if they help.

0


a source


I am using wordpress and the table with new posts and custom posts is at index.php, the code I paste between the td tags

<td style="font-weight: <?php echo ($var <= 0 ? 'normal' : 'bold'); ?>"><?php echo get_portf_posts_count_from_last_24h(); ?></td>

      

I tried this code too and it doesn't work:

 <?php
    if ($output <= 0) {
      $output = 'normal';
      print $output;
      }
    else // +
    {
      $output = 'bold';
      print $output;
    }
    ?>

      

0


a source







All Articles