Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highlighting all rowspans within a table row using CSS only

Tags:

css

Is there a way of getting the CSS to highlight the entire row, including the cells within the rowspans, rather than just the first line?

As you can see from the example, only the first line of the row is highlighted but not the other cells, this obviously looks quite messy, and I would rather be able to clear it up using CSS only, but will use Javascript if there is no other way.

http://codepen.io/anon/pen/jErMee

HTML

<table class="tb" style="width: 100%;" border="1" cellpadding="10" cellspacing="10">
  <tbody>
    <tr>
      <td>Package</td>

      <td>Includes</td>

      <td>Number of recruits</td>

      <td>Cost per recruit + VAT</td>
    </tr>

    <tr>
      <td rowspan="4">Lorem ipsum dolor sit amet</td>

      <td rowspan="4">Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</td>

      <td>20+</td>

      <td>£105</td>
    </tr>

    <tr>
      <td>10–19</td>

      <td>£120</td>
    </tr>

    <tr>
      <td>6–9</td>

      <td>£135</td>
    </tr>

    <tr>
      <td>1–5</td>

      <td>£150</td>
    </tr>

    <tr>
      <td rowspan="4">Lorem ipsum dolor sit amet</td>

      <td rowspan="4">Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</td>

      <td>20+</td>

      <td>£175</td>
    </tr>

    <tr>
      <td>10–19</td>

      <td>£200</td>
    </tr>

    <tr>
      <td>6–9</td>

      <td>£225</td>
    </tr>

    <tr>
      <td>1–5</td>

      <td>£250</td>
    </tr>

    <tr>
      <td rowspan="2">Lorem ipsum dolor sit amet</td>

      <td rowspan="2">Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</td>

      <td>20+</td>

      <td>£220 (40% payable upfront)</td>
    </tr>

    <tr>
      <td>10–19</td>

      <td>£275 (40% payable upfront)</td>
    </tr>

    <tr>
      <td rowspan="2">Lorem ipsum dolor sit amet</td>

      <td rowspan="2">Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</td>

      <td>20+</td>

      <td>£300 (40% payable upfront)</td>
    </tr>

    <tr>
      <td>10–19</td>

      <td>£375 (40% payable upfront)</td>
    </tr>

    <tr>
      <td>Lorem ipsum dolor sit amet</td>

      <td>Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</td>

      <td><br /></td>

      <td><br /></td>
    </tr>
  </tbody>
</table>

CSS:

body {
  padding: 50px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

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


tr:hover td {
    background-color: blue;
}
like image 824
G Fo Avatar asked Dec 17 '14 14:12

G Fo


People also ask

How do I highlight a row in a table CSS?

Highlight Table Row using CSS. You can use CSS without any javascript to make the row of a table highlight on hover. All it requires is that the use the pseudo class :hover to add the effect to whatever html element you choose.

How do you highlight rows in a table?

To select an entire table, click in the table, and then click the Table Move Handle in the upper-left corner. To select a row, column, cell, or group of cells, click and drag your mouse pointer to highlight the cells you want.

How do I highlight a row in a table in HTML?

When you create Web pages in HTML, you can use JavaScript functions to alter the appearance of page elements on user interaction. To highlight certain rows in a table, you can set Cascading Style Sheet declarations for these rows in their normal state and in their highlighted state.

How do I highlight a cell in an HTML table?

How do you highlight cells in an HTML table by letting the user select them -- just like you would do in Excel? You can make use of JavaScript to do that. To save the selected cell positions, use localStorage.


1 Answers

There is a JSBin.

I omit your part code, just for easily display.

The key of the issue is about <tbody>. A table must have a <thead>,but can have many <tbody>. So, use these code:

tbody:hover .td {
    background-color: blue;
}
like image 51
Todd Mark Avatar answered Nov 18 '22 17:11

Todd Mark