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?
The below code works well for me (at least for Chrome).
I also added some margin and page orientation controls.(portrait, landscape)
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.
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 imageMake sure to use the !important attribute. This dramatically increases the likelihood your styles are retained when printed.
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.