HTML DOM Style pageBreakAfter Property


The HTML DOM Style pageBreakAfter property returns and modify the page-break behavior for printing or print preview after an HTML element in an HTML document.

Syntax

Following is the syntax −

1. Returning pageBreakAfter

object.pageBreakAfter

2. Modifying pageBreakAfter

object.pageBreakAfter = “value”

Here value can be −

ValueExplanation
initialIt set this property value to its default value.
inheritIt inherits this property value from its parent element.
autoIt insert a page break before the element in an HTML document if necessary.
alwaysIt always insert a page break before the element in an HTML document.
avoidIt avoid a page break before the element in an HTML document
leftIn it the next page can be considered as a left page when one or two page breaks are inserted before the element.
rightIn it the next page can be considered as a right page when one or two page breaks are inserted before the element.
Empty string(“”)It doesn’t insert a page break before the element in an HTML document

Let us see an example of HTML DOM Style pageBreakAfter Property −

Example

 Live Demo

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
      background-color: #8BC6EC;
      background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
   p {
      text-align: center;
   }
</style>
<body>
<h1 style="text-align:center">DOM Style pageBreakAfter Property Demo</h1>
<p>
Hi! I'm a para element with some dummy text. Hi! I'm a para element with some dummy text.
</p>
<p class="page-break">
Hi! I'm second para element with some dummy text. Hi! I'm a second element with some dummy text.
</p>
<button onclick="set()" class="btn">Break Page Here</button>
<p>
Hi! I'm another para element with some dummy text. Hi! I'm another para element with some dummy text.
</p>
<script>
   function set() {
      document.querySelector(".page-break").style.pageBreakAfter = "always";
   }
</script>
</body>
</html>

Output

Now open print preview and observe how our html page would be displayed.

Now, click on the “red” button available on the web page and then again open the print preview to observe our webpage again. Here you can clearly see our web page gets split into two pages −

Updated on: 01-Oct-2019

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements