Sergey Korotenko
Sergey Korotenko

Reputation: 15

Table. How to save background color of col using colSpan?

Is it possible to save col background color using colpan? In example below I want to achive yellow color in space for last col even if I use colspan="3".

Please, take a look to picture above, I want to achive this result using colspan="3"

Solution example

Wrong solution example

Thanks!

table,
th,
td {
  border: 1px solid black;
}
<table>
  <colgroup>
    <col span="2" style="background-color:#E6E6DC">
      <col style="background-color:yellow">
  </colgroup>
  <tr>
    <th>ISBN</th>
    <th>Title</th>
    <th>Price</th>
  </tr>
  <tr>
    <td colspan="3">Error descriotion, last col should be yellow</td>
  </tr>
  <tr>
    <td>5869207</td>
    <td>My first CSS</td>
    <td>$49</td>
  </tr>
</table>

Upvotes: 0

Views: 4671

Answers (3)

zer00ne
zer00ne

Reputation: 43910

UPDATE

OP didn't make sense:

  • I want to achieve yellow color in space for last col even if I use colspan="3"

  • Ok, I chopped up that large 3 rowspan and now the last column is yellow.

  • Ok, kept middle row at colspan='3', and used <mark> in order to hold the styles within the <td>. See updated Snippet.

SNIPPET

<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    th,
    td {
      border: 1px solid black;
    }
  </style>
</head>

<body>

  <table style='border-collapse:collapse;table-layout:fixed;width:325px;'>
    <colgroup>
      <col span="2" style="background-color:red">
        <col span='1' style="background-color:yellow">
    </colgroup>
    <tr>
      <th>ISBN</th>
      <th>Title</th>
      <th>Price</th>
    </tr>
    <tr>
      <td colspan='3' style='padding:0'>Error description, last col should 
        <mark style='line-height:1.2;width:12.25ex;border-right:1.75px solid yellow;border-left:3.75px solid yellow;display:inline-block;padding:0 12px 0 2px;position:relative; left: 3px;'>&nbsp;be yellow</mark>
      </td>

    </tr>
    <tr>
      <td>5869207</td>
      <td>My first CSS</td>
      <td>$49</td>
    </tr>
  </table>

</body>

</html>

Upvotes: 1

Liz
Liz

Reputation: 575

Try adding a class to col and apply the same style for the colspan.

table, th, td {
  border: 1px solid black;
}

table col.col-color, table td[colspan="3"]{
  background-color: yellow;
}

<table>
   <colgroup>
       <col span="2" style="background-color:red">
       <col class="col-color">
   </colgroup>
   <tr>
       <th>ISBN</th>
       <th>Title</th>
       <th>Price</th>
   </tr>
   <tr>
        <td colspan="3">Error descriotion, last col should be yellow</td>
   </tr>
   <tr>
        <td>5869207</td>
        <td>My first CSS</td>
        <td>$49</td>
   </tr>
</table>

Upvotes: 0

Oleksii Dniprovskyi
Oleksii Dniprovskyi

Reputation: 135

You can just add an "id" to your td colspan,

<td colspan="3" id="your id here">Error descriotion, last col should be yellow</td>

and add this to css

#your id here{ background-color: yellow; }

Upvotes: 1

Related Questions