"Enhance the appearance with jQuery: beautifying code

In order to achieve a pretty print layout using jQuery and HTML, I need to ensure that if any div with the class .section reaches the bottom of an A4 page (where the A4 body width is 595px and each page height is 842px), it will have a margin added to push it onto the next page. See below for the implementation code:

  $(".section").each(function () {
    // Add element offset top to detect element page number
    // For example, if the element top is 1400 then it is on page 2 (842*2 > 1400)
    $(this).attr('data-top', $(this).offset().top);
});
$(".section").each(function () {
        $elementTop = $(this).data('top');
        if (parseInt($elementTop) > ($currentPage * $A4PageHeight)) {
                if (!$(this).hasClass('rendered')) {
                    $(this).css('margin-top', ($elementTop - ($currentPage * $A4PageHeight)) + 'px');
                    $(this).addClass('rendered');
                    $(this).addClass('page-'+$currentPage);
            }
            $currentPage += 1;
        }
    });

see image here

Answer №1

Upon exploring the CSS print functionality, I came across a useful property known as page-break-inside that helps to prevent elements from being split across multiple pages. Implementing this property resolved my issue seamlessly.

@media print {
    .section {
        display: block;
        page-break-inside: avoid;
    }
}

No need for jQuery!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS vertical alignment property

I'm facing a rather simple problem that has left me scratching my head. The vertical align property is performing correctly, but as soon as I introduce a tag after the text meant to be centered along the image, any text that follows the tag appe ...

Controlling the scroll behavior of div elements with the CSS overflow-y property

This is the current setup I have: <div> <div> </div> <div> </div> <div> </div> </div> The inner divs are styled with this CSS: float: left; margin-right: 40px; width: 100px; While ...

jQuery concealing selections from dropdown menus

Is there a way to use jquery sort to order dropdown list options and hide the "select" item in the dropdown? Check out my fiddle code here <select id="mySelect"> <option value="">select</option> <option value="1">a</opt ...

Setting the value of an <h1> element using data from an API response - A simple guide

I have been attempting to update the title of a page using data from an API. Here is my HTML: <h1 id='league_name'></h1> This is my JavaScript code: <script> fetch('https://api.example.com') .then(response ...

Executing jQuery after the body has finished loading

I'm trying to execute a JavaScript function once the body has finished loading. My framework loads the header and footer from static files, while the body content is dynamic. I read online that I should place the $(elem).load(function()) at the end of ...

Styling input groups with box shadows in Bootstrap 4

One issue I am encountering is the focus shadow that appears when the input text is focused. My goal is to have both the text input and the right button display a focus border. To illustrate this problem, I have created a JSfiddle: http://jsfiddle.net/a5u ...

How can I turn off shadows for every component?

Is it feasible to deactivate shadows and elevation on all components using a configuration setting? ...

Are you attempting to retrieve the most up-to-date trading price of a stock with Python?

After attempting to extract the LTP of a stock with the code below, I am not getting any value in return. Can you identify the error in the following code: from selenium import webdriver from bs4 import BeautifulSoup driver=webdriver.Chrome("C:&bsol ...

Activate the 'click' function for changing data sources

My webpage has a unique feature where checkboxes are generated dynamically from a .csv file. These checkboxes include an additional 'Select All' option created dynamically as well. The functionality I'm aiming for is that upon checking the & ...

Issue with scrolling feature in div

I am currently facing an issue with scrolling on my website. Visit this site to see the problem. When scrolling down, it causes the "hidden" part of the site to slide up. I want to achieve a similar sliding effect, but it needs to be smooth. I have attempt ...

Move your cursor over an image within a div container

After not finding any helpful answers on this topic, I've decided to ask for help again. My issue involves having an image inside a div. <div id ="gratterlendis"> <a href ="?page_id=8"><img src="img/gratterlendis.png" align="right" wid ...

What is the method in jQuery to add a left alignment style to every element on the page?

I need jQuery to automatically add style="text-align: left;" to all elements throughout my webpage. My initial approach would be something like this: $('everything-in-the-page').each( ... However, I am a bit unsure about how to properly target ...

What is the best way to incorporate a for loop in order to create animations with anime.js?

I am currently working on implementing a for loop to create a page loader using JQuery along with the animation framework anime.js var testimonialElements = $(".loader-animation"); for(var i=0; i < Elements.length; i++){ var element = ...

Issues with implementing Bootstrap Collapse functionality on dynamically generated content using Ajax

I am utilizing Ajax to retrieve some [information] which will then be used to set attributes within a duplicated div. Contained within the div is a bootstrap accordion: <div class='accordion-toggle' data-toggle='collapse' data-targe ...

Using jQuery to remove an iframe upon a click event

My goal is to remove an iframe whenever anything on the page is clicked. I tried using this code: $('*').on('click',function(){ $('iframe').remove(); }); However, I encountered a problem where the i ...

How can I align two tags horizontally using HTML and CSS?

I've been diving into HTML and CSS lately and making some progress, but I've hit a snag. I'm trying to create a horizontal ul list with li elements on the same line at the top of the screen, similar to Stack Overflow's layout. Although ...

What is the best way to ensure a dropdown in an input group occupies the full width?

One of the challenges I'm facing involves a row with 2 columns - the first column contains a title and the second column features an input group. Within the input group, there is an icon and a dropdown menu. Unfortunately, the dropdown menu is not oc ...

the menu didn't glide into view

I'm currently working on implementing a stylish menu For this project, I am following a helpful guide that instructs to create the HTML in a single file. The issue is that the menu isn't sliding smoothly as expected. Here is the HTML code - no ...

Tips for Maintaining Consistent Height and Width of Cards

My latest project involves a code snippet with 3 CSS cards displayed in a row using bootstrap. <div class="container mt-4"> <div class="row"> <div class="col-auto mb-3"> ...

How to effectively utilize Python to compare HTML files

Can the HTML be compared differently as shown below? html_1 = "<h1>text<h1>" html_2 = "<h2>text<h2>" Using Google's diff_prettyHtml may not yield accurate results. If I want to say that 1 has changed to 2: <h <del styl ...