Keep contents of a div together for printing in IE8

泄露秘密 提交于 2019-12-22 05:11:49

问题


Given the following HTML document, I need to keep the "Table title" line on the same page as the <table> when being printed in IE8.

Despite the page-break-inside:avoid;, there is still a page break between the title and the table. My understanding of this suggests a page break should be avoided and the whole div pushed on to page 2.

The doctype is XHTML 1.0 Transitional, I have <meta http-equiv="X-UA-Compatible" content="IE=8" /> set to force IE8 into Standards Mode which supposedly supports this syntax, and I have verified the rendering is being done in standards mode by checking document.compatMode == "CSS1Compat". The XHTML is valid.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=8" /> 
  <title>Page title</title>
</head>
  <body>
    <h1>Page content</h1>
    this is some content
    <br />which<br />should<br />push<br />the<br />table<br />below<br />on<br />to<br />the<br />next<br />page<br />but<br />the<br />table<br />should<br />be<br />kept<br />together<br />if<br />at<br />all<br />possible<br />please!

    <div style="page-break-inside:avoid;">
      <p><strong>Table title which needs to be kept with the table</strong></p>
      <table>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
        <tr><td>one</td><td>two</td><td>three</td></tr>
      </table>
    </div>
  </body>
</html>

回答1:


IE8 support for this feature is buggy.

Although Internet Explorer for Windows version 8 supports the value avoid it is a little buggy in places. For example, if applied to a p element, the browser will try to avoid breaking the page inside the element as expected; but if applied to a ul element, the whole list is not set to avoid as the list may span the two pages. That said, the individual list element will try to avoid having a page break inside.

From this example, we can conclude it will try to not break the page inside the p, nor inside the table, but it won't combine both. You can test it by having a really long text in your <p>.

A way to fix this would be to include your title in your table :

  <table style="page-break-inside:avoid;" border="1">
    <tr><th colspan="3">Table title which needs to be kept with the table</th></tr>
    <tr><td>one</td><td>two</td><td>three</td></tr>
    <tr><td>one</td><td>two</td><td>three</td></tr>
    [...]



回答2:


page-break only works on a paged media context, like @media print:

@media print {
  tr {
    page-break-inside:avoid;
  }
}



回答3:


Try to use <th> and add title

for example

<table>
    <tr>
        <th>your title</th>
    </tr>
    <tr>
        <td>one</td>
        <td>two</td>
        <td>three</td>
    </tr>
</table>

CSS use @ media

@media print {
    tr th {
    page-break-inside:avoid;
    }
}

Recently i have created print version HTML it works fine! hope it helps you!



来源:https://stackoverflow.com/questions/6493474/keep-contents-of-a-div-together-for-printing-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!