I am working on a multi-page HTML document with CSS for printing, specifically targeting the A4 page size. The layout works fine, but the issue I’m facing is that the margin I’ve applied results in an unwanted white area around the content on each page. My goal is to remove this white area and have the background color extend to the full page, including the margin.
CSS Code:
@page {
size: A4;
margin: 1cm;
}
body {
background-color: var(--bg);
}
.page {
width: 21cm;
min-height: 29.7cm;
}
Current Outcome:
The current output displays the content centered within an A4-sized page with a 1cm margin around it. However, this margin creates an unwanted white area around the content on each page, as seen in the following image:
Desired Outcome:
I want to achieve a printed output where the background color extends to the full page, including the margin area. This means that the white area around the content should be removed, and the background color should cover the entire page, while the content remains centered within the page.
What I’ve Tried:
I have attempted to adjust the CSS properties, including setting margins and padding, to remove the white area around the content and have the background color extend to the full page. Unfortunately, these attempts have not yielded the desired outcome.
If anyone has encountered a similar issue or knows how to achieve the desired result, I would greatly appreciate any insights or alternative approaches. Thank you in advance for your help!
2
Answers
There are quite a few reasons why printing a full-page light background color is a bad idea:
So, what to do? The solution is quite easy: Set the background color to white when printing. This can be done using CSS:
This does not affect the background color when looking at your page with the browser.
You can add in any other CSS rule you need. See: @media.
EDIT: Added the normal body rule to show that the
@media
rule should come after it.modified css file:
by using box-shadow property you can avoid unwanted white.