break-inside: avoid doesn't work for basic example - html

I'm using Chrome v99 and a pretty basic usage of break-inside: avoid - but it does nothing.
Moderator: This is not a duplicate of this post and the 1 answer there isn't helpful.
My objective is to disallow the contents of all these divs from splitting at page breaks:
<div class="opt">
Here is my inline css:
<style media="screen" type="text/css">
#media print {
div.opt, table {
break-inside: avoid !important;
}
}
</style>
The full html is a bit lengthy but you can view the entirety here.
Simply press CTRL+P and observe that it page-breaks in the middle of divs with a class of opt - but it shouldn't because I'm using break-inside: avoid.
The example I linked to is very short and contrived, but my real scenario has many more sections and is dynamic, pulling from a database. So there's no way to know where the page is going to break relative to the divs. I just want the contents within those divs with that class to always stay together on the same page and never split across a page break. No div contents ever exceed a page, they are even shorter than my example.
Help, what am I doing wrong?

You have media=screen in the style tag...
Your print styles will only load when you're using a screen and not a printer
<style media="print" type="text/css">
div.opt, table {
break-inside: avoid !important;
}
</style>
When you fix it though it still seems to wrap onto multiple pages uglier but at least now you can play around with what print styles work

Related

where to add page-break related css in wkhtmltopdf?

I am using wkhtmltopdf 0.12.3.2 on Windows.
I know there are a lot of questions and answers around this topic, but I still can't find an answer to my problem; I don't know where to put the according CSS - or the CSS doesn't work for some (other) reason:
for example i tried to put the page-break related CSS directly into my html file which i want to render. i tried to force page-breaks with <span class="break_here"></span> in my <body>:
<!-- ... -->
<head>
<style>
span.break_here {
page-break-after: always !important;
}
</style>
</head>
<!-- ... -->
this didn't do anything.
then i also tried to put it into #media print{} or #media screen{} which did not change anything either:
<style>
#media screen{
span.break_here {
page-break-after: always !important;
}
}
</style>
thanks for any help!
edit: there is even another possibility by adding the --user-style-sheet option for using an external stylesheet.
Adding pagebreaks via a standalone element in wkhtmltopdf has caused me problems as well.
I've found that applying the pagebreaks to an element which wraps the contents to be much more reliable.
This doesn't work so well:
<div>some content</div>
<div class="pagebreak"></div>
Whereas this does the trick:
<div class="pagebreak">some content</div>
To make this work, I did not use .pagebreak{page-break-after: always!important;}
But, instead used: .pagebreak{page-break-inside: avoid!important;}
It's also good to note that pagebreaks on print should be as high-up in the dom-tree as possible. Applying pagebreak rules to elements that are deeply nested can cause headaches (or at least has for me in the past)
Hope this helps!

Orphan CSS: How avoid headers (h1, h2...) on bottom page?

