Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS page-break-after and float not playing nicely?

If I have the following HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>Test</title>
    </head>
    <body>
        <div style="page-break-after: always; float: left;">hello</div>
        <div style="page-break-after: always; float: left;">there</div>
        <div style="page-break-after: always; float: left;">bilbo</div>
        <div style="page-break-after: always; float: left;">baggins</div>
    </body>
</html>

I want one word to be printed on each page, with a page break after each one. (This is simplified code - in my actual web page, the floats are important.)

Firefox prints this fine, but both IE and Safari print them all on one page, ignoring the page breaks. Is this a bug in those browsers? Is there a better way to do this?

Thanks.

like image 468
Colen Avatar asked Oct 07 '10 22:10

Colen


People also ask

Is it good practice to use float in CSS?

The short answer: clear: both. Floats work really well in small cases like when there's an element, such as a button, that you'd like to move to the right of a paragraph. But the real issue arises when you start using floats to lay out entire web pages. And the reason for that is: floats are not meant for layouts!

Why page-break is not working?

If manual page breaks that you add don't work, you may have the Fit To scaling option selected (Page Layout tab -> Page Setup group -> click Dialog Box Launcher Button image -> Page). Change the scaling to Adjust to instead.

How do you make a float down in CSS?

The footer div will need to be either: position:absolute;bottom:0; ; This will push it to the bottom of its container, however, when you scroll down past the container, the footer will scroll with it. position:fixed;bottom:0; ; This is used more often for sticky footers.


1 Answers

It is the floats that are messing it up for printing.

Do you need the floats there for printing? or are floats only needed for the web?

Why I am asking is you can have different CSS classes for different medias (print, screen) http://www.w3.org/TR/CSS2/media.html

So your float can be on the screen media - which will show only for web. While you will want your page break only to show for print media.

Here is an example using the media: (note when referencing CSS you can choose media via an attribute )

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>Test</title>
    <style>
        @media print {
       .myDiv { page-break-after: always; }
      }
      @media screen {
        .myDiv {float:left;}
      }
    </style>
    </head>
  <body>
    <div class="myDiv">hello</div>
    <div class="myDiv">there</div>
    <div class="myDiv">bilbo</div>
    <div class="myDiv">baggins</div>
    </body>
  </html>

Update:

Will this work for what you need? GIves you a 3x3 on each page when printing out. But on the screen it's a 3x6. Quick sample.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>Test</title>
    <style>    
        .break
        {
                   page-break-after: right; 
                   width:700px;
                   clear:both;
        }
        .myDiv {    
        float:left;
        width:200px;
        height:100px;
        background-color:blue;
        margin:5px;
        }
      }
    </style>
    </head>
  <body>
    <div class="break">
        <div class="myDiv">1</div>
        <div class="myDiv">2</div>
        <div class="myDiv">3</div>  


        <div class="myDiv">4</div>
        <div class="myDiv">5</div>
        <div class="myDiv">6</div>  


        <div class="myDiv">7</div>
        <div class="myDiv">8</div>
        <div class="myDiv">9</div>  
    </div>

    <div class="break">
        <div class="myDiv">11</div>
        <div class="myDiv">22</div>
        <div class="myDiv">33</div> 


        <div class="myDiv">44</div>
        <div class="myDiv">55</div>
        <div class="myDiv">66</div> 


        <div class="myDiv">77</div>
        <div class="myDiv">88</div>
        <div class="myDiv">99</div> 
    </div>
    </body>
  </html>
like image 62
Ryan Ternier Avatar answered Sep 30 '22 13:09

Ryan Ternier