0

I am not sure why my table header is repeating on every page after printing HTML Code. I just want it on first page so how can I do it?

enter image description here

I wanted it to display only when the table starts

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Can't you just remove it from the other pages? – Steve Tomlin Mar 27 '23 at 09:51
  • @SteveTomlin The question is regarding *printed* pages, where the table header is automatically included by the browser on all pages spanned by the table itself. I thought it might be possible to use [`@page` and `:first`](https://developer.mozilla.org/en-US/docs/Web/CSS/:first) to target the styles only on the first page, but `:first` cannot be used to set display, visibility, etc. – Sampson Mar 27 '23 at 10:24
  • Ah I see what you mean. I was confusing 'pages' with browser pages. I don't think css has any concept of printed pages. A possible option for you is to create 2 tables, (one hidden in the browser with the table head, and the other without) – Steve Tomlin Mar 27 '23 at 10:47
  • @SteveTomlin CSS has some nice controls over printed pages (see `@media print` and `@page` for starters), but not nearly as robust as this issue would require. – Sampson Mar 27 '23 at 10:58

2 Answers2

0

What you're observing is the intentional behavior of modern browsers. In fact, for quite some time users here on Stack Overflow would ask how to achieve this behavior, prior to it seeing broad support in browsers.

My suggestion would be to leave this behavior as-is, since it provides important context across page-breaks. After all, if you provide 6 pages of tabular data, and page 3 is reviewed on its own, how will the handler know what data is represented by the values unless there is a header to explain the information?

If you feel strongly that the repeated header is not needed, then there may be another approach you can take. You could show the thead for web-viewers, and replace it with a single tr (containing the same information) for printed viewers. For example:

<table>
    <thead>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
    </thead>
    <tbody>
        <tr class="faux-header">
            <th>Header 1</th>
            <th>Header 1</th>
        </tr>
        <tr>
            <td>Row 1, Cell 1</td>
            <td>Row 1, Cell 2</td>
        </tr>
    </tbody>
</table>

You wouldn't want the faux-header visible by default, and you wouldn't want the thead to be visible in a printed context:

@media screen {
    .faux-header {
        display: none;
    }

}

@media print {
    table:has(.faux-header) thead {
        display: none;
    }
}
Sampson
  • 265,109
  • 74
  • 539
  • 565
0

use td instead of th tag for quick fix like <td>Preventive Care</td>

Rahul Thakur
  • 126
  • 1
  • 5
  • I would discourage this approach for several reasons. First, it doesn't work. Browsers will still repeat the `THEAD` element across pages. Secondly, it conflates `TD` and `TH` elements, which are distinctly different, and should not be confused. Third, this approach wouldn't explicitly communicate the author's intention to hide `THEAD` elements after the first page. – Sampson Mar 27 '23 at 11:14