Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show div only when printing

Let's say that I have

<div id="printOnly">
     <b>Title</b>
     <p>
        Printing content 
     </p>
</div>

Is it possible to hide this div when page rendering and to show only when printing this div?

like image 602
user1765862 Avatar asked Jun 22 '15 07:06

user1765862


People also ask

How do I hide the elements when printing?

To hide the element, add “display:none” to the element with CSS.

How do I print a certain div on my website?

To print the content of div in JavaScript, first store the content of div in a JavaScript variable and then the print button is clicked. The contents of the HTML div element to be extracted.

How do I print a specific area in HTML?

You can use this function for print a specific area of web page or full web page content. Use printPageArea() function on onclick event of print button element and provide the content area div ID which you want to print.


4 Answers

You need some css for that

#printOnly {
   display : none;
}

@media print {
    #printOnly {
       display : block;
    }
}
like image 132
Nikhil Aggarwal Avatar answered Oct 04 '22 14:10

Nikhil Aggarwal


You should use media query.

In your case:

#printOnly {
    display: none;
}

@media print { 
    #printOnly {
        display: block;
    }
}

PS take a look here http://www.joshuawinn.com/css-print-media-query/ for browser support

like image 24
alberto-bottarini Avatar answered Oct 04 '22 15:10

alberto-bottarini


@media screen
{
    #printOnly{display:none;}
}

@media print
{
    #printOnly{}
}
like image 31
Kiran Dash Avatar answered Oct 04 '22 13:10

Kiran Dash


You can attach external css stylesheet with media="print" attribute:

<link rel="stylesheet" type="text/css" media="print" href="print.css">
like image 25
hsz Avatar answered Oct 04 '22 14:10

hsz