Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS page headers - how to use print margins?

I can get a header to print on each page, but I'm new to print margins. I thought the @page css would work, but it does not seem to affect page margins. If I set the margins on the body, it works on page one, but subsequent pages start the top margin at the default, putting the header over top of the text.

<style>  
.header {  
 position: fixed;  
 top: 0;  
}  
@page {  
 size: 11in 17in;  
 margin-left: 1in;  
 margin-right: 1in;  
 margin-top: 1in;  
 margin-bottom: 1in;  
} 
</style>

<body>  
<span class=header>This is the header</span>  
This is the text of the document. (repeat until I get to page 2)  
</body>
like image 327
Ted Scheckler Avatar asked Dec 03 '10 17:12

Ted Scheckler


2 Answers

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.

like image 111
GlennG Avatar answered Oct 08 '22 17:10

GlennG


The accepted answer which is now 4 1/2 years old says:

"The short answer is to avoid HTML/CSS printing if you need to ensure a specific layout."

These days you may do HTML/CSS printing for comparatively simple layouts in browsers or if you use a tool like wkhtmltopdf you can go for more complex layouts. See also http://www.toccon.com/toc2013/public/schedule/detail/26714

like image 25
z-- Avatar answered Oct 08 '22 17:10

z--