Chrome seems to miscalculate css sizes given in mm unit when printing

我的未来我决定 提交于 2021-01-27 20:51:40

问题


I want to print to A4 using css.

A4 is 297mm high - 6 mm margin top - 6 mm margin bottom = 285 mm should be left for content.

In IE 11 : I have to set page borders to 6 mm and disable header/footer --> Text appears where it should when I set content height to 284 mm (1 mm diff is close enough for me) IE 11: OK: Text at 284 mm is located at bottom of page 1

In Chrome 33: I don't have to change any settings --> Text is located far below from where it should be. It only works when I set content height to 267 mm (where did 18 mm diff go?) Chrome 33: Fail: Text at 284 mm is located somewhere near top of page 2

<!DOCTYPE html>
<html>
   <head>
      <style type="text/css">
         @page {
            margin: 6mm;
            size: A4 portrait;
         }
         body {
            margin: 0mm;
         }
         table {
            page-break-after: always;
            border-spacing:0mm;
         }
         tr {
            vertical-align: bottom;
            height:284mm;
         }
      </style>
   </head>
   <body>
      <table>
         <tr>
            <td>Page 1</td>
         </tr>
      </table>
      <table>
         <tr>
            <td>Page 2</td>
         </tr>
     </table>
   </body>
</html>

Is there something wrong with my css or does Chrome not know the size of 1 mm?


回答1:


I figured it out just now. It is not enough to set height, I need to set width as well. (I still need to set height to 284 instead of 285, but that is ok (probably a rounding error))

tr {
    vertical-align: bottom;
    height:284mm;
    width:198mm;
}


来源:https://stackoverflow.com/questions/22024144/chrome-seems-to-miscalculate-css-sizes-given-in-mm-unit-when-printing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!