Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page Margins for printing in CSS

Tags:

html

css

printing

I searched a lot before asking here, but didn't find an answer for my question.

I want to insert top and bottom margin for my website page when printed, so I used the normal margin-top and margin-bottom for the printed div but it affected on the first sheet only! so I used this as explained in W3C CSS2.1 Specifiction:

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

but no effect in Firefox Print Preview or Print to PDF. so how can I insert top and bottom margin (for each printed sheet) via CSS? or is there any trick to do this in Firefox?

like image 506
Anass Ahmed Avatar asked Feb 17 '11 02:02

Anass Ahmed


People also ask

How do I set print margins in CSS?

The dimensions of the page box are set with the 'size' property. The dimensions of the page area are the dimensions of the page box minus the margin area. You can use the margin, margin-top, margin-bottom, margin-left, and margin-right properties within the @page rule to set margins for your page.

What should my margins be for printing?

For best results, print within the recommended printable area. The printer may not print outside this area. The minimum top margin is 4.2 mm (0.17 inch). The minimum left and right margins are 3.0 mm (0.12 inch) each.

How do you write a print page in CSS?

You can use CSS to change the appearance of your web page when it's printed on a paper. You can specify one font for the screen version and another for the print version. You have seen @media rule in previous chapters. This rule allows you to specify different style for different media.


2 Answers

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

like image 176
z-- Avatar answered Sep 18 '22 12:09

z--


This should work even in firefox

 @media print {
     #main{margin:100px 0;}
 }

The "@media print" will make sure the style is only used when printing.

like image 25
jansmolders86 Avatar answered Sep 19 '22 12:09

jansmolders86