Monday, July 13, 2009

Hide an html element while printing the web page.

We may encounter certain scenarios where we may have to hide some part of our web page from printing.
Eg: A printer icon shown in a print preview page. Or a price column in the shopping cart etc.

we can use css for hiding the html elements while printing.

1. Add the following css class to your stylesheet.

@media print {
.printHide {
display:none;
}
}


2. Add the style class, printHide for the elements which you want to hide.

The following eg code will hide the link "print" while printing the page.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<style>
@media print {
.printHide {
display:none;
}
}
</style>
</HEAD>

<BODY>
<a href="javascript:void(0);" onclick="window.print();"
class="printHide">Print This Page</a>

<p>
Text Text Text Text Text
Text Text Text Text Text
</p>

<p>
Text Text Text Text Text
Text Text Text Text Text
</p>

</BODY>
</HTML>