html - @media 打印和(方向 :portrait) does not work if (orientation:landscape) is also specified

标签 html css media-queries

无论您在打印对话框窗口中选择纵向还是横向,我都试图制作一个打印时占 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/

相关文章:

html - CSS 断点不起作用

javascript - 查找由 rgba 颜色创建的十六进制颜色

html - Cocoa WebView 不会在 OSX 10.8 上渲染所有图像

javascript - ReactJS有函数重复运行

css - 替代/深/

html - GWT 剪辑图像

jquery - 分步设置 div 高度以适应平铺背景

html - 为什么只有一个媒体查询不起作用?

header 中的 css3 媒体查询不起作用

javascript - 将 div 或 svg 转换为 datauri