Thursday, September 22, 2005

CSS: Hide blocks of a webpage when printing

When printing a webpage, how do you hide certain elements of the page?

There is no onprint action in Javascript that I am aware of.

The key is to use css:

Note: In css '#' denotes an ID selector, '.' denotes a class selector.
<style type="text/css">
@media print
{
div.headerbox{ display:none; }
}
</style>

<
div class="headerbox">...
hidden when printed</div>
<
div class="otherbox">...
not hidden when printed</div>

or

<style type="text/css">
@media print
{
div#headerbox{ display:none; }
}
</style>

<
div id="headerbox">... hidden when printed</div>
<
div id="otherbox">... not hidden when printed</div>

No comments: