Easy Print CSS Integration - NoPrint (Thanks Rick Osborne!)

Posted December 31, 2007 at 8:23 AM by Ben Nadel

Tags: HTML / CSS

Last week, I talked about a little bit about a revelation that I had as far as print CSS vs. printer friendly pages go. I had said that print CSS was optimized to make haphazard printing better and printer friendly pages were optimized for purposeful printing. In response to this, Rick Osborne had some very good comments. For starters, he made the point that you want to build print CSS into your site as you are building the site; this way, it is just well thought out. You don't want to go back after a site has been built and then try to apply a print style sheet.

That's all good, but really, it was his next piece of advice that was quite fantastic:

"noprint"

He suggested that an easy way of integrating print CSS was to simply add the class "noprint" to the elements that you did not want to print. Then, a major part of your print style sheet will simply be:

  • .noprint {
  • display: none ;
  • }

Of course, there's more to a print style sheet than simply hiding elements, but this does have a lot to do with it. What makes this so insanely easy is that modern browsers can all handle multiple class definitions, so appending the "noprint" class doesn't mean that you have to sacrifice other classes in any way:

  • <div class="site-header noprint">
  • ....
  • </div>

Anyway, I thought this was a stroke of genius. And, as a final point, what makes this so awesome, is that as you build or modify your page architecture, there is so much less work to maintain the print CSS for it.

Rick Osborne, as always, your advice is most excellent!


You Might Also Be Interested In:



Reader Comments

Dec 31, 2007 at 11:46 AM // reply »
37 Comments

In addition to the "noprint" class, you can create a "noshow" class and allow specific information (copyright, URL, instructions, etc) to be printed but not readily viewable.

I use both classes in my single CSS file like this:

/* Screen Only */
@media screen {
.noprint {display:block !important;}
.noshow {display:none !important;}
}

/* Print Only */
@media print {
.noprint {display:none !important;}
.noshow {display:block !important;}
}


Dec 31, 2007 at 11:59 AM // reply »
10,640 Comments

@James,

Excellent addition. I get a little uneasy about having stuff on the page that is reserved only for print, because at that point, I feel like the print CSS is almost being abused; however, for small things like a copy right, I am at peace.


Feb 4, 2008 at 12:12 AM // reply »
1 Comments

Great tip! We are just in the process of integrating printer-friendly CSS into one of our sites, and this helps solve one of our questions. Dealing with multiple layout and templates for printable pages can be quite an undertaking.


Feb 27, 2008 at 8:10 AM // reply »
2 Comments

Making the association

There are a variety of ways to associate media-specific stylesheets with a document. The most familiar way is to use the LINK element and simply add the media attribute:

<link rel="stylesheet" type"text/css"
href="print.css" media="print">

In the previous example, we see that the stylesheet print.css has been given a media of print; thus, its styles will be used only when the document is printed. These styles will have no effect on the display of the document when viewed using a monitor, audio browser, or any other non-printing device.


Jul 1, 2008 at 3:50 AM // reply »
1 Comments

The codes is helpful.. Thanks a lot =D


Raj
Mar 26, 2009 at 4:22 PM // reply »
1 Comments

Mr.James Moberg that was an excellent tip... Thanks a lot..


Feb 5, 2010 at 6:23 PM // reply »
2 Comments

I'm confused here. If you put the noprint in any div it hides that div for everything (display:none;). What am I missing. What makes it know you are printing and not just trying to read the website? Has to be something obvious I'm missing.
thanks


Feb 5, 2010 at 6:53 PM // reply »
37 Comments

@Terry, because the screen and printer each use different styles for the classnames. The "noprint" class will suppress the section from being printed, yet allow it to be displayed on the website. The "noshow" class will suppress the section from being viewed in the browser, yet it can be printed.

Here's a sample page to illustrate:

http://www.ssmedia.com/temp/noshow.cfm


Feb 5, 2010 at 7:10 PM // reply »
2 Comments

@James Moberg,

Thanks for the reply. This is another one of those things in CSS that I've never thought about and didn't know it was there (some of the functions to make this happen). CSS is a fun adventure...

Thanks


Feb 6, 2010 at 5:35 PM // reply »
10,640 Comments

@Terry, @James,

This is one of those features that I really like... but *still* have not started taking better advantage of it.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »