Print Specific Part of Web Page

The developers often allow printing only the specific portion of a web page’s content, such as any payment plans or a piece of particular information. Normally, the traditional method to print can be used for printing, like the print command. However, it does not allow you to print the particular content unless you use a Screenshot tool to capture and print. This approach can be helpful, however, it decreases the readability of the text.

This tutorial will illustrate the procedure to print a particular part of a web page using JavaScript.

How to Print a Specific Part of a Web Page?

To print the specific part of the web page with high quality:

  • Use the “getElementById()” method to get the reference of the content element.
  • Then, utilize the “” method, which opens the print window with the specified size.
  • window.document.write” writes the text in the window.
  • window.document.close” closes the document.
  • window.focus()” sets the focus on the print window’s content.
  • Finally, use the “window.print()” for printing the content.

In an HTML file, first, create a div in a web page with some content:

<div id="divPrint">
 <p>Linuxhint is the best website for learning skills. <br>
 It provides multiple Programming Languages to learn skills such as </p>
 <ul class="centerBullets">

Create a print button outside the div and attach an “onclick” event with it that calls the function “print()” while it is clicked:

 <button id="print" onclick="print()">Print</button><br><br>

After executing the above-given code, the web page will look like as follows:

Now, in the JavaScript file, or the tag, add the given lines of code:

function print () {
 var printDiv = document.getElementById("divPrint");
 var printWindow ='', '', 'left=0, top=0, width=800, height=500, toolbar=0, scrollbars=0, status=0');

In the above code snippet:

  • First, define the “print()” method that is invoked when the “onclick” event is triggered.
  • Get the element (div) that you want to print by passing its assigned id to the “getElementById()” method.
  • Call the “” method and pass the window size according to your requirement.
  • Pass the reference of the div element to the “window.document.write()” method with the “innerHTML” property to write the content on the print window.
  • Then, close the document using the “window.close()” method.
  • Set focus on the print window using the “window.focus()” method.
  • Finally, call the “print” method of the window object to print the specified content.


That’s all about printing the specified part of the web page.


To print the specific part of the web page, use the “getElementById()” method to get the reference of the element of the content. Then, use the “window’s” methods, including “”, “window.document.write”, “window.document.close”, “window.focus()”, and “window.print()”. In this tutorial, we illustrated the procedure for printing a particular part of a web page using JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.