Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I emulate a page break with CSS and HTML?

Tags:

css

I'm trying to generate a CSS class that will help me define a "page break".

I know that if I use @media:print .MyDiv{ width:100%; height:100%; }, I can then set the class of a div to MyDiv and it will be -- as best as possible -- 1 page of print.

This is great, but I have encountered a new item that I don't know how to handle, and I'm hoping that something similar can be done. I need to create an empty div that will stretch itself out to the "end of the page".

Something like:

<style type="text/css">
@media print .PageBreak { 
    width: 100%; 
    height: *; /* space left on current page */ 
}
@media screen .PageBreak {
    border-bottom: 1px solid black;
}
</style>

<!-- ... -->

<div>This should appear on the first page.</div>

<div class="PageBreak"><!--
    This DIV should stretch itself out so that the next piece
    of content appears on the next page (only when printing).
--></div>

<div>This should appear on the second page.</div>

like image 723
Jerry Avatar asked Feb 02 '09 19:02

Jerry


People also ask

What is the HTML code for page-break?

To suggest a page break, add <P style="page-break-before: always"> before the beginning of a new printed page. For example, if you place the following tags on a HTML page and print it using a compatible browser, you will end-up with three pages with the sample text.

How do I make a page-break dynamic in HTML?

page-break-before: auto instead of . page-break-before: always. The "auto" will break the page only if the contents are at the end if the page, this will prevent breaking the page and leaving a lot of blank space. Save this answer.

How do I insert a page-break before HTML?

The page-break-before property adds a page-break before a specified element.. Tip: The properties: page-break-before, page-break-after and page-break-inside help to define how a document should behave when printed. Note: You cannot use this property on an empty <div> or on absolutely positioned elements.

What is a page-break in CSS?

There isn't an actual page-break property in CSS. It is actually a set of 3 properties: page-break-before , page-break-after and page-break-inside . These properties help define how the document is supposed to behave when printed. For example, to make a printed document more book-like.


2 Answers

This is for print, right?

Can't you just use this?

<p style="page-break-before: always"></p>

There are some other options to page-break-before that are worth knowing about too.

like image 142
Mark Biek Avatar answered Nov 09 '22 09:11

Mark Biek


In case anyone is interested in the solution I used:

<style type="text/css">
@media print {
    .PageBreak {
        page-break-before: always;
    }
}
@media screen { 
    .PageBreak { 
        border-bottom: 1px dashed black; 
        width: 100%;
        margin-bottom: 12px; 
    }
}
</style>

So simple. Thanks Mark and Mark.

like image 33
Jerry Avatar answered Nov 09 '22 09:11

Jerry