"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Prevent DIVs from Being Split Across Pages When Printing?

How to Prevent DIVs from Being Split Across Pages When Printing?

Published on 2024-11-07
Browse:513

How to Prevent DIVs from Being Split Across Pages When Printing?

Print Issue: Preventing DIV Bifurcation Across Pages

Encountering a printing dilemma where dynamic DIVs are getting sliced in half between pages? This issue arises when trying to print lengthy documents with numerous DIV elements of variable height.

CSS Solution to the Rescue

To address this issue, the CSS property break-inside comes to the rescue. By specifying the value avoid, you can ensure that the rendering engine prevents DIVs from being split mid-way. Here's the code snippet:

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

This solution is supported by all major browsers, including:

  • Chrome 50
  • Edge 12
  • Firefox 65
  • Opera 37
  • Safari 10

Alternative Option

Another viable option is to use page-break-inside: avoid; instead of break-inside: avoid;. However, this approach has been deprecated and should be used with caution.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3