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 »
46 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 »
11,314 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 »
46 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 »
11,314 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
seb
Jun 20, 2013 at 2:32 AM
Working With Inherited Collections In AngularJS
@mike, @ben, The best article about scope and prototypal prototypical inheritance in angularjs is http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical- ... read »
Jun 20, 2013 at 2:17 AM
ColdFusion NumberFormat() Exploration
Nice read thanks Ben, Is there a way to mask a negative number? Long story short in the finance sector when you go 'short' on a stock you want the price to fall this is a good thing because you are ... read »
Jun 20, 2013 at 1:09 AM
The Beauty Of The jQuery Each() Method
my html code : <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="nss.js"> ... read »
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools