Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS print a custom sized page margin when the content is on multiple pages

Tags:

html

css

printing

I'm trying to print an HTML page that might be long and contain text paragraphs and tables. My goal is to define a print margin for every page. I added the following CSS to my page:

@media print {
    body {
        margin: 2.5cm 0;
    }
}

But the problem that I have is that the margin gets added only to the beginning and end of the document and NOT to every single page. As you can see from the attached image I have a margin top in the first page but not in the second one, my desired behavior is something similar to a book or a word document (where you define page margins). Is it possible to do it in CSS?

issue visibile in the print preview screen

like image 641
MixPower Avatar asked May 04 '16 16:05

MixPower


People also ask

How do I set print margins in HTML?

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.

How do you give a print margin?

To specify custom page margins, click Custom Margins and then—in the Top, Bottom, Left, and Right boxes—enter the margin sizes that you want. To set header or footer margins, click Custom Margins, and then enter a new margin size in the Header or Footer box.


2 Answers

CSS for Printed Page Margins

@page {
  margin: 1in; // margin for each printed piece of paper
}

@page :first {
  margin-top: 2in; // top margin for first page of paper
}

@media print {
  body {
    margin: 0; // 
  }
}

Print Dialog Settings

In the print preview dialog, ensure that "Margins" is set to "Default".

If "Margins" are set to "None" (or other non-"Default" option) the @page settings will be ignored.

Browser Support

@page is not supported in all browsers. Learn more about @page.

like image 62
Beau Smith Avatar answered Sep 20 '22 12:09

Beau Smith


Please try this:

@media print {
   body {
   display:table;
   table-layout:fixed;
   padding-top:2.5cm;
   padding-bottom:2.5cm;
   height:auto;
    }
}
like image 22
satya Avatar answered Sep 21 '22 12:09

satya