Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS page-break-inside Property

Posted in CSS Tutorial
Updated on Jul 21, 2024
By Mari Selvan
👁️ 22 - Views
⏳ 4 mins
💬 1 Comment
CSS page-break-inside Property

Photo Credit to CodeToFun

🙋 Introduction

The page-break-inside property in CSS is used to control the behavior of page breaks inside an element when printing a document.

This property is particularly useful for ensuring that content such as tables or images are not split across pages, providing a more readable printed document.

💡 Syntax

The syntax for the page-break-inside property is simple and can be applied to any block-level element.

Syntax
Copied
Copy To Clipboard
element {
  page-break-inside: value;
}

🎛️ Default Value

The default value of the page-break-inside property is auto, which allows the browser to automatically insert page breaks inside the element as needed.

🏠 Property Values

ValueDescription
autoDefault. Allows page breaks to be inserted inside the element.
avoidAvoids inserting a page break inside the element, keeping the content together on the same page if possible.

📄 Example

In this example, we'll prevent a table from being split across two pages when printed.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS page-break-inside Example</title>
  <style>
    table {
      page-break-inside: avoid;
      width: 100%;
      border-collapse: collapse;
    }
    th, td {
      border: 1px solid black;
      padding: 8px;
      text-align: left;
    }
  </style>
</head>
<body>
  <h1>Table with Page Break Avoidance</h1>
  <table>
    <thead>
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Row 1, Cell 1</td>
        <td>Row 1, Cell 2</td>
      </tr>
      <tr>
        <td>Row 2, Cell 1</td>
        <td>Row 2, Cell 2</td>
      </tr>
      <!-- Add more rows as needed -->
    </tbody>
  </table>
</body>
</html>

🖥️ Browser Compatibility

The page-break-inside property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your printed documents across different browsers to ensure consistent behavior.

🎉 Conclusion

The page-break-inside property is a valuable tool for web developers who need precise control over how content is printed.

By using this property, you can ensure that important elements like tables and images are not awkwardly split across pages, resulting in more professional and readable printed documents. Experiment with this property to improve the print layout of your web projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy