打印时防止 HTML 元素跨越多个页面

发布于 2024-10-12 20:12:28 字数 2108 浏览 5 评论 0原文

我有以下 HTML,我希望在跨多个页面时不被破坏。问题是,如果我在之前或之后使用分页符,它将把每个元素放在它自己的页面上。我遇到的另一个问题是,如果我在 cell CSS 类或 wrap 类上设置 display: block,则 DIV 或LI还是分手了。我有一个印刷媒体 CSS 文件和一个屏幕 CSS 文件。我想防止

  • 元素及其内容被破坏。
  •         <div class="pad">
            <h1 style="text-align: center; margin: 10px 0">
    
                Work Orders for Jan 05, 2011
            </h1>
            <p class="printHidden">
                <a href="/orders/print-all/date/2011-01-05">Print All Work Orders</a>
            </p>
            <ul class="workorders">                    
                <li class="cell">
                    <div class="wrap" id="146">
    
                        <div class="scheduled">
                            <p>
                                <strong>Work Order:</strong> <a href="/orders/view/work-order/146">158801</a>
                            </p>
                            <p>
                                <strong>Client:</strong> Client Name
                            </p><br>
    
                            <b>Resources</b>
                            <ul>
                                <li>
                                    <a href="/resources/view/resource-id/5" id="Person-5">Mikell McLaindon</a>
                                </li>
                                <li>
                                    <a href="/resources/view/resource-id/9" id="Person-9">Jose Copper</a>
    
                                </li>
                            </ul>
                        </div>
                        <div class="unschedule printHidden">
                            <h1 style="text-align: center; margin: 10px 0 10px; font-size: 12px; font-weight: bold;">
                                Unschedule Resource for 15880-PW
                            </h1>
                        </div>
                    </div>
                </li>
                {... removed for brevity ...}
    

    I have the following HTML that I would like to keep from being broken up when spanning multiple pages. The problem is that if I use page-break-before or after, it will put each element on it's own page. The other problem I'm having is that if I set display: block on either the cell CSS class or the wrap class, the DIV or LI still gets broken up. I have a print media CSS file and a CSS file for the screen as well. I want to keep the <li class="cell"> element and its contents from being broken up.

            <div class="pad">
            <h1 style="text-align: center; margin: 10px 0">
    
                Work Orders for Jan 05, 2011
            </h1>
            <p class="printHidden">
                <a href="/orders/print-all/date/2011-01-05">Print All Work Orders</a>
            </p>
            <ul class="workorders">                    
                <li class="cell">
                    <div class="wrap" id="146">
    
                        <div class="scheduled">
                            <p>
                                <strong>Work Order:</strong> <a href="/orders/view/work-order/146">158801</a>
                            </p>
                            <p>
                                <strong>Client:</strong> Client Name
                            </p><br>
    
                            <b>Resources</b>
                            <ul>
                                <li>
                                    <a href="/resources/view/resource-id/5" id="Person-5">Mikell McLaindon</a>
                                </li>
                                <li>
                                    <a href="/resources/view/resource-id/9" id="Person-9">Jose Copper</a>
    
                                </li>
                            </ul>
                        </div>
                        <div class="unschedule printHidden">
                            <h1 style="text-align: center; margin: 10px 0 10px; font-size: 12px; font-weight: bold;">
                                Unschedule Resource for 15880-PW
                            </h1>
                        </div>
                    </div>
                </li>
                {... removed for brevity ...}
    

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    祁梦 2024-10-19 20:12:28

    编辑:正确的解决方案

    请参阅:http://www.w3schools.com/ css/pr_print_pagebi.asp
    因此,如果您将以下内容添加到 CSS 中,它应该可以解决您的问题:

    @media print
    {
        div.pad { page-break-inside:avoid; }
    }
    

    所有主要的桌面浏览器现在都支持此功能。

    不过,还有 page-break-after:avoidpage-break-before:avoid,您可以将它们添加到中的每个元素。 pad,以便在某些旧版本的浏览器中产生相同的结果。

    @media print
    {
        div.pad * { 
            page-break-after:avoid;
            page-break-before:avoid;
        }
    }
    

    http://www.w3schools.com/Css/pr_print_pagebb.asp

    http://www.w3schools.com/css/pr_print_pageba.asp

    旧答案:

    好像您正在尝试将不适合页面的内容放入页面中。我会尝试添加一个带有 media="print"... 的样式表

    <link rel="stylesheet" href="css/print.css" type="text/css" media="print" charset="utf-8" />
    

    ,以减少所有这些元素的字体大小、填充等,以便您可以在此之前添加分页符部分并使其适合页面。

    EDIT: PROPER SOLUTION

    See: http://www.w3schools.com/css/pr_print_pagebi.asp
    So if you add the following to your CSS it should fix your problem:

    @media print
    {
        div.pad { page-break-inside:avoid; }
    }
    

    All the major desktop browsers now support this.

    However, there is also page-break-after:avoid and page-break-before:avoid which you can add to every element within the .pad class in order to produce the same result in some older versions of browsers.

    @media print
    {
        div.pad * { 
            page-break-after:avoid;
            page-break-before:avoid;
        }
    }
    

    http://www.w3schools.com/Css/pr_print_pagebb.asp

    http://www.w3schools.com/css/pr_print_pageba.asp

    OLD ANSWER:

    It sounds like you're trying to fit something on a page that doesn't fit on a page. I would try adding a stylesheet with media="print"...

    <link rel="stylesheet" href="css/print.css" type="text/css" media="print" charset="utf-8" />
    

    ...that reduces the font size, padding, etc. of all of these elements so that you CAN put a page break before this section and have it fit on the page.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文