Prevent Page Break between 2 div elements

只愿长相守 提交于 2019-12-05 10:04:49

You mean how to prevent a page-break when printing?

#c{
    page-break-after: avoid;
}

#d {
    page-break-before: avoid;
}

Note that the 'new' way of doing it would be using the generic break-before and break-after like this:

#c{
    break-after: avoid-page;
}

#d {
    break-before: avoid-page;
}

But this is not supported in all browsers yet. See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/break-after

I came here, with the same problem. And I found a solution!

My Situation:

  • I have a div that serves as a heading
  • then another div that contains a very long pre
  • that pre extends across multiple pages

Example:

<div>Heading</div>
<div>
    <pre>Very long pre.</pre>
</div>

The Problem:

  • My Heading Div appears on page 1
  • The pre starts on page 2 - so HUGE gap between the "heading" and content.

The Solution:

Finally, I tried making my pre style display: inline. Tada! Now my heading div and the beginning of the pre start on page 1 together!

Perhaps you can mess with the display of your elements to control this better. Hope this helps whoever may stumble here again!

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