I would like to show an alternate image in print.css. That is, when they click on the browser’s print button, I want to swap out an image for a fuller picture for output.
I tried using background-image:url() !important in the print.css but that doesn’t work (I suppose it is standard not to print background images).
I know I can put the alternate image in html and do a display: hide for that page’s styles.css, then for the print.css display: block it and display: hide the original. The problem is that the page will load both images, and the alternate is a larger image and will slow down loading on mobile.
Is there some way to swap an alternate image when a browser’s Print button is used?
In my tests (in Chrome) the image is not loaded the first time you try and print the page. If you try a second time then the image appears.
The image seems to need to be in cache before the print is selected. The only way I can see that that will happen is by loading it by default in the main stylesheet and hiding it offscreen and then just revealing it in the print stylesheet. This of course means the image is loaded when it may never be needed.
I tried moving it out into a CSS file both as a media=“print” and a file with @media but still no joy.
I’m thinking if this approach is to be of any value for counting user “print” actions, some server-side stuff will need to be done.
And the success of that depends on when exactly the assets do get called in.
Dev tools shows the status as “pending” the anonymous function “window.print()”
So it looks like the more reliable way would be using JavaScript to test for onbeforeprint / window.matchMedia('print')
Not sure if I got something wrong in the syntax, or it just doesn’t work and browsers are lazy and print what is there already, rather than re-loading for the preview.
I thought I may be on the right track with this, but no.
This small change solves the issue for all three major browsers: content: url(images/top-down.jpg);
It no longer requires printing twice in Chrome. Here’s how I use it (I want the cropped top-down.350.jpg on the web page to be replaced with top-down.jpg in print):
No that’s not working I’m afraid and the print image only shows the second time around as already demonstrated in my previous example. Changing the url path is not going to make a difference to this issue (unless of course you point to the wrong place).
You need to clear your cache to test.
Also why are you linking to 2 print stylesheet files that are almost the same?
Note that because you have media = print in the link tag then there’s no point specifying a media in the actual css file.