Click here to Skip to main content
Licence CPOL
First Posted 28 Feb 2006
Views 24,513
Bookmarked 18 times

Print a doument with CSS

By | 28 Feb 2006 | Article
How to hide controls when printing a document.

Introduction

What do we do when we need to print a note? Perhaps you open a new pop up with the same template as your current document, but the pop up has no buttons, images, bars, and other stuff. When you open the new window, you hit the database again (if you have one), which means more steps that you need to do. We'll see how to avoid that in good shape.

Let us suppose that we have the following table:

<table>
  <tr>
    <td>Navigate</td>
    <td>Content</td>
    <td><input type="button" value="ok"></td>
  </tr>
</table>

Now we want to print only the content cell and hide the other ones. I will make a new style CSS for this task:

<style media="screen">
  .noPrint{ display: block; }
  .yesPrint{ display: block !important; }
</style> 
<style media="print">
  .noPrint{ display: none; }
  .yesPrint{ display: block !important; }
</style>

Now I just need to add this class to my controls or table cell (and also row):

<table>
  <tr>
    <td class="noPrint">Navigate</td>
    <td class="yesPrint">Content</td>
    <td>My button is hidden <input type="button" value="ok" class="noPrint"></td>
  </tr>
</table>

As you can see, you can also add your CSS class into a button. Open this file in your browser. We will see Navigate, Content and the button in there. Now go to PrintPreview in your browser and see what happens. The Content and button are gone (hidden), and we can set the button to print.

<table>
  <tr>
    <td class="noPrint">Navigate</td>
    <td class="yesPrint">Content</td>
    <td>My button is hidden <input type="button" 
          value="Print" class="noPrint" onclick="window.print();"></td>
  </tr>
</table>

Run your page and click Print. That's all...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

benjarras

Web Developer
Softtek GDC Ensenada
Mexico Mexico

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to Print With No Prompt Pinmembersamitcom0:04 28 Sep '06  
AnswerRe: How to Print With No Prompt Pinmemberbenjarras5:51 28 Sep '06  
AnswerRe: How to Print With No Prompt Pinmemberjeyaraman18:49 26 Dec '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 28 Feb 2006
Article Copyright 2006 by benjarras
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid