[英]Keeping a header on one page when printing
I am designing a web page which I would like to be printable.我正在设计一个 web 页面,我希望它是可打印的。 The layout looks acceptable, except for one point: Headers sometimes get split (vertically) between different pages:布局看起来可以接受,除了一点:标题有时会在不同页面之间(垂直)拆分:
I obtained this example by trying to print https://www.craftinginterpreters.com/evaluating-expressions.html (which is a terrific read, by the way).我通过尝试打印https://www.craftinginterpreters.com/evaluating-expressions.html获得了这个例子(顺便说一句,这是一本很棒的书)。
What causes this ugly artifact?是什么导致了这个丑陋的神器? Is there anything I can do in CSS, HTML, or otherwise to prevent this kind of splitting?在 CSS、HTML 或其他方面我能做些什么来防止这种分裂?
Try this:尝试这个:
@media print
{
div{
page-break-inside: avoid;
}
}
Found it here: https://stackoverflow.com/a/907719/1195615在这里找到它: https://stackoverflow.com/a/907719/1195615
In the CSS for the header, there is the following line:在 header 的 CSS 中,有以下行:
font: 600 30px/24px "Crimson", Georgia, serif;
The 30px/24px means that the font size is 30px, while the line height is only 24px. 30px/24px 表示字体大小为 30px,而行高仅为 24px。
The header is split based on the line-height, which is usually about 20% bigger than the font size. header 是根据行高拆分的,通常比字体大小大 20% 左右。 In this case, it is smaller, which can cause the header to exceed the line height and split between pages.在这种情况下,它更小,这可能导致 header 超出行高并在页面之间拆分。
Simply deleting the line height from the CSS works:只需从 CSS 中删除行高即可:
font: 600 30px/24px "Crimson", Georgia, serif;
In this case, the default is used.在这种情况下,使用默认值。 It is also possible to increase the font height manually.也可以手动增加字体高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.