Margin while printing html page

后端 未结 6 1215
栀梦
栀梦 2020-11-28 01:17

I am using a separate style-sheet for printing. Is it possible to set right and left margin in the style-sheet which set the print margin (i.e. margin on paper).

Tha

6条回答
  •  伪装坚强ぢ
    2020-11-28 01:53

    Firstly said, I try to force all my users to use Chrome when printing because other browsers create different layouts.

    An answer from this question recommends:

    @page {
      size: 210mm 297mm; 
      /* Chrome sets own margins, we change these printer settings */
      margin: 27mm 16mm 27mm 16mm; 
    }
    

    However, I ended up using this CSS for all my pages to be printed:

    @media print 
    {
        @page {
          size: A4; /* DIN A4 standard, Europe */
          margin:0;
        }
        html, body {
            width: 210mm;
            /* height: 297mm; */
            height: 282mm;
            font-size: 11px;
            background: #FFF;
            overflow:visible;
        }
        body {
            padding-top:15mm;
        }
    }
    



    Special case: Long Tables

    When I needed to print a table over several pages, the margin:0 with the @page was leading to bleeding edges:

    I could solve this thanks to this answer with:

    table { page-break-inside:auto }
    tr    { page-break-inside:avoid; page-break-after:auto }
    thead { display:table-header-group; }
    tfoot { display:table-footer-group; }
    

    Plus setting the top-bottom-margins for @page:

    @page { 
        size: auto;
        margin: 20mm 0 10mm 0;
    }
    body {
        margin:0;
        padding:0;
    }
    

    Result:

    I would rather prefer a solution that is concise and works with all browser. For now, I hope the information above can help some developers with similar issues.

提交回复
热议问题