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?
To hide the element, add “display:none” to the element with CSS.
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.
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.
You need some css for that
#printOnly {
display : none;
}
@media print {
#printOnly {
display : block;
}
}
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
@media screen
{
#printOnly{display:none;}
}
@media print
{
#printOnly{}
}
You can attach external css stylesheet with media="print"
attribute:
<link rel="stylesheet" type="text/css" media="print" href="print.css">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With