Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

page-break-after not working in flexboxes

This doesn't produce the expected result inside print preview in Firefox:

<aside>
  side
</aside>

<div>
  <p> page 1 </p>
  <p> page 2 </p>
</div>

CSS:

body{
  display: flex;
}

aside{
  flex: none;
  width: 100px;
}

div{
  flex: auto;
}

p{
  break-after: always;
  page-break-after: always;
}

In Chrome and IE I get 2 pages like I should. It appears that FF doesn't break the div in 2 pages when an ancestor is a flex box. Why?

like image 367
nice ass Avatar asked Sep 25 '14 10:09

nice ass


People also ask

Why page-break is not working?

Parent elements can not have float on them. Setting float:none on all parent elements makes page-break-before:always work correctly. Other things that can break page-break are: using page-break inside tables.

How do you insert a page-break after always?

always − A page break should be forced after this element's box. avoid − No page break should be placed after the element's box if at all possible. left − Force one or two page breaks after the element's box, such that the next page on which an element is printed will be a left-hand page.

What are the disadvantages of using flexbox?

Disadvantages. While flexbox is supported by most major browsers, it's still newer than the traditional box model. This means older browsers don't support it as well (some not at all). There are also more inconsistencies across different browsers.

What does page-break after do?

The page-break-after CSS property adjusts page breaks after the current element. This property applies to block elements that generate a box. It won't apply on an empty <div> that won't generate a box.


2 Answers

I'm pretty sure that won't work in firefox.

Things that can break page-break are(using page-break inside)

  • tables
  • floating elements
  • inline-block elements
  • block elements with borders

To define if a break must be done, the following rules are applied:

1.If any of the three concerned values is a forced break value, that is always, left, right, page, column or region, it has precedence. If several of the concerned values is such a break, the one of the element that appears the latest in the flow is taken (that is the break-before value has precedence over the break-after value, which itself has precedence over the break-inside value).

2.If any of the three concerned values is an avoid break value, that is avoid, avoid-page, avoid-region, avoid-column, no such break will be applied at that point.

Once forced breaks have been applied, soft breaks may be added if needed, but not on element boundaries that resolve in a corresponding avoid value.

break after - CSS | MDN

In short words, in your case cause you are using it inside flex won't work.

like image 74
Alex Char Avatar answered Sep 19 '22 19:09

Alex Char


Firefox doesn't do page-break correctly even with float elements, so I'm not surprised that flex doesn't work. Source: CSS Page-Break Not Working in all Browsers

In general, Firefox page-break support isn't great. See: https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after

If you need consistent, cross-browser printing results, the answer is almost always to use server-side PDF generation, with a tool like wkhtmltopdf or princexml.

like image 29
William Hertling Avatar answered Sep 16 '22 19:09

William Hertling