CSS Properties
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.
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
Value | Description |
---|---|
auto | Default. Allows page breaks to be inserted inside the element. |
avoid | Avoids 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS page-break-inside Property), please comment here. I will help you immediately.