如何强制浏览器打印 CSS 中的背景图像?
这个问题之前曾被问过,但解决方案不适用于我的情况。我想确保打印某些背景图像,因为它们是页面的组成部分。 (它们不是直接在页面中的图像,因为其中有几个被用作 CSS 精灵。)
同一问题的另一个解决方案建议使用 list-style-image
,它仅在您有每个图标都有不同的图像,不可能有 CSS 精灵。
除了创建一个单独的页面并内嵌图标之外,还有其他解决方案吗?
This question was asked before but the solution is not applicable in my case. I want to make sure certain background images are printed because they are integral to the page. (They are not images directly in the page because there are several of them being used as CSS sprites.)
Another solution on that same question suggests using list-style-image
, which only works if you have a different image for every icon, no CSS sprites possible.
Aside from creating a separate page with the icons inline, is there another solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
使用 Chrome 和 Safari,您可以添加 CSS 样式
-webkit -print-color-调整:精确;
强制打印背景颜色和/或图像的元素With Chrome and Safari you can add the CSS style
-webkit-print-color-adjust: exact;
to the element to force print the background color and/or image默认情况下,浏览器可以选择关闭打印背景颜色和图像。您可以在 CSS 中添加一些行来绕过此问题。
只需添加:
Browsers, by default, have their option to print
background-color
s and images turned off. You can add some lines in CSS to bypass this.Just add:
我找到了一种用 CSS 打印背景图像的方法。这有点取决于你的背景的布局,但它似乎适用于我的应用程序。
本质上,您将
@media print
添加到样式表的末尾,并稍微更改正文背景。例如,如果您当前的 CSS 如下所示:
在样式表的末尾,您添加:
这会将图像作为“前景”图像添加到正文中,从而使其可打印。
您可能需要添加一些额外的 CSS 以使
z-index
正确。但同样,这取决于页面的布局方式。当我无法在打印视图中显示标题图像时,这对我有用。
I found a way to print the background image with CSS. It's a bit dependent on how your background is laid out, but it seems to work for my application.
Essentially, you add the
@media print
to the end of your stylesheet and change the body background slightly.Example, if your current CSS looks like this:
At the end of your stylesheet, you add:
This adds the image to the body as a "foreground" image, thus making it printable.
You may need to add some additional CSS to make the
z-index
proper. But again, its up to how your page is laid out.This worked for me when I couldn't get a header image to show up in print view.
您对浏览器的打印方法几乎没有控制权。您最多可以建议,但如果浏览器的打印设置有“不打印背景图像”,那么您就无能为力,除非重写页面以将背景图像变成恰好位于其他内容后面的浮动“前景”图像。
You have very little control over a browser's printing methods. At most you can SUGGEST, but if the browser's print settings have "don't print background images", there's nothing you can do without rewriting your page to turn the background images into floating "foreground" images that happen to be behind other content.
下面的代码对我来说效果很好(至少对于 Chrome 来说)。
我还添加了一些边距和页面方向控件。(纵向、横向)
The below code works well for me (at least for Chrome).
I also added some margin and page orientation controls.(portrait, landscape)
确保使用 !important 属性。这极大地增加了打印时保留样式的可能性。
Make sure to use the !important attribute. This dramatically increases the likelihood your styles are retained when printed.
就像 @ckpepper02 所说, body content:url 选项效果很好。然而我发现,如果你稍微修改它,你就可以使用它来使用 :before 伪元素添加各种标题图像,如下所示。
这会将图像滑到页面顶部,根据我有限的测试,它适用于 Chrome 和 IE9
-hanz
Like @ckpepper02 said, the body content:url option works well. I found however that if you modify it slightly you can just use it to add a header image of sorts using the :before pseudo element as follows.
That will slip the image at the top of the page, and from my limited testing, it works in Chrome and the IE9
-hanz
使用伪元素。虽然许多浏览器会忽略背景图像,但其内容设置为图像的伪元素在技术上不是背景图像。然后,您可以将背景图像大致定位在图像应该放置的位置(尽管它不像原始图像那么简单或精确)。
一个缺点是,要在 Chrome 中使用此功能,您需要在打印媒体查询之外指定此行为,然后使其在打印媒体查询块中可见。所以,像这样......
缺点是图像通常会在正常页面加载时下载,增加不必要的体积。您可以通过仅使用与原始可见图像相同的图像/路径来避免这种情况。
Use psuedo-elements. While many browsers will ignore background images, psuedo-elements with their content set to an image are technically NOT background images. You can then position the background image roughly where the image should have gone (though it's not as easy or precise as the original image).
One drawback is that for this to work in Chrome, you need to specify this behavior outside of your print media query, and then make it visible in the print media query block. So, something like this...
The drawback is that the image will often be downloaded on normal page loads, adding unnecessary bulk. You can avoid that by just using the same image/path you'd already used for the original, visible image.
默认情况下,浏览器可以选择关闭打印背景颜色和图像。您可以在 CSS 中添加一些行来绕过此问题。只需添加:
注意:它不适用于整个主体,但您可以将其指定为内部元素或容器 div 元素。
Browsers, by default, have their option to print background-colors and images turned off. You can add some lines in CSS to bypass this. Just add:
Note: It's not working on the entire body but you could speciy it for a inner element or a container div element.
当您向背景图像添加 !important 属性时,它可以在 google chrome 中工作
确保您先添加属性并重试,您可以这样做
}
it is working in google chrome when you add !important attribute to background image
make sure you add attribute first and try again, you can do it like that
}
您可以使用固定颜色的边框。
对于渐变背景,您可以使用:
You can use borders for fixed colors.
and for gradient background you can use:
https://gist.github.com/danomanion/6175687 提出了一个优雅的解决方案,使用自定义项目符号代替背景图像。在此示例中,目标是将背景图像应用到具有
logo
类的a
元素。 (您应该将这些替换为您想要设置样式的元素的标识符。)通过将其包含在
块中,我可以将屏幕上呈现为背景图像的白色透明徽标替换为用于打印的透明黑色徽标。
https://gist.github.com/danomanion/6175687 proposes an elegant solution, using a custom bullet in place of a background image. In this example, the aim is to apply a background image to an
a
element with classlogo
. (You should substitute these for the identifier of the element you wish to style.)By including this within a
block, I'm able to replace a white-on-transparent logo on the screen, rendered as a background-image, with a black-on-transparent logo for print.
您只需在打印时选中打印设置中的“背景图形”复选框即可在打印页面上显示背景图像。
You simply had to check the "Background Graphics" checkbox in the print settings while printing to display your background image on the printed page.
你可以做一些这样的技巧:
在 body 标签中:
You can do some tricks like that:
In body tag: