HTML CSS 打印页面去除顶部和底部的空白

标签 html css

我正在尝试打印一个 HTML 页面,但我很难摆脱页面顶部和底部的多余空白区域。

这是打印预览的样子(箭头指向空格):

enter image description here

和我的 CSS:

@@media print {
        body {
            margin: 5px;
        }

        .body.menubar-top {
            padding-top: 0px;
        }

        .no-print {
            display: none;
        }

        #section-to-print, #section-to-print * {
            visibility: visible;
            margin-top: 0px;
            padding-top: 0px;
        }

        #section-to-print {
            /*position: absolute;*/
            /*left: 0;
            top: 0;*/
        }
    }

和页面代码:

    <div class="row" id="section-to-print" style="vertical-align:top">

    <div class="col-md" id="projectDetails">

        <div class="col-md-12">
            <h3 class="headerTitle">
                Main Title
            </h3>
        </div>

        <div class="col-md-12">
            <h4 class="headerTitle">
                Sub title
            </h4>
        </div>

        <div class="row">
            <div class="col-md-12">
                <h5>
                    Project Details
                </h5>
            </div>
        </div>

etc......

我已经有一段时间没有创建打印页面了,但我相信这应该很简单。

最佳答案

您的代码看起来完全有效。我用你的代码制作了 JSFiddle:

https://jsfiddle.net/a6fu1vcg/2/

例如,如果您有例如代码:

body {
  margin: 200px;
}

当您尝试打印它时,它将被覆盖:

@media print {
        body {
            margin: 5px;
        }
}

并且打印文档将有 5px 的正文边距。

请确保您没有任何其他样式,例如 !important 会覆盖您的打印样式

关于HTML CSS 打印页面去除顶部和底部的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52320143/

相关文章:

html - 如何在响应式电子邮件模板中堆叠列?

javascript - 最小化窗口时标签的文本与文本区域重叠

html - CSS 问题 : fixed footer with width of 100% is too wide

css - 我怎样才能简单地添加一个 css 文件来更改我 Shiny 的应用程序的背景颜色

html - 如何按基线垂直对齐文本和图像?

asp.net - 从分层 sql 数据返回无序列表

jquery - 我可以允许用户仅使用 JQuery 来保留对 HTML 页面的更改吗?

javascript - 如何使用 JavaScript 缩放图像以适应

jquery - Bootstrap 进度条; 1%不可能?

javascript - 使用 PHP 和 JQuery 的响应式条件链接/按钮