Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS page-break-after Property

Posted in CSS Tutorial
Updated on Oct 07, 2024
By Mari Selvan
👁️ 42 - Views
⏳ 4 mins
💬 1 Comment
CSS page-break-after Property

Photo Credit to CodeToFun

🙋 Introduction

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

This property is particularly useful for creating printable documents where control over page breaks is necessary, such as reports, invoices, or other formatted documents.

💡 Syntax

The syntax for the page-break-after property is straightforward. It can be applied to block-level elements, and it takes one of several keyword values.

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

🎛️ Default Value

The default value of the page-break-after property is auto, which means that the browser will automatically insert a page break if necessary.

🏠 Property Values

ValueDescription
autoAllows the browser to automatically insert a page break if needed.
alwaysAlways insert a page break after the element.
avoidAvoid inserting a page break after the element.
leftInsert one or two page breaks after the element so that the next page is formatted as a left page.
rightInsert one or two page breaks after the element so that the next page is formatted as a right page.
initialSets the property to its default value (auto).
inheritInherits the property from its parent element.

📄 Example

In this example, we'll ensure that a page break always occurs after a heading when printing the document.

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-after Example</title>
  <style>
    @media print {
      h1 {
        page-break-after: always;
      }
    }
  </style>
</head>
<body>
  <h1>Chapter 1</h1>
  <p>This is the first chapter.</p>
  
  <h1>Chapter 2</h1>
  <p>This is the second chapter.</p>
</body>
</html>

🖥️ Browser Compatibility

The page-break-after property is widely supported in modern browsers. It works reliably in browsers such as Chrome, Firefox, Safari, Edge, and Opera. It's important to note that this property is primarily used for print styles, so testing should be done by previewing the print version of your document in these browsers.

🎉 Conclusion

The page-break-after property is an essential tool for creating well-formatted, printable documents.

By controlling where page breaks occur after specific elements, you can ensure that your printed content appears organized and professional. Experiment with different values to achieve the desired layout for your print media.

👨‍💻 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