Break div when content exceeds maximum size

Asked

Viewed 560 times

1

My style css to a div, like an A4 page

.pagina {
    width: 210mm;
    min-height: 297mm;
    max-height: 297mm;
    padding: 30mm 20mm 20mm 30mm;
    margin: 10mm auto;
    border-radius: 5px;
    background: white;
    box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
}

<div class="pagina"> CONTEUDO </div>

But I wish that by exceeding the maximum size of the content of div, she created me a new "page"

Is there any way I could do it ? Because I have no way to separate all my content into several div

  • Is the content plain text? or are they grouped into blocks? div(s), p(s), li(s), etc?

  • @Tobymosque Content has div/p, etc, it comes from a structure generated in the Ckeditor

  • 'Cause you need this max-height? Is for printing?

  • You need to use the property page-break-inside CSS. Take a look here

  • I believe that the page-break and page-break-initial are considered only in print (I may be wrong)...

  • It’s not for printing

Show 1 more comment

1 answer

2


@Rod, I performed an implementation here, in case it will create the other pages according to the need:

var paginaAtual = document.querySelector(".pagina");
var book = document.querySelector(".book");
var millimeter = 3.779527559; //1mm = 3.779527559 pixel

var getHeight = function (elem) {    
    return Math.round(elem.offsetHeight / millimeter);
}

var addPage = function () {
    var page = document.createElement("div");
    page.classList.add("pagina");
    book.appendChild(page);
    return page;
}

var ultimaPagina = null;
var largura = getHeight(paginaAtual);
if (largura > 297)
    ultimaPagina = addPage();

while (largura > 297) {   
    var lastNode = paginaAtual.childNodes[paginaAtual.childNodes.length - 1];
    ultimaPagina.insertBefore(lastNode, ultimaPagina.firstChild);
    
    largura = getHeight(paginaAtual);
    if (largura <= 297) {
        paginaAtual = ultimaPagina;
        largura = getHeight(paginaAtual);
        if (largura > 297)
            ultimaPagina = addPage();        
    }
}
body {
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    background-color: gainsboro;
    padding: 0px;
    margin: 0px;
}

.pagina {
    width: 210mm;
    min-height: 297mm;
    padding: 30mm 20mm 20mm 30mm;
    margin: 10mm auto;
    border-radius: 5px;
    background: white;
    box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);    
    box-sizing: border-box;
}
<div class="book">
    <div class="pagina">
        <p>
        Fusce viverra sed nisl a pharetra. In feugiat urna eget accumsan cursus. Donec in tincidunt quam, aliquam varius quam. Donec id pharetra purus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam ultricies in quam ut imperdiet. Aliquam risus dolor, facilisis ac metus eu, dignissim dignissim ligula. Aenean gravida bibendum quam ut rhoncus. Etiam scelerisque eros eget ante ultrices tristique. Donec quis tortor magna. Etiam in nisi eget risus aliquam vestibulum ac vel nisl. Maecenas sit amet sem ornare, faucibus orci sit amet, maximus sapien. Nulla nec ex id nulla luctus elementum eu sit amet risus. Suspendisse blandit scelerisque elit et consequat. Nulla congue est et mi dictum porta.
        </p>
        <p>
        Suspendisse sed gravida nulla. Vestibulum a tempor tellus. Nullam blandit diam eu eros mattis, vel ultricies urna venenatis. Donec ut lectus molestie, lacinia dui ac, malesuada diam. Maecenas at pretium odio. Donec enim magna, congue sit amet molestie vel, efficitur a nibh. Vivamus neque sem, porttitor at leo at, ultricies lobortis nisl. Maecenas quis tortor vitae magna malesuada faucibus. Nunc congue, magna a molestie pulvinar, mi nibh tempus lacus, at tincidunt nibh nulla vitae lacus. Fusce purus augue, vestibulum vel venenatis sit amet, vulputate a purus. Cras id dolor bibendum, rutrum enim eget, ornare erat. Cras vel lectus eget turpis malesuada tincidunt.
        </p>
        <p>
        Aliquam maximus ante ut sodales vestibulum. Sed sit amet eros nec nulla rhoncus gravida. Praesent eleifend elementum mi, ac porta mauris dapibus sit amet. Maecenas id urna venenatis, viverra nisl et, finibus quam. Nam augue ligula, elementum laoreet nisi porta, dignissim dignissim diam. Suspendisse scelerisque laoreet mauris vitae elementum. Fusce in ante elementum, vulputate urna vehicula, faucibus sapien. Aenean dignissim vestibulum ligula sed rutrum. Sed sed placerat ligula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam ut malesuada mi. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla sit amet feugiat felis, et facilisis neque.
        </p>
        <p>
        Maecenas vel hendrerit dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent ut iaculis nibh. Curabitur porta feugiat sapien et auctor. Quisque vel sodales est. Aenean dui ligula, sodales a tristique ac, ullamcorper a elit. In sagittis vel odio sed euismod. Fusce vitae mi commodo, aliquet felis sit amet, auctor orci. In id sapien rhoncus, fermentum nunc a, luctus quam. Donec ut risus nibh. Proin id massa suscipit, euismod eros vel, auctor est. Nunc sapien lacus, rutrum eu diam id, semper ornare diam. Ut nec neque semper, venenatis leo vitae, vulputate mi. Sed vehicula orci sit amet malesuada pellentesque.
        </p>
    </div>
</div>

JSFIDDLE

  • For some reason, loaded everything out of order my pages, kkk but gave to have an idea, I will give a tested

  • @Tobbymosque, he separated the pages even though he generated in reverse, generates the page first, then comes back to front the pages =/

  • @Rod, Jsfiddle is working normal.

  • notice after the paragraph that starts with the word "Donec", you will see that it puts the first page correctly and the rest are backwards

  • @Rod, it was a small (big) logic error. I fixed it.

  • Where the value comes from 4.41414141?

  • @Tobymosque thank you, and also would like to know, where did that value 4.41414141

  • @Tobymosque :/ bá what is that 4.4... that makes the division? to is curious yet

  • Actually an error induced by your css. I arrived at it through trial and error, dividing the innerheight by some value until I reached 297. The error here is that innerheight is the sum of height with padding.

  • I updated the response taking into account that 1mm = 3.779527559 pixel, as seen in http://stackoverflow.com/questions/2651029/how-to-convert-pixel-into-point-size-in-html. I’ve also added the style box-sizing: border-box; so that the page has the correct size.

  • @Tobymosque I +- understood the code, you take the contents in lastnode and add already with the class page, still debugging the code to understand kkk

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.