Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different background-color for each cell of the first row and first column in a CSS table

I'm trying to add a different background color to each cell of the first row and first column of a table.

This is what it should look like:

Itinerary

I found a selector to apply different color to the cells of the first row but now I'm stuck with the cells of the first column (Morning, Afternoon and Evening). I managed to get them all blue but each of them should actually have a different shade of blue...). This is my CSS code:

table.agenda {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;}

table.agenda td, table.agenda th {
border: 1px solid #fff;
padding: 8px;
text-align: center;}

table.agenda td {
padding-top: 12px;
padding-bottom: 12px;
background-color: rgb(193, 212, 174);
color: black;}

th:nth-child(1) { background: white; }
th:nth-child(2) { background: rgb(72, 151, 54); color: white;}
th:nth-child(3) { background: rgb(84, 155, 64); color: white;}
th:nth-child(4) { background: rgb(97, 160, 73); color: white;}
th:nth-child(5) { background: rgb(110, 165, 82); color: white;}
th:nth-child(6) { background: rgb(120, 169, 91); color: white;}

table.agenda tr td:first-child { background: rgb(16, 69, 142); color: white;}

and this is my HTML code:

<table class="agenda">
<thead>
<tr>
<th></th>
<th>August 4</th>
<th>August 5</th>
<th>August 6</th>
<th>August 7</th>
<th>August 8</th>
</tr>
</thead>
<tbody>
<tr>
<td>Morning</td>
<td>Day 1 Morning</td>
<td>Day 2 Morning</td>
<td>Day 3 Morning</td>
<td>Day 4 Morning</td>
<td>Day 5 Morning</td>
</tr>
<tr>
<td>Afternoon</td>
<td>Day 1 Afternoon</td>
<td>Day 2 Afternoon</td>
<td>Day 3 Afternoon</td>
<td>Day 4 Afternoon</td>
<td>Day 5 Afternoon</td>
</tr>
<tr>
<td>Evening</td>
<td>Day 1 Evening</td>
<td>Day 2 Evening</td>
<td>Day 3 Evening</td>
<td>Day 4 Evening</td>
<td>Day 5 Evening</td>
</tr> 
</tbody>
</table>
like image 804
user5692485 Avatar asked Dec 17 '15 20:12

user5692485


People also ask

How do you change the background color of a table row and column?

What to Know. Easiest: add the style property background-color to the table, row, or cell tag. Next easiest: use the attribute bgcolor.

How do I put the background color in a specific cell in a table?

The background color of the table is given by the bgcolor="color" attribute. When applied to the <table> tag, the color fills the background. Cell background colors are set by applying the bgcolor attribute to a <tr> tag (to color the row) or to a <td> tag (to color the cell).

Which CSS property is used to specify the background Colour of a row or a data cell?

The background-color CSS property sets the background color of an element.


2 Answers

You can modify the colors of each cell in the first column without modifying the HTML by combining nth-child on the tr and first-child on the td like this:

table.agenda tr:nth-child(1) td:first-child {
  background: rgb(16, 20, 50);
  color: white;
}

table.agenda tr:nth-child(2) td:first-child {
  background: rgb(16, 69, 150);
  color: white;
}

table.agenda tr:nth-child(3) td:first-child {
  background: rgb(16, 69, 255);
  color: white;
}

Working Live Demo:

table.agenda {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

table.agenda td,
table.agenda th {
  border: 1px solid #fff;
  padding: 8px;
  text-align: center;
}

table.agenda td {
  padding-top: 12px;
  padding-bottom: 12px;
  background-color: rgb(193, 212, 174);
  color: black;
}

th:nth-child(1) {
  background: white;
}

th:nth-child(2) {
  background: rgb(72, 151, 54);
  color: white;
}

th:nth-child(3) {
  background: rgb(84, 155, 64);
  color: white;
}

th:nth-child(4) {
  background: rgb(97, 160, 73);
  color: white;
}

th:nth-child(5) {
  background: rgb(110, 165, 82);
  color: white;
}

th:nth-child(6) {
  background: rgb(120, 169, 91);
  color: white;
}

table.agenda tr:nth-child(1) td:first-child {
  background: rgb(16, 20, 50);
  color: white;
}

table.agenda tr:nth-child(2) td:first-child {
  background: rgb(16, 69, 150);
  color: white;
}

table.agenda tr:nth-child(3) td:first-child {
  background: rgb(16, 69, 255);
  color: white;
}
<table class="agenda">
  <thead>
    <tr>
      <th></th>
      <th>August 4</th>
      <th>August 5</th>
      <th>August 6</th>
      <th>August 7</th>
      <th>August 8</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Morning</td>
      <td>Day 1 Morning</td>
      <td>Day 2 Morning</td>
      <td>Day 3 Morning</td>
      <td>Day 4 Morning</td>
      <td>Day 5 Morning</td>
    </tr>
    <tr>
      <td>Afternoon</td>
      <td>Day 1 Afternoon</td>
      <td>Day 2 Afternoon</td>
      <td>Day 3 Afternoon</td>
      <td>Day 4 Afternoon</td>
      <td>Day 5 Afternoon</td>
    </tr>
    <tr>
      <td>Evening</td>
      <td>Day 1 Evening</td>
      <td>Day 2 Evening</td>
      <td>Day 3 Evening</td>
      <td>Day 4 Evening</td>
      <td>Day 5 Evening</td>
    </tr>
  </tbody>
</table>

JSFiddle Version: https://jsfiddle.net/wyh11L66/1/

like image 118
Maximillian Laumeister Avatar answered Oct 02 '22 15:10

Maximillian Laumeister


for the row you can use this (you must adjust the color, i copied your color from header)

tr:nth-child(1) td:first-child { background: rgb(72, 151, 54); color: white;}
tr:nth-child(2) td:first-child  { background: rgb(72, 151, 54); color: white;}
tr:nth-child(3) td:first-child  { background: rgb(72, 151, 54); color: white;}
like image 36
ScaisEdge Avatar answered Oct 02 '22 16:10

ScaisEdge