views:

169

answers:

3

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:

Pages (Ignore the columns)

+1  A: 

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

webdestroya
Hi, that's not exactly what I was looking for... I clarified it in the description with a picture. Thanks.
stevenheidel
+2  A: 

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.

ablaeul
My thought would be to determine where the page would break using javascript, does this sounds possible?
stevenheidel
So I might have to go with something like flash, etc. then?
stevenheidel
+1, very good answer.
ANeves
When you state that there is no measure function in javascript, were you thinking of something other than jquery's height?
stevenheidel
Yes, we need the height, an element *would* have, if we were to insert specified text and subelements. jquerys height function returns the *current* height instead.
ablaeul
Thanks for responding, but that went over my head. Can you give me an example?
stevenheidel
Well, you *could* use `height()` to measure the height of an element. But the problem is, that you have to put that element into the document tree. So it is visible to the user. But if you are building up your pages, that will result in a lot of flickering, since the browser has to update the page every time you want to measure your new element.
ablaeul
Oh okay I got it now thanks :)
stevenheidel
+1  A: 

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.

jeroen
Perfect! I actually got this to work sort of using javascript. As for breaking paragraphs, I'll just use a lot of spans I guess :P.
stevenheidel
Sounds good, good luck!
jeroen