1

As shown in the image i want to apply different color to different rows and columns, not only to alternate ones

As shown in the image i want to apply different color to different rows and columns, not only to alternate ones

5
  • 1
    Have you tried anything ? Too broad! Commented Jun 1, 2016 at 6:18
  • 1
    You might need to add a class to each row and do the styling as necessary. Commented Jun 1, 2016 at 6:19
  • @Rayon yeas, applying color to alternate rows. Will upload the code in few minutres Commented Jun 1, 2016 at 6:20
  • I doubt if it can done using only css Commented Jun 1, 2016 at 6:24
  • @user2181397 Is their any way using JS Commented Jun 1, 2016 at 6:25

3 Answers 3

1

Check below examples.

  1. Add a class to required row and apply styles as required.
  2. Use nth-child selector.

Using class

table {
  border-collapse: collapse;
}
td {
  border: 1px solid grey;
  padding: 10px 20px;
}
tr.yellow {
  background-color: yellow;
}
tr.green {
  background-color: green;
}
tr.blue {
  background-color: blue;
}
tr.red {
  background-color: red;
}
<table>
  <tr class="yellow">
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr class="red">
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr class="green">
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr class="blue">
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

Using nth-child

table {
  border-collapse: collapse;
}
td {
  border: 1px solid grey;
  padding: 10px 20px;
}
tr:nth-child(1) {
  background-color: red;
}
tr:nth-child(2) {
  background-color: green;
}
tr:nth-child(3) {
  background-color: blue;
}
<table>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

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

1 Comment

will try it on rows and columns
0

You can define class for every row with different color or you can user :nth-child(n) pseudo class for every row

2 Comments

instead of "nth" , is their any way to write "1st child" "2nd child" "3rd child" and so
@Yashika: your can write tr:nth-child(1), tr:nth-child(2), tr:nth-child(3)
0

You could do something like this, create a random function which assigns random color to each row of the table.

Checkout a sample of that

function init(){
  var elements = document.getElementsByClassName('trentries');
    for(var i=0; i<elements.length; i++){  
      getRandomColor(elements[i]);
    }
  }

function getRandomColor(element) {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.floor(Math.random() * 16)];
    }
  element.style.background = color;
}
<body onload="init()">
  <table class='table'>
    <tr class='head'>
      <th>Col1</th>
      <th>Col2</th>
    <tr>
    <tr class='trentries'>
      <td class='entries'>a</td>
      <td class='entries'>a</td>
    </tr>
    <tr class='trentries'>
      <td class='entries'>a</td>
      <td class='entries'>a</td>
    </tr>
    <tr class='trentries'>
      <td class='entries'>a</td>
      <td class='entries'>a</td>
    </tr>
    <tr class='trentries'>
      <td class='entries'>a</td>
      <td class='entries'>a</td>
    </tr>
  </table>
<body>

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.