• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2208
  • Last Modified:

Using CSS to keep paragraphs/blocks together when printing

Is there a way to keep a block of text together, either in a DIV tag or as P tag, when you print a page? Example. A quotation page with many items, and at the end there is a disclaimer and signature area that must be together, either on the first page or next page.

Is this possible at all??  I have searched but cannot see anything for this.
0
mandymoo
Asked:
mandymoo
  • 2
  • 2
2 Solutions
 
David S.Commented:
Try using the page-break-before property: http://reference.sitepoint.com/css/page-break-before
0
 
mandymooAuthor Commented:
Unfortunately this doesn't fix the problem.
Applying this to the #invfooter DIV style, still allows its contents to be split over 2 pages, as I'm wanting either all the info on the same page (if short quote) or the info to be on the next page in full.

Code sample:
#invfooter {
      clear: both;
      width: 650px;
      margin-top: 0px;
      margin-right: auto;
      margin-left: auto;
      font-size: 11px;
      line-height: 14px;
      text-align: right;
      page-break-before: auto;
}

I have also tried "orphans"... do I have to use them both together?? Again I have tried this above with "orphans: 12".

Any other suggestions?
0
 
quincydudeCommented:
try page-break-after

see the examples in on how to use it, and set it to always
http://www.jonasjohn.de/snippets/html4strict/css-pagebreak.htm
0
 
David S.Commented:
It seems I should have explained more.

The property you want is page-break-inside, however, only recent versions of Opera support it, so it won't do you much good. Likewise, only recent versions of Opera are the only browsers to support the orphans and widows properties.

Thus you're stuck with page-break-before and page-break-after. The default value for each of those properties is "auto", so declaring it doesn't do any good. Because browsers only partially support those properties you're down to two choices: page-break-before:always and page-break-after:always
0
 
mandymooAuthor Commented:
Right, Ok. Thanks Kravimir.

I was a little discouraged, but I have resolved the issue with a little help from PHP!

They way I got around it was to have a form on my print page, where I can indicate if I want to break the page or not. After indicating I want to break the page, it passes a value to the URL string of "break=always" to the same page. Then, in my stylesheet, I added the variable to it. The CSS is called in when the page loads and everything works beautifully.

Code sample:
#invfooter {
     ....
      page-break-before: $_GET['break'];
}

So a combination of CSS & PHP allows me to break the page at a certain place if the page is too long for 1 page.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now