div 设置a4大小_CSS设置A4纸张尺寸

I need simulate an A4 paper in web and allow to print this page as it is show on browser (Chrome, specifically). I set the element size to 21cm x 29.7cm, but when I send to print (or print preview) it clip my page.

HTML

Page 1/2
Page 2/2

CSS

body {

margin: 0;

padding: 0;

background-color: #FAFAFA;

font: 12pt "Tahoma";

}

* {

box-sizing: border-box;

-moz-box-sizing: border-box;

}

.page {

width: 21cm;

min-height: 29.7cm;

padding: 2cm;

margin: 1cm auto;

border: 1px #D3D3D3 solid;

border-radius: 5px;

background: white;

box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);

}

.subpage {

padding: 1cm;

border: 5px red solid;

height: 256mm;

outline: 2cm #FFEAEA solid;

}

@page {

size: A4;

margin: 0;

}

@media print {

.page {

margin: 0;

border: initial;

border-radius: initial;

width: initial;

min-height: initial;

box-shadow: initial;

background: initial;

page-break-after: always;

}

}

I think I'm forgetting something. But what would it be?

Chrome: clipping page, double page (it's just what I need it to work)

Firefox: it works perfectly.

IE10: believe it or not, it's perfect!

Opera: very buggy on print preview

解决方案

I looked into this a bit more and the actual problem seems to be with assigning initial to page width under the print media rule. It seems like in Chrome width: initial on the .page element results in scaling of the page content if no specific length value is defined for width on any of the parent elements (width: initial in this case resolves to width: auto ... but actually any value smaller than the size defined under the @page rule causes the same issue).

So not only the content is now too long for the page (by about 2cm), but also the page padding will be slightly more than the initial 2cm and so on (it seems to render the contents under width: auto to the width of ~196mm and then scale the whole content up to the width of 210mm ~ but strangely exactly the same scaling factor is applied to contents with any width smaller than 210mm).

To fix this problem you can simply in the print media rule assign the A4 paper width and hight to html, body or directly to .page and in this case avoid the initial keyword.

@page {

size: A4;

margin: 0;

}

@media print {

html, body {

width: 210mm;

height: 297mm;

}

/* ... the rest of the rules ... */

}

This seems to keep everything else the way it is in your original CSS and fix the problem in Chrome (tested in different versions of Chrome under Windows, OS X and Ubuntu).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值