CSS css打印样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7086342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:20:01  来源:igfitidea点击:

css print styling

cssprintingmargin

提问by php-b-grader

I have a page which displays fine on the screen.

我有一个在屏幕上显示正常的页面。

I have a css which then formats the screen for print and re-sizes the sections.

我有一个 css,然后它会格式化屏幕以进行打印并重新调整各个部分的大小。

My problem is that the print layout has a margin of approximately an inch from the left of the page which makes 2 elements print off the page on the right hand side.

我的问题是打印布局距页面左侧大约有 1 英寸的边距,这使得 2 个元素从页面右侧打印出来。

I could probably compress the contents from the right but I want to know if I can reduce the margin on the left (and basically center the contents)

我可能可以从右侧压缩内容,但我想知道是否可以减少左侧的边距(基本上将内容居中)

I have set the body tag on print to margin:0;padding: 0;but this has no effect?

我已将打印时的正文标签设置为,margin:0;padding: 0;但这没有效果?

Is there another setting that controls print margins?

是否有其他设置可以控制打印边距?

回答by Brock Adams

You can set the print margin (and landscape orientation) with CSS like:

您可以使用 CSS 设置打印边距(和横向),例如:

@media print {
    @page {
        size: letter landscape;
        margin: 4.0cm;
    }
}


And the good news is, it works! (On Chrome. Not supported on other browsersthough I did not test IE9.)


好消息是,它有效!(在 Chrome 上。 虽然我没有测试 IE9,但在其他浏览器上不受支持。)

The margin must be in a unit that makes sense in print -- pixels are not allowed.

边距的单位必须在打印时有意义——不允许使用像素。

Chrome will not shrink the margin below a fixed minimum, which may be determined by the printer driver.

Chrome 不会将边距缩小到固定最小值以下,这可能由打印机驱动程序决定。

回答by Jason Gennaro

You could also try to set

你也可以尝试设置

margin:0;and padding:0;

margin:0;padding:0;

to the html, main content divand ptags

html,main content divp标签

and see if that helps.

看看这是否有帮助。

Otherwise, your best bet is to set a specific widthon your elements.

否则,最好的办法是为width您的元素设置一个特定的。