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,246 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,246 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
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools