How can CSS be used to print multiple pages without any margins? And what is causing this slight extra height in the output

Check out the Live Demo here, apologies for the messy CSS as I have been experimenting with different styles.

I am currently attempting to print PDF files using the browser's print dialog. Each file consists of 4 pages in the DIN lang format with an additional 3mm on each side.

In the HTML code below, each .page represents a physical page with dimensions outlined above. The printable area is precisely the DIN lang measurements (105mm x 210mm) centered on the page.

Despite my efforts, I am struggling to make the .page element fit perfectly into the parameters set in @page. There seems to be a slight excess height that becomes more noticeable when additional pages are added. When I include page-break-after: always;, it appears that the first page is already too tall (resulting in what seems like a blank page following it).

https://i.sstatic.net/myT67.png

Answer №1

It took me hours of experimenting with different setups and scouring the internet for answers on various platforms before finally stumbling upon a workaround that, while not perfect, is currently the most effective solution available.

To implement this fix, simply add the following code snippet to your print media query:

@media print {
  body {
     border: 1px solid transparent;
  }
}

Complete code for execution:

window.print();
/* apply a natural box layout model to all elements, but allowing components to change */
html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

html,
body {
  padding: 0;
  margin: 0;
  height: fit-content;
}

.print-area {
  background-color: #ffffff;
  width: 105mm;
  height: 210mm;
  font-size: 12pt;
  display: none;
}

.page {
  display: block;
  height: 216mm;
  width: 111mm;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  font-size: 0;
  margin: 0;
  padding: 0;
  border: 0;
  box-shadow: 0;
  /* page-break-after: always; */
}

#page-one {
  background-color: rgb(206, 135, 208);
  top: 0;
  left: 0;
}
#page-two {
  background-color: rgb(78, 73, 212);
}
#page-three {
  background-color: rgb(232, 227, 151);
}
#page-four {
  background-color: rgb(118, 196, 124);
}

@media print {
  body {
    border: 1px solid transparent;
  }
}

@page {
  size: 111mm 216mm;
  margin: 0 !important;
}
<div class="page" id="page-one">
  <div class="print-area">
    <h1>Hello from page 1</h1>
  </div>
</div>
<div class="page" id="page-two">
  <div class="print-area">
    <h1>Hello from page 2</h1>
  </div>
</div>
<div class="page" id="page-three">
  <div class="print-area">
    <h1>Hello from page 3</h1>
  </div>
</div>
<div class="page" id="page-four">
  <div class="print-area">
    <h1>Hello from page 4</h1>
  </div>
</div>

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

Troubleshooting Media Queries Problems in HTML and CSS

Check out the code snippet below: //Modifying text content (function() { var texts = $(".altered"); var textIndex = -1; function displayNextText() { ++textIndex; var t = texts.eq(textIndex) .fadeIn(2000) if (textIndex < te ...

Distributing elements evenly across the parent width using Angular FlexLayout for a responsive design

I need to create 3 div squares: div-left, div-middle, and div-right, each with a width of 300px. These squares should be arranged horizontally within the parent div main-section, which will adjust its size based on the screen width, being 1300px if the scr ...

Verify the accurate sequence for arranging the items in the drag and drop list

I am currently developing a Language learning platform that includes several web applications. I am still new to javascript and struggling to find a solution for one of my apps. This specific app involves draggable list items that need to be arranged in t ...

Is it possible to precisely position multiple children in a relative manner?

I am currently developing a custom dropdown menu where I want the parent element to be relatively positioned within the page. The goal is for all the child elements of the dropdown menu (the list items) to appear as if they are part of a select element and ...

Issue with Responsive Behavior of Multi-Row Cards in Bootstrap 4

My goal is to create responsive rows of cards with some margin, but I am running into issues. When I use margin:5px; on my cards, the row overflows and they are not centered correctly. There seems to be extra space on the right side of the cards. How can I ...

HTML5: Enhancing Video Playback on the iPad with Custom Zoom Controls

