Fixing DIVs That Cause Content Truncation When Printing

Posted July 23, 2007 at 7:00 AM

Tags: HTML / CSS

A client recently called me complaining that their pages weren't printing correctly. This had never been a problem before. But apparently, now that the site has been up for a while and has some good content, the pages stopped printing correctly. After trying it for myself, I was irked to see that indeed, the first page printed but then none of the other pages printed. And, on top of that, the first page didn't even print properly; the first page's content overflowed right into the margin and off the page entirely. And then, nothing after that.

I had never seen this behavior before. After a few hours of Google searching, I didn't quite find the answer, but I found the right path. All suggestions to dealing with this content overflow print problem talked about taking away FLOAT styles and making displays "static" and even trying to put in some MS word "always break after" type of directives. None of that worked for me, but it got me thinking about the display.

Before I talk about the solution, let's see the problem in action. Imagine having a really simple page that has two DIV elements; one to define the width of the page and one to define the content area:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Print Style Cutoff Overflow Issues</title>
  • <style type="text/css">
  •  
  • div#pagewidth {
  • margin: 0px auto 0px auto ;
  • overflow: hidden ;
  • width: 500px ;
  • }
  •  
  • div#sitecontent {}
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <!-- BEGIN: Page Width. -->
  • <div id="pagewidth">
  •  
  • <!-- BEGIN: Site Content. -->
  • <div id="sitecontent">
  •  
  • <!---
  • Make some really long content so that when we
  • go to print, we will definitely be requiring
  • more than once page.
  • --->
  • <cfloop
  • index="intPara"
  • from="1"
  • to="20"
  • step="1">
  •  
  • <p>
  • Strings of lights above the bed Curtains
  • drawn and a glass of red All I ever get for
  • Christmas is blue Saxophone on the radio
  • Recorded forty years ago All I ever get for
  • Christmas is blue When you play my song,
  • play it slowly Play it like I'm sad and
  • lonely Maybe you can solve my mystery, Wrap
  • me in your arms and whisper You miss me With
  • a man sex is miserable, But the snow is so
  • beautiful All I ever get for christmas is
  • blue It would take a miracle To get me out
  • to a shopping mall All I really want for
  • Christmas is you Let them ring the bells
  • They won't miss us I'll be drinking down
  • your kisses Deep into the night we'll go
  • stealing Underneath the starry ceiling
  • Revealing White lights on the Christmas tree
  • Thank God you are here with me All I ever
  • get for Christmas is blue
  • </p>
  •  
  • </cfloop>
  •  
  • </div>
  • <!-- END: Site Content. -->
  •  
  • </div>
  • <!-- END: Page Width. -->
  •  
  • </body>
  • </html>

Now, if we try to go print this page, we get a really strange content overflow problem:


 
 
 

 
Print Overflow Problem  
 
 
 

Notice that going from page 1 to page 2, the content completely overflows the margins of the page and then does not even proceed onto the second page. No wonder my client was a bit peeved! This is entirely unacceptable.

The problem stems from the nested DIVs, PageWidth and SiteContent. I am not sure why the nesting seems to cause problems, but I know that if I remove the parent PageWidth DIV, the page with the SiteContent DIV alone will print just fine. I think it has something to do with the fact that both elements are block-level elements. A block level element inside of another block level element must have a hard time figuring out when to break because, from the parent element's point of view, there is no "page break" to accommodate. It's only when the parent element itself can wrap nicely that the inner block level element must also break nicely (... this is all theory here folks, but it's how I got to my solution).

So, to overcome this problem, we simply need to change the way the parent DIV, PageWidth, displays. Using a print-only style sheet definition (as denoted by the media="print" attribute in the STYLE tag), we will change the display of PageWidth to be "inline" when in the context of a print job. A display of "inline" will force the PageWidth DIV tag to act much like a SPAN or STRONG element that, by its nature, will naturally wrap from page to page when required.

 Launch code in new window » Download code as text file »

  • <head>
  • <title>Print Style Cutoff Overflow Issues</title>
  • <style type="text/css">
  •  
  • div#pagewidth {
  • margin: 0px auto 0px auto ;
  • overflow: hidden ;
  • width: 500px ;
  • }
  •  
  • div#sitecontent {}
  •  
  • </style>
  •  
  • <!-- Print styles. -->
  • <style type="text/css" media="print">
  •  
  • div#pagewidth {
  • display: inline ;
  • }
  •  
  • </style>
  • </head>

Because the STYLE tag has the media = "print" attribute, we don't have to worry about those styles affecting the run-time look and feel of the page. This is only for print scenarios. Having this in place, when we go to print:


 
 
 

 
Print Overflow Problem Fixed With Inline DIV Display  
 
 
 

