HTML onbeforeprint Event Attribute

The HTML onbeforeprint event attribute is triggered when a page is about to be printed or before the print dialog box appears in the HTML document.


Following is the syntax −

<tagname onbeforeprint=”script”></tagname>

Let us see an example of HTML onbeforeprint event Attribute−


 Live Demo

<!DOCTYPE html>
   body {
      color: #000;
      height: 100vh;
      background-color: #FBAB7E;
      background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
      text-align: center;
      padding: 20px;
   p {
      font-size: 1.1rem;
<body onbeforeprint="get()">
<h1>HTML onbeforeprint Event Attribute Demo</h1>
<p>I'm a paragraph HTML element with some dummy text.</p>
<p style="color:#db133a;">Now try to print this document.</p>
<div class="show"></div>
   function get() { = "lightblue";


Now try to print the document by using ctrl + p and observe how onbeforeprint event attribute works.