CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



page-break-inside

The page-break-inside property is a CSS property that allows you to control how page breaks occur inside an element. This property is particularly useful when you are working with long documents that need to be printed or displayed on multiple pages.

When you use the page-break-inside property, you can specify whether an element should be allowed to break across multiple pages or whether it should be kept together on a single page. This can help you to ensure that your content is presented in a clear and organized way, without any awkward page breaks or other formatting issues.

How Does Page-Break-Inside Work?

The page-break-inside property works by allowing you to specify one of two values: auto or avoid. When you set the value to auto, the browser will determine whether or not to allow the element to break across multiple pages based on its own internal algorithms.

On the other hand, when you set the value to avoid, the browser will do everything it can to keep the element together on a single page. This can be particularly useful when you are working with elements that contain important information that should not be split up across multiple pages.

Code Examples

Here are some examples of how you can use the page-break-inside property in your CSS code:

<style>
  /* Allow elements to break across multiple pages */
  .breakable {
    page-break-inside: auto;
  }

  /* Keep elements together on a single page */
  .unbreakable {
    page-break-inside: avoid;
  }
</style>

<div class="breakable">
  <p>This element can break across multiple pages if necessary.</p>
</div>

<div class="unbreakable">
  <p>This element will be kept together on a single page if possible.</p>
</div>

In this example, we have defined two CSS classes: .breakable and .unbreakable. The .breakable class allows elements to break across multiple pages if necessary, while the .unbreakable class keeps elements together on a single page if possible.

We then use these classes to define two <div> elements, each containing a <p> element. The first <div> has the .breakable class, while the second <div> has the .unbreakable class.

When we view this code in a browser, we can see that the first <div> element can break across multiple pages if necessary, while the second <div> element will be kept together on a single page if possible.

Conclusion

The page-break-inside property is a useful tool for controlling how page breaks occur inside an element. By using this property, you can ensure that your content is presented in a clear and organized way, without any awkward page breaks or other formatting issues.

Whether you are working with long documents that need to be printed or displayed on multiple pages, or you simply want to ensure that your content is presented in the best possible way, the page-break-inside property can help you to achieve your goals.

References

Activity