Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Page Layout w/ Breaks

I'm trying to make a webpage where it basically looks like a word document. There would be multiple boxes that would scroll down and the text would flow and page break from one page to the next.

Does anyone have any idea where I would even start? Thanks.

Edit: It should be right in the browser, looking similar to this:

enter image description here (Ignore the columns)

like image 496
stevenheidel Avatar asked May 06 '10 21:05

stevenheidel


People also ask

How do you do a page-break in CSS?

The page-break-after property adds a page-break after 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.

How can page breaks and page margins be specified in CSS?

The size of a page box cannot be specified in CSS 2.2. Authors can specify the margins of a page box inside an @page rule.

What does @page do in CSS?

The @page at-rule is a CSS at-rule used to modify different aspects of a printed page property. It targets and modifies the page's dimensions, page orientation, and margins. The @page at-rule can be used to target all pages in a print-out, or even specific ones using its various pseudo-classes.

What is page-break inside in CSS?

The page-break-inside property in CSS is used to specify how the page breaks inside the element to which it is applied while printing. It inserts a page break or sometimes it used to avoid page break inside an element while printing. Syntax: page-break-inside: auto|avoid|initial|inherit.


3 Answers

CSS mostly applies styles to a full element due to its box model. Exceptions are pseudo elements. So to create an appropriate break after a fixed length you would have to separate your text into correctly sized different elements.

EDIT: It would be possible using javascript. But even in the simplest case, where everything inside the pages delivered as just one text element with no sub elements (not even other text elements), the code will be a development nightmare and will run quite crappy. This is because there is no measure function in javascript. So you would be forced to do trail and error to find the correct position to break the element. Since the properties of the elements are live it means, that the viewer of the website will see a lot of flickering of your page just after loading. If you dare put other elements inside the html element to break into pages you get even more problems. More or less you get hundreds of special cases (break inside other elements, what if those elements are inside even other elements) to look out for.

like image 105
ablaeul Avatar answered Sep 25 '22 02:09

ablaeul


Something like that sounds possible using javascript, but it depends a bit on the structure of your html and whether or not you want to break paragraphs or just move the next paragraph to the next page if it doesn´t fit

So the simplest example, not breaking paragraphs / html elements with a flat html structure (no nested divs, columns, etc) like:

<div class="document">
  <h1>title</h1>
  <p>texts</p>
  <h2>subtitle</h2>
  <p>texts</p>
  ...
  <p>texts</p>
</div>

would be to do something like:

height = 0
loop through all direct child elements of .document
{
    if ( (height + element_height) > page_height)
    {
        add page_break_element before current element
        height = 0
    }
    height = height + element_height
}

I´d use jquery because it makes it easy to loop through the elements, measure heights, etc.

I guess breaking paragraphs would be possible as well, but a lot of extra work.

like image 32
jeroen Avatar answered Sep 22 '22 02:09

jeroen


<p style="page-break-before: always">This would print on the next page</p>

like image 34
Mitch Dempsey Avatar answered Sep 22 '22 02:09

Mitch Dempsey