How to Remove Unwanted White Space in Printed HTML Divs?
Introduction When printing an HTML , many users encounter unexpected white space at the top and bottom of their printed output. This often occurs due to default browser print styles or CSS margin settings. In this article, we will explore why this issue happens and how to effectively remove the unwanted white space, specifically aiming to create a clean output, reminiscent of a POS receipt. Understanding the Issue The presence of unwanted white space during printing can be attributed to various factors: Browser Default Styles: Most browsers have predefined styles for printing that include margins and padding, which can add extra space. CSS Styles: If your or its child elements have set margins or padding, these can extend the printed area beyond what is desired. Page Breaks: CSS can automatically add page breaks that lead to unwanted spacing in printed content. By addressing these issues with customized CSS for printing, you can achieve a more precise layout. Step-by-Step Solution to Remove White Space Here’s a focused approach to eliminate unwanted white space when printing the content of a specific . Step 1: Set Up Your HTML Structure Make sure your HTML structure is organized. Here’s an example of a simple receipt layout: Job Sheet My Company Name My Address here 1234567890 Date: 000-00-00 00:00:00 T & C : NO Responsibility of data lost during service all Subject To Gwalior jurisdiction Step 2: Apply CSS for Printing Add the following CSS to customize your print layout: @media print { @page { size: 100mm 150mm; margin: 0; } body { margin: 0; } #receipt { margin: 0; padding: 0; } #receipt * { margin: 0; padding: 0; } } Explanation: The @page rule sets the page size and removes default margins. Setting body { margin: 0; } eliminates the browser’s default margin, ensuring the print area starts and ends where desired. Applying margin: 0; padding: 0; to the #receipt and its children ensures there’s no extra space added. Step 3: Modify the Print Function Ensure your JavaScript print function is generating content correctly without extra space around it: function printdiv(printpage) { var headstr = ""; var footstr = ""; var newstr = document.getElementById(printpage).innerHTML; var oldstr = document.body.innerHTML; document.body.innerHTML = headstr + newstr + footstr; window.print(); document.body.innerHTML = oldstr; return false; } Key Considerations: Use document.getElementById() instead of the old method for better performance and readability. The function constructs an HTML string that only includes the desired content for printing. Frequently Asked Questions (FAQs) How do I ensure my printed output does not get cut off? To prevent content from being cut off, ensure that your page size in the @page rule accurately reflects the dimensions of your receipt printer or paper size. Additionally, make sure there is no overflow in any child elements. Can I cancel page breaks? Yes, you can use CSS properties such as page-break-inside: avoid; on tables or divs to prevent breaking of elements during printing. Why is my receipt still not printing correctly? Double-check your CSS for any conflicting styles that may override your print styles and ensure that your browser's print settings are set to use the correct paper size without scaling. Conclusion By understanding how CSS affects printing and implementing specific print styles, you can successfully eliminate unwanted white space from your printed output. This tutorial provided a detailed code example designed to help you craft a clean and professional receipt format that mirrors the functionality of a POS system.

Introduction
When printing an HTML The presence of unwanted white space during printing can be attributed to various factors:
By addressing these issues with customized CSS for printing, you can achieve a more precise layout.
Here’s a focused approach to eliminate unwanted white space when printing the content of a specific Make sure your HTML structure is organized. Here’s an example of a simple receipt layout:
Add the following CSS to customize your print layout:
Ensure your JavaScript print function is generating content correctly without extra space around it:
To prevent content from being cut off, ensure that your page size in the Yes, you can use CSS properties such as Double-check your CSS for any conflicting styles that may override your By understanding how CSS affects printing and implementing specific print styles, you can successfully eliminate unwanted white space from your printed output. This tutorial provided a detailed code example designed to help you craft a clean and professional receipt format that mirrors the functionality of a POS system. Understanding the Issue
Step-by-Step Solution to Remove White Space
Step 1: Set Up Your HTML Structure
My Company Name
My Address here
1234567890
Date:
000-00-00 00:00:00
Step 2: Apply CSS for Printing
@media print {
@page {
size: 100mm 150mm;
margin: 0;
}
body {
margin: 0;
}
#receipt {
margin: 0;
padding: 0;
}
#receipt * {
margin: 0;
padding: 0;
}
}
Explanation:
@page
rule sets the page size and removes default margins.body { margin: 0; }
eliminates the browser’s default margin, ensuring the print area starts and ends where desired.margin: 0; padding: 0;
to the #receipt
and its children ensures there’s no extra space added.Step 3: Modify the Print Function
function printdiv(printpage) {
var headstr = "
Key Considerations:
document.getElementById()
instead of the old method for better performance and readability.Frequently Asked Questions (FAQs)
How do I ensure my printed output does not get cut off?
@page
rule accurately reflects the dimensions of your receipt printer or paper size. Additionally, make sure there is no overflow in any child elements.
Can I cancel page breaks?
page-break-inside: avoid;
on tables or divs to prevent breaking of elements during printing.
Why is my receipt still not printing correctly?
print
styles and ensure that your browser's print settings are set to use the correct paper size without scaling.
Conclusion