I have a large HTML document with headers (h1, h2, h3...) and paragraphs. When I print the document, I want that, automatically, headers at bottom of document go to next page:
How can I do? I use "orphans: 3" CSS with paragraphs and works with "p" tags, but with h1 or h2 don't work.
#page {
size: A4;
}
p {
orphans:3;
}
h1, h2 {
orphans:3
}
Full example on action where:
1-2 page: paragraphs orphan works fine.
2-3 page: headers don't works.
Requirements:
HTML have one main div container.
Don't alter HTML.
Browser support isn't important (on my specific job).
I need some trick in CSS (no JS or Jquery, preferably)
I can't use page-break-before:always because I want that headers only go to next page when appears at bottom of page.
In typography an orphan is:
A paragraph-opening line that appears by itself at the bottom of a page or column, thus separated from the rest of the text.
However in HTML <h1> and <p> are different paragraphs then what you have to use is break-after property to tell layout engine to do not put a page break after that paragraph (with the side effect to move next paragraph up to previous page - if fit - or to move header to next page.
h1, h2 {
break-after: avoid-page;
}
Note about compatibility: break-after setting is a true working draft and even basic features are not widely supported (notably Internet Explorer 10 does). To workaround this you may use another property with similar meaning:
h1, h2 {
page-break-after: avoid;
}
Note that page-break-after applies to both page and columns. page-break-after isn't well supported by FF (it is a bug) then if compatibility is important and paragraph won't span across multiple pages you can workaround wrapping <h1> and <p> inside a container (let's say <section>) and then apply page-break-inside like this:
section {
page-break-inside: avoid;
}
IMO you should combine page-break-after and page-break-inside using page-break-inside with -moz prefix until it will fix that bug.

Printing Tables with Bootstrap W/ Proper Page Breaks

I am trying to make a table printable, specifically never have it break a <td> entry in half, while maintaining proper borders. I am trying this:
#media print {
table.print-friendly tr td, table.print-friendly tr th {
page-break-inside: avoid;
}
}
However, this still causes a problem where the table data is missing upper or lower margins. Actually, I am not sure it does anything, because without it, the table misses the last margin on the first page. Easier to explain with an image here:
Plnkr here. Everything relevant is in index.html - I think you need to copy it to notepad and run locally to test printing. Note that the only relevant things in that code are the <style> definitions and the table classes.
To summarize - my goal is to make the table break properly AND maintain margins on print.

Printing an Invoice on pre-formatted paper using CSS

I have an invoice in html that displays well on the screen. I want to print it to pre-formatted paper. the paper has three sections
Header (fixed height from the Top of the page)
Body (table made up of 1 - N rows)
Footer (fixed height from the bottom of the page)
I have tried using CSS and creating a div using #InvFooter and CSS
<link rel="stylesheet" href="print.css" type="text/css" media="print" />
#page { size:8.5in 11in; margin: 2cm }
#InvFooter {position:absolute;left:50px;bottom:0px;}
I Have two problems I can't figure out.
How to anchor the footer to the bottom of the printed page.
How to limit the Body to a fixed section of the page and overflow into another page if the table has too many rows.
Simple answer: don't, html/css was never intended for printing. Even if you can "hack" absolute sizes to fit your current printer there is no guarantee they will fit the next (or the customer's).
For this reason PDF was invented, to give you want you expect in a printed format (as well as high portability).
By using PDF you are guaranteed to get a fixed (paper-) size.
You will be able to find free pdf-solutions (open source) as well as commercial solutions (such as for example Dynamic PDF - and there are many others) that allow you to generate PDF from .Net on the fly/dynamically from server based applications with no dependencies of Adobe products.
This is the best compilation of CSS printing techniques I've come across. It has deep links to the W3 standards, excellent examples, 3rd party recommended HTML -> PDF software and a handy JPEG which visializes the 16 different page-margin selectors. It got me started and now I can print over 100+ varying-length project summaries to bring with for executive reviews.
The article:
https://www.smashingmagazine.com/2015/01/designing-for-print-with-css/
The handy-dandy page margin JPEG:
https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2014/12/1-image-margin-boxes-large-opt.jpg
The demo HTML book:
https://github.com/rachelandrew/css-for-print/blob/master/book.html
The demo print CSS:
https://github.com/rachelandrew/css-for-print/blob/master/pdf-styles.css
Hope this helps!
EDIT
These might be what you're looking for:
.invoice {
page-break-before: always;
}
.invoice h1.title {
page-break-after: avoid;
}
.invoice .line-item {
page-break-inside: avoid;
page-break-after: auto;
}
EDIT
How does formatted paper accommodate multi-page invoices? You must have to print every invoice individually... ugh

Should we be setting width on div#wrapper or the <body>?

A lot of people's HTML markup looks like this:
<html>
<body>
<div id="wrapper">
<p>Stuff in here</p>
</div>
</body>
</html>
And most of the time in examples here, or on the web, people suggest that you should apply width settings to the #wrapper, instead of the <body>.
Is there an underlying reason for that?
For example, in an article on techniques for gracefully degrading media queries, and to give you some context on Technique 1: Do Nothing:
The elephant in the room is Internet Explorer for the desktop.
With a mobile-first solution, large screens will display the content
in one column, resulting in a painful reading experience for the user,
way past the established maximum for comfort: 50 to 75 characters.
It might be worth setting a max-width property in your main container and then upping that max-width in a media query.
And their CSS:
#wrapper {
_width: 460px; /* Take that, IE6! */
max-width: 460px;
}
#media only screen and (width) {
#wrapper {
max-width: 1200px;
}
}
Here's how it'd come together for IE (media query is commented out).
Would there be any difference whereby instead of applying that to #wrapper, we would apply it to <body> — with the standard website in mind?
Any potential bugs? I tried it here, and it seems to be OK. Just what if the layout gets more advanced...
Well, you want to use as few elements as possible I guess. However there are many instances where #page-wrapper and body are not interchangeable. In many situations you need to use the body as the background color instead of the html tag. In these cases (weighted footers for example) you need the body to stretch out the html and you need a wrapper to contain the content, maybe center the content, and force the body to stretch out and contain it.
So - I guess I would say, that most people use a wrapper because they saw it in their first class or online tutorial. I think that for the most part, it is a habit for many. I would leave the body as is and margin the wrapper to 0 auto and use a max width like you have. It's just EI 8 and before - can I use media queries ? - maybe you should detect EI 8 and make a unique style sheet. I find that after defining everything for mobil, my media queries are only a few lines of iteration after that -