I've customized a smaller UIWebView specifically for the iPad, and I've created my own HTML5 controls for the video playback. However, when I try to maximize the video, all I see is a black screen instead of the actual content. The audio still pl ...

Explore the possibilities with Intel XDK's customizable keyboard feature

I recently started using Intel XDK for development and I encountered the following issue: I have an input text field (HTML) and I need to restrict user input to only numbers, decimals, and negative sign when they click on the field. How can I achieve this ...

Problem with loading image from local path in Angular 7

I'm having trouble loading images from a local path in my project. The images are not rendering, but they do load from the internet. Can someone please help me figure out how to load images from a local path? I have already created a folder for the im ...

The particular division is failing to display in its designated location

This is quite an interesting predicament I am facing! Currently, I am in the process of coding a website and incorporating the three.js flocking birds animation on a specific div labeled 'canvas-div'. My intention is to have this animation displa ...

Expanding your knowledge of AngularJS: utilizing ng-click to interact with elements

After a user clicks on an element in my app, I have some logic that runs. For example: html <h3 ng-click="showAlert('text')">this is text! (try to click and select)</h3> js $scope.showAlert = function(text) { console.log(' ...

Implementing HTML rendering in a PyQt5 window

As a newcomer to PyQt5 for GUI creation, I could use some assistance. I'm attempting to create a basic window that displays a simple HTML file. However, despite using the code provided below, the window shows up empty. Is there a step I missed in ord ...

Tips for customizing the appearance of a label when a MUI Radio Button is selected

Hello everyone, I am attempting to customize the label text color of a radio button to turn blue when selected. https://i.stack.imgur.com/btSc2.jpg HERE IS THE CODE FOR MY MUI BUTTON SO FAR import * as React from "react"; import Radio from &quo ...

Tips for creating dynamic animations in an opencart dropdown menu

Currently, I am in the process of enhancing my ecommerce website using OpenCart and I have been trying to incorporate a simple animation for the dropdown menu. However, despite applying the transition properties in the code snippet below, it seems that the ...

Issue in Less.js: the 'min' function encountered an error due to incompatible types

In my _main.less file, I have the following styling: #mapid { width: 100%; min-height: min(65vh, 500px); max-height: 500px; } However, when using the Less compiler with grunt, it throws an error: >> File "public/less/_main.less" chan ...

`Scrolling through a horizontal menu with no scrollbar present`

Is there a way to create a horizontal menu that can scroll left or right without the scrollbar? I'm looking for a solution like adding on-screen arrow buttons or implementing mouseover functionality. What would be the best approach? div.scrollmenu ...

When resizing the browser, HTML/CSS elements tend to shift positions :(

I've experimented with different position values like absolute, fixed, and static without success. Wrapping the element in a div didn't yield the desired result, so I'm seeking assistance to correct this. (If that is the solution) The issu ...

Displaying a loading animation within a specific div element

Is it possible to use jQuery to insert a div into another div? My goal is to replace the contents of one div with a loading div while waiting for an ajax call to return. <div id="content1">foo</div> <div id="content2">bar</div> < ...

Obtain consistent outcomes by entering identical data in both Ajax and PHP

My code takes a number input in a <textarea>, then uses AJAX to validate it randomly with PHP (using rand()). The validated number is returned along with the words correct or incorrect to a <div> in HTML. The issue is that if the same number i ...

Guide to displaying WordPress functions within an accordion in my loop

Currently, I'm working on implementing a vertical accordion feature that will display blog posts within each section. The idea is to have 5 of the most recent posts displayed in the accordion, with each post showing the date (day/month/year) and title ...

Having trouble with the button's functionality in Internet Explorer?

Recently, I encountered an issue with a "dropdown button" that I created using only CSS and HTML. Surprisingly, it works flawlessly in Chrome and FireFox, but seems to have some glitches when tested on Internet Explorer (IE). The "New Invoice" button fails ...