Fixing DIVs That Cause Content Truncation When Printing

Posted July 23, 2007 at 7:00 AM by Ben Nadel

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:

  • <!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.

  • <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!



Reader Comments

Jul 23, 2007 at 8:19 AM // reply »
35 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 »
11,238 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.


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 »
11,238 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 :(


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 »
11,238 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.


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

Thanks, great tip.


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 »
11,238 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


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 »
11,238 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.


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

Thanks, this took care of the problem perfectly!


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;
}


Nov 10, 2009 at 4:56 PM // reply »
1 Comments

Thanks! The solution posted works for me--I didn't need to do any of the gyratins listed below.

Thanks again!


Nov 11, 2009 at 7:10 PM // reply »
1 Comments

thank you for sharing, overflow: visible !important; solved the problem. love your work!


Nov 15, 2009 at 10:32 PM // reply »
11,238 Comments

@Dan, @Moshaver,

Sweeeet :)


Jan 8, 2010 at 3:00 PM // reply »
1 Comments

I can't use overflow: visible !important; because on my search results I am using overflow:hidden on a div containing a preview photo of the items.

This makes the preview images all appear to be the same size and gives a nice appearance.

* { overflow: visible !important; }

would obviously break that and a few other things like it.

Also some people use overflow:hidden to make parent divs expand around their floating children.

CSS needs an official way to make parent divs expand around their floating children without needing clearing hacks like clear:both on spans at the bottom of the parent div or the misuse of overflow:hidden


Jan 11, 2010 at 12:49 AM // reply »
2 Comments

The solution works fine to some extent on firefox. but IE its still giving problem.. :(.. anyone figured out the solution for this??

thanks in advance


Jan 11, 2010 at 4:22 AM // reply »
1 Comments

Have come across this a few times I find simplifying the layout and taking off overflows, positioning and floats (either entirely or to some degree) fixes the problem.

Debugging is a pain but you can try adding borders to see what is going on best as possible, ideally strip the print layout to bare bones and explain to client print stylesheet isn't meant to mimic the site but to provide main content in a print efficient fashion.

Hope that helps,
Dave


Jan 11, 2010 at 10:54 AM // reply »
2 Comments

I removed Div tried putting table cell. and removing float... tried different values for overflow.. doesn't work... [:(].. badly in need of solution..
Thanks in advance for any replies, suggestions & solutions


Jan 11, 2010 at 2:03 PM // reply »
11,238 Comments

@Parag,

It definitely takes some serious debugging sometimes! Such a pain. I only ever try to figure this out when a client specifically asks for it; otherwise, I typically create secondary, print-friendly versions of pages.


Jan 22, 2010 at 1:22 PM // reply »
2 Comments

Many thanks, your post helped me fix the same type of issue that a company I'm working with had on their pages too. You definitely helped guide me in the right direction to get this fixed!


Jan 22, 2010 at 5:02 PM // reply »
11,238 Comments

@Matt,

Awesome - glad it's working well for you.


Apr 7, 2010 at 2:07 PM // reply »
270 Comments

Important new information about the CSS property "overflow".

Having just gotten an iPad, I've been using multi-touch Safari a lot more lately (that is, Safari for iPod Touch, iPhone and iPad). I discovered something that had escaped my notice on the iPod: Multi-touch Safari doesn't support overflow:auto or overflow:scroll! It behaves the same as overflow:hidden.

Just FYI, as this will probably become an issue as iPads become more common.


Apr 7, 2010 at 9:11 PM // reply »
11,238 Comments

@Steve,

Really?? That is too odd. That seems like such a core CSS thing; and Safari has always been so cutting edge with CSS. Thanks for bringing this to my attention.


Apr 7, 2010 at 10:30 PM // reply »
270 Comments

Apple just announced that there's iPad help at the bottom of iPad Safari's bookmarks. I went there and navigated to Safari > Zooming and Scrolling. There they mentioned that the way to scroll a frame is with 2 fingers, not 1 finger like the rest of the page.

Just in case, I went back to the scrollable div (the one with style="overflow:auto") and tried it. Sure enough, it worked. So the problem is that Safari doesn't TELL YOU that the content is scrollable by putting up scroll bars. Without that feedback, the user continues to use one finger to scroll and doesn't know that there's content they aren't seeing!

It's like putting white text on a white background. The content is there, but the user doesn't know it's there.

It's still a violation of CSS2 that Safari doesn't put up scroll bars. But at least, if you're familiar with a site, and you know that there's content there that you're not seeing, and you know that you're supposed to switch over to 2 fingers, the content won't be inaccessible.

I still think it's a bug. But I hope it helps to know what the real problem is and how to do the 2 finger workaround. (That sounds so naughty.)


Apr 9, 2010 at 10:05 PM // reply »
270 Comments

It gets worse. 2 finger scrolling a frame DOESN'T work. (I just found one to check with.) Starting to get very unhappy with multi-touch Safari.


Apr 10, 2010 at 2:07 PM // reply »
70 Comments

Sorry for cluttering up this post with iPad stuff, but it really is CSS2-overflow-property-related. If you have an iPad, try the following (and then on a PC/Mac browser to see how it's supposed to look):

http://www.webmanwalking.org/library/experiments/dsp_frames_outer_document.html


Apr 19, 2010 at 10:19 PM // reply »
11,238 Comments

@Steve,

I know what you mean about the scrollable things not having scrollbars sometimes. I've played around a bit with the iPad and it is definitely NOT clear that some things are scrollable. I find this even to be true in core apps like the App Store app and other news-related apps.

I like nice design as much as the next guy - but interfaces should *still* be intuitive.


Oct 28, 2010 at 12:52 PM // reply »
1 Comments

Thank you very much, Ben!

We had the quite a similar problem, two nested divs with long content.

All Browsers except Firefox just truncated the content when printing.

Now we are using just one div and a different css for print media with your "display:inline;" fix - and it just works ;)

This solution seems to be accepted by Firefox and all WebKit-based browsers...

So thanks again,

Udo


Nov 1, 2010 at 9:35 PM // reply »
11,238 Comments

@Udo,

Glad to help out :)


Nov 10, 2010 at 7:14 PM // reply »
270 Comments

@Ben,

I sent in the Safari-for-iOS overflow problem to Apple back in mid-April, when we were discussing this. Guess what? Just YESTERDAY Apple finally got back to me that it was a "known problem".

I'm used to talking to a tech in less than a minute on AppleCare, so this seven-month response delay was somewhat surprising. :-)


Nov 13, 2010 at 11:55 AM // reply »
11,238 Comments

@WebManWalking,

Ha ha ha, 7-month turn around time. Hey, at least they area aware of the problem.


Oct 2, 2011 at 3:16 PM // reply »
2 Comments

Sir,

I am creating a application in that when am trying to none of the styles printed but in web page all styles were reflected, it is specific to IE alone. could you provide your thoughts?


Oct 2, 2011 at 3:17 PM // reply »
2 Comments

Sir,
Ignore my previous comment.

I am creating a application in that when am trying to print none of the styles printed but in web page all styles were reflected, it is specific to IE alone. could you provide your thoughts?


Nov 30, 2011 at 11:35 AM // reply »
1 Comments

Hey Ben, nice tip. Thanks.



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 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools