CSS Page Headers - How to Use Print Margins

CSS page headers - how to use print margins?

Printing support by all browsers is very poorly supported with horrendous bugs in many popular browsers that have gone unfixed for years.

The short answer is to avoid HTML/CSS printing if you need to ensure a specific layout. Use PDF, possibly dynamically generated on-demand. There's various PDF generator APIs such as iTextSharp. It's possible to print from Flash, but only if Flash is installed and working (i.e. no Flashblock, iOS).

HTML/CSS printing should be restricted to simple layouts. Form printing is a nightmare with fieldset & legend support being especially problematic (particularly on Firefox). Interestingly printing support is best on the internet explorers.

The CSS3 printing support specification hasn't been completed and is some time off.

General principles:

  • No backgrounds or background CSS images are supported (by default - users can change their browser settings for an intranet application). Only foreground images print.

  • Widths need to be fluid as page sizes vary around the planet. US Letter format is shorter and wider than A4 layout

  • All browsers support printing in different ways. Bugs are legion.

  • Test using print preview.

Page Margins for printing in CSS

Both

 @page {
margin-top: 5cm;
margin-bottom: 5cm;
}

and

@media print {
body {margin-top: 50mm; margin-bottom: 50mm;
margin-left: 0mm; margin-right: 0mm}
}

work fine in Firefox 35

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?



Related Topics



Leave a reply



Submit