Set Page Break Behavior After an Element with JavaScript



Use the pageBreakAfter property in JavaScript to set the page-break behavior after an element. Use the always property for page after an element.

Note − The changes would be visible while printing or viewing the print preview.

Example

You can try to run the following code to return the page-break behavior after an element with JavaScript −

<!DOCTYPE html>
<html>
   <body>
      <h1>Heading 1</h1>

      <p>This is demo text.</p>
      <p>This is demo text.</p>
      <p id = "myFooter">This if footer text.</p>

      <button type = "button" onclick="display()">Set page-break</button>

      <script>
         function display() {
            document.getElementById("myFooter").style.pageBreakAfter = "always";
         }
      </script>
   </body>
</html>
Updated on: 2020-06-23T12:36:09+05:30

786 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements