Margin while printing html page
You should use cm
or mm
as unit when you specify for printing. Using pixels will cause the browser to translate it to something similar to what it looks like on screen. Using cm
or mm
will ensure consistent size on the paper.
body
{
margin: 25mm 25mm 25mm 25mm;
}
For font sizes, use pt
for the print media.
Note that setting the margin on the body in css style will not adjust the margin in the printer driver that defines the printable area of the printer, or margin controlled by the browser (may be adjustable in print preview on some browsers)... It will just set margin on the document inside the printable area.
You should also be aware that IE7++ automatically adjusts the size to best fit, and causes everything to be wrong even if you use cm
or mm
. To override this behaviour, the user must select 'Print preview' and then set the print size to 100%
(default is Shrink To Fit
).
A better option for full control on printed margins is to use the @page
directive to set the paper margin, which will affect the margin on paper outside the html body element, which is normally controlled by the browser. See http://www.w3.org/TR/1998/REC-CSS2-19980512/page.html.
This currently works in all major browsers except Safari.
In Internet explorer, the margin is actually set to this value in the settings for this printing, and if you do Preview you will get this as default, but the user can change it in the preview.
@page
{
size: auto; /* auto is the initial value */
/* this affects the margin in the printer settings */
margin: 25mm 25mm 25mm 25mm;
}
body
{
/* this affects the margin on the content before sending to printer */
margin: 0px;
}
Related answer:
Disabling browser print options (headers, footers, margins) from page?
How to control page margin when printing HTML document?
It's not margin between pages but page margin. It's controlled by a specific CSS media @page
:
@page {
margin: 1cm;
}
Please refer to linked specs for all the details (especially about how @page
combines with, for example, <body>
margins).
Note that with pseudo-selectors you can control the aspect of :left
, :right
and :first
pages (just to mention few common use cases).
CSS print a custom sized page margin when the content is on multiple pages
Please try this:
@media print {
body {
display:table;
table-layout:fixed;
padding-top:2.5cm;
padding-bottom:2.5cm;
height:auto;
}
}
Related Topics
Flexbox Layout Does Not Work in Internet Explorer 10
How to Replace Color of Png Image Using CSS
Responsive Image Align Center Bootstrap 3
How to Set a Microdata Image Property, Without Letting the Browser Download the Image
How to Insert Spaces/Tabs in Text Using HTML/Css
How to Make a Border Overlay Child Div
How to Calculate Required Hue-Rotate to Generate Specific Colour
Label Text Is Wrapped But Not Indent Second Line
How to Strike Through Obliquely with CSS
Line Height Issue with Inline-Block Elements
Bash Script to Convert from HTML Entities to Characters
Is There a CSS Selector for Selecting an Element Futherup in the HTML
Generate Scroll Bar for Overflow to the Left
How to Align Footer (Div) to the Bottom of the Page
How to Bookmark or Link to a Section of a Page Without an Anchor