HTML Table Row Color Based on Value

HTML Table Row Color Based on Value
Code Snippet:Based Table-cell Value Its Background Color is Changed On the Particular User Defined Color
Author: Mahendranath Reddy
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 738
License: MIT
Edit Code online: View on CodePen
Read More

This code snippet helps you to set HTML table row color based on value inside the cell. It uses JavaScript to calculate a color for each row based on its value relative to the maximum value in the table. The colors are interpolated between a user-defined start color and a user-defined end color.

You can use this code to create heatmaps or other data visualizations where the color of each row indicates the magnitude of its value. For example, this code is helpful for coloring a table of sales data by region, with the darkest rows indicating the regions with the highest sales.

How to Create HTML Table Row Color Based On Value

1. First of all, load the Bootstrap and jQuery by adding the following CDN links into the head tag of your HTML file:

<link rel='stylesheet' href=''>
<script src=''></script> 
<script src=''></script>

2. Now, create the HTML structure for your table. Include the class “heat-map” in your table’s <tbody> tag. This class is crucial for the code to target the correct elements and apply the heat map effect.

<div class="container">

<table  cellpadding="0" cellspacing="0" border="0" class="heat-map table">
            <th class="first">Title</th>
            <th>data 1</th>
            <th>data 2</th>
            <th>data 3</th>
            <th>data 4</th>
            <th>data 5</th>
            <th class="last">data 6</th>
        <tr class="stats-row">
            <td class="stats-title">Wanda</td>
		<tr class="stats-row">
            <td class="stats-title">Wave</td>
		<tr class="stats-row">
            <td class="stats-title">Candy</td>
      	<tr class="stats-row">
            <td class="stats-title">chocos</td>

3. Style the table using the following CSS styles:

  margin:20px auto;

  box-shadow:1px 0px 1px #379007;
   padding:10px 15px;
    padding:10px 15px;

4. Finally, insert the JavaScript code inside a <script> tag or in an external file. Make sure to include it within the $(document).ready(function(){}) block to ensure it runs when the document is fully loaded.

Modify the RGB color values in the JavaScript code to achieve the desired color scheme for your table rows. Adjust the xr, xg, xb, yr, yg, and yb variables to match your preferred color range.

        Array.max = function(array){
        return Math.max.apply(Math,array);

    var counts= $('.heat-map tbody td').not('.stats-title').map(function() {
        return parseInt($(this).text());
    var max = Array.max(counts);
    n = 90; 

    xr = 255; // Red value
    xg = 255; // Green value
    xb = 255; // Blue value
  //User Defined Color
    yr = 143; // Red value
    yg =72; // Green value
    yb = 117; // Blue value
    $('.heat-map tbody td').not('.stats-title').each(function(){
        var val = parseInt($(this).text());
        var pos = parseInt((Math.round((val/max)*80)).toFixed(0));
        red = parseInt((xr + (( pos * (yr - xr)) / (n-15))).toFixed(0));
        green = parseInt((xg + (( pos * (yg - xg)) / (n-15))).toFixed(0));
        blue = parseInt((xb + (( pos * (yb - xb)) / (n-15))).toFixed(0));
        clr = 'rgb('+red+','+green+','+blue+')';

That’s all! hopefully, you have successfully created a feature to set HTML Table row color based on value. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link