Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:03:29+00:00 2026-05-27T18:03:29+00:00

My site reads a XML file that contains information (values) for a data-table. I

  • 0

My site reads a XML file that contains information (values) for a data-table.
I use CSS to style the table and everything works fine.

To get a better user-experience I wondered if it is possible to change the background color of each cell dynamically depending on its value?

For example:

Each cell that contains a number less then “5” has a red background color;

each cell >= “5” has a green background color.

My first solution on this is to use Javascript – but I want to know if there is a way to solve this with CSS-styles only?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:03:30+00:00Added an answer on May 27, 2026 at 6:03 pm

    This isn’t possible with just CSS (though you can use JavaScript to assign classes to enable it to be partially implemented with CSS). To use plain JavaScript, rather than a library:

    var table = document.getElementById('tableID');
    var tbody = table.getElementsByTagName('tbody')[0];
    var cells = tbody.getElementsByTagName('td');
    
    for (var i=0, len=cells.length; i<len; i++){
        if (parseInt(cells[i].innerHTML,10) > 5){
            cells[i].style.backgroundColor = 'red';
        }
        else if (parseInt(cells[i].innerHTML,10) < -5){
            cells[i].style.backgroundColor = 'green';
        }
    }
    

    JS Fiddle demo.

    Or, to use CSS classes:

    var table = document.getElementById('tableID');
    var tbody = table.getElementsByTagName('tbody')[0];
    var cells = tbody.getElementsByTagName('td');
    
    for (var i=0, len=cells.length; i<len; i++){
        if (parseInt(cells[i].innerHTML,10) > 5){
            cells[i].className = 'red';
        }
        else if (parseInt(cells[i].innerHTML,10) < -5){
            cells[i].className = 'green';
        }
    }
    

    JS Fiddle demo.

    References:

    • className.
    • getElementById().
    • getElementsByTagName().
    • innerHTML.
    • parseInt().
    • style.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m making a simple website to display information from XML file that is created
Am working on a project that requires uploading xml file to remote FTP site.
I have xml file that contains the URLs for geocoding requests to Google. I
I have a controller that looks like this for importing xml to my site:
The following test function reads an XML file with PHP's xmlreader and returns a
I want to read the following XML file received from my e-commerce site and
In a remote site, there is a HTML file (say http://www.example.com/abc.html ), which reads:
I am trying to open a directory that contains a series of XML's in
So I have this pretty large XML file (40MB) that I'll have to repeatedly
I'm trying to create a site that will pull xml nodes into html using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.