0

I want to print a table in chrome. The styles applied on table header are not being applied if a single row goes beyond the page and the header is repeated for it.

I have a very large value for the row and which made it go beyond a page and repeat the header in next page which doesn't have styles applied properly.

Reference image: here background color is not applied for the next page header enter image description here

code url: https://drive.google.com/file/d/1iAKsBQ82J24r0TJaR9pMiFOtBNax7idq/view?usp=sharing

code:

<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
th {
  border: 1px solid #dddddd;
  text-align: left;
  color: red;
}
thead{
 color: red;
  background-color: blue;
}
tr:nth-child(odd) {
  background-color: #dddddd;
}
@media print{
thead{
 color: red !important;
  background-color: blue !important;
}
}
</style>
</head>
<body>

<h2>HTML Table</h2>

<table>
  <thead>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </thead>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma
    </td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

</body>
</html>
Sashi yadav
  • 308
  • 4
  • 16

1 Answers1

0

Yep, that's a Webkit/Chrome thing. It won't print the thead on each page. What you could do to achieve something like this is use page-break.

Page break does only work on block elements, so you should style your tr's accordingly.

tbody tr.head {
    page-break-before: always;
    page-break-inside: avoid;
}

Refer here.

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td,
th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

th {
    border: 1px solid #dddddd;
    text-align: left;
    color: red;
    background-color: blue !important;
}

thead {
    color: red;
    background-color: blue !important;
}


tr:nth-child(odd) {
    background-color: #dddddd;
}
tbody tr.head {
    page-break-before: always;
    page-break-inside: avoid;
}
@media screen {
    tbody .head {
        display: none;
    }
    
    thead {
        color: red !important;
        background-color: blue !important;
    }
}
@media print {
    body {
        -webkit-print-color-adjust: exact;
    }
}
<h2>HTML Table</h2>

<table>
  <thead>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </thead>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma
    </td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>
Saravana
  • 3,389
  • 4
  • 21
  • 37