无论您在打印对话框窗口中选择纵向还是横向,我都试图制作一个打印时占 A4 页面 100% 的 div。
test.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="test.css" type="text/css"/>
<title>Title</title>
</head>
<body>
<div class="page"></div>
</body>
</html>
test.css
html, body {
padding: 0;
margin: 0;
}
@media print and (orientation: portrait) {
.page {
height: 297mm;
width: 210mm;
background: red;
}
}
@media print and (orientation: landscape) {
.page {
height: 210mm;
width: 297mm;
background: red;
}
}
预期:在横向和纵向模式下,div 将在打印预览中占据 A4 页面的 100%。
实际:div 仅在横向模式下占据页面的 100%。在纵向模式下,它只占页面的 60%。
如果我为纵向模式指定 height: 420mm;width: 300mm;
,它会在两种模式下占据 100% 的页面,但这似乎不是一个合适的解决方案。
如果我只是删除横向规则,div 将开始以纵向模式占据页面的 100%。
浏览器是 Ubuntu 16.04 上的 Chrome 54.0.2840.100(64 位)。
为什么这段代码没有达到预期的效果,如何达到预期的效果?
谢谢
最佳答案
我遇到了这个问题,然后这样解决了:
横向打印:
@media print {...}
对于纵向打印:
@media print and (max-width: 40em) {...}
我希望有人找到了更清洁的解决方案。
关于html - @media 打印和(方向 :portrait) does not work if (orientation:landscape) is also specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386632/