... you will see that all the content wraps appropriately. Not only that, since we changed the DIV to be an inline element, it means that all Width rules on it no longer apply (most inline elements don't like have explicit widths). This is actually a good thing since the print version of a page probably shouldn't have any set height and width, other than the layout of the print paper itself.

Note: Content of page is composed of lyrics from the Over The Rhine song, All I Ever Get For Christmas Is Blue - a fantastic song!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 23, 2007 at 8:19 AM // reply »
30 Comments

Ben:
Just glancing at it, I would guess that what is happening has to do with the overflow hidden definition...
If your outside div is set to 500px wide, and the only thing inside it are div's, what is the reason for having the overflow definition at all?
In any case... print stylesheets are geat tools! I always define one to get rid of things such as content navigation (side menu's) and other items that interfere with the actual content.

Thanks for the post!


Jul 23, 2007 at 8:36 AM // reply »
6,371 Comments

@Ken,

The overflow: hidden is meant to stop the page from breaking if someone puts in inappropriate content (ie. pasting a table from MS word that is like 900px wide). This example doesn't demonstrate that need very much, but if you think of this being part of a multi-column layout of a content management system, then the overflow: hidden will be much more important.

And yes, you are correct about removing the overflow: hidden. If I remove that for the Print style sheet, it works as well. Just another way to alter the DIV in someway for printing. I like the "inline" rule though because it will also get rid of height / width definitions (which don't really work well inline elements). But certainly, two correct answers to the same problem.


Glenn Engel
Sep 24, 2007 at 8:05 PM // reply »
1 Comments

I had the same Firefox problem with page truncation and found I had an overflow:hidden in a nested div. However, setting display:inline did not have an effect. Changing to overflow:visible for print style though fixed my problem.


Dec 18, 2007 at 5:59 AM // reply »
1 Comments

Hey I'd just like to thank you for taking the time to write this article. You helped me solve a problem I was trying to fix.

So thank you very much and all the best.

Keep up the great work.

Richard


Dec 18, 2007 at 7:12 AM // reply »
6,371 Comments

@Richard,

Glad I could help. Although, I just came across a situation with Internet Explorer 7 where this didn't fix the problem! Same page works great in IE6 and FireFox, but noooo, IE7 had to be different :(


Leandro Falanga
Feb 13, 2008 at 12:47 PM // reply »
1 Comments

I'm having this problem since a week ago. I've been searching trough the web for several days, finding "very confusing" solutions that didn't worked.
Keep taking a minute for explaining the error clearly, that's more important that knowing the solution. When I read why was happening, I understood what should I do.
Thanks for being so precise and concise, that is what people need, you are my man.


Feb 13, 2008 at 12:51 PM // reply »
6,371 Comments

@Leandro,

Glad to help. Since posting this, I have run into some situations where this didn't help in IE7. But, for the most part, this type of fix seems to work nicely.


Andrew Mager
Feb 20, 2008 at 4:48 PM // reply »
1 Comments

Thanks, great tip.


itpretty
Apr 21, 2008 at 3:43 AM // reply »
1 Comments

Thanks a lot for the great knack.
I am trying to print out some contents hidden in the div tag that is set with overflow property. Tried many times but failed always. Finally I find you blog and it works now. Great stuff.


Jay
May 8, 2008 at 7:23 PM // reply »
1 Comments

I had a similar issue but when I tried applying the method on this page I could get it working fine in IE 7, but then everything was mangled in Firefox.

I finally noticed that whoever created the print.css placed a style for the main containing div (called #uber) and gave it a width of auto. I removed this line and it cleared-up my issue in IE and FireFox.

I'm not a CSS guru, so I have no idea throwing a width of auto on the main container div would do this, but the actual content div was nested within (3 or 4 levels) so this ultimately affected my content when printing.

I'd spent all my time focusing on the styles for the content div not thinking that the issue could have come from one of its ancestor divs.

Can we go back to text-based browsers? ;)


May 8, 2008 at 7:26 PM // reply »
6,371 Comments

@Jay,

I hear you man. When I started testing in IE7, this whole task became even harder! I was so irritated to find out that things that would not print ok in IE6 were messing up in IE7. Arrrg!


May 21, 2008 at 3:48 PM // reply »
1 Comments

Thanx a bunch, I can't believe I didnt tried display inline with my css, LOL save me

Buddha Soumpholphakdy
Downstairz Entertainment


Ted Graf
Jun 23, 2008 at 3:25 PM // reply »
1 Comments

Hey Ben,
This fix worked great in Firefox 3, but like you said, not in IE7.
Have you had any luck yet in IE7?


Jun 24, 2008 at 1:18 PM // reply »
6,371 Comments

@Ted,

It works sometimes but not others. I guess it's just one of the tools you can use when trying to fix printing problems, but it's not the end-all, be-all.


Lisa
Nov 10, 2008 at 12:01 PM // reply »
1 Comments

Thanks, this took care of the problem perfectly!


manan shah
Mar 30, 2009 at 7:32 AM // reply »
1 Comments

this not work with height of div.

i have fix height of div and overflow to hidden then when i m printing page it is printing the over flowed conent.

so anyone can help me?

thank you,
manan shah


Oct 20, 2009 at 9:55 PM // reply »
1 Comments

Thanks for posting this

My problem was solved by using:

* {
overflow: visible !important;
}

div#name {
display: inline;
}


Post Comment  |  Ask Ben

Recent Blog Comments
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »
Nov 6, 2009 at 4:53 PM
How To Unformat Your Code (Like A Pro)
I tried to go *back* the other way. Adding formatting is actually a much more complicated problem than removing formatting. Anyway, here is what I could put together with a minimal amount of time: ... read »