40

Is there any way to set ff and ie to print background images?

I am using stars image to classify some skills and I set it as a background image and positioning to either set one start, two, three, etc. When I try to print the page the images disappear.

So is there any way to make them appear when I print the page or at least have a way of replacing the images with * or something that would be visible?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
AntonioCS
  • 8,335
  • 18
  • 63
  • 92
  • 7
    It may look like a browser question but this is a css question. – Ross Feb 27 '09 at 22:22
  • 3
    @Ross, perhaps, but since this kind of CSS can be browser-specific (i.e. -webkit-print-color-adjust:exact; works for Chrome v17+ but not FF or IE) the answer in this case might revolve around print options provided by the applications themselves. – Charlie Schliesser Jul 05 '12 at 15:30

8 Answers8

22

Have you considered using a print stylesheet? This could allow you to do something like:

<div class="star">*</div>

/* media:screen */
.star {
    background: ...;
    overflow: hidden;
    text-indent: 9999em;
}

/* media:print */
.star {
    text-indent: 0;
}

or even easier:

<div class="star"><img src="./images/star.jpg" alt="*" /></div>

/* media:screen */
.star img {
    visibility: hidden;
}

/* media:print */
.star img {
    visibility: visible;
}

You can specify stylesheets browsers should use by supplying a media tag, either by css or on the link element:

<link rel="stylesheet" type="text/css" href="main.css" media="screen" />
<link rel="print stylesheet" type="text/css" href="print.css" media="print" />
Ross
  • 46,186
  • 39
  • 120
  • 173
  • 11
    With this approach I might as well have the images instead of the background image. I use the background image because it's a lot easier to have one image and then position then have 10 images – AntonioCS Feb 27 '09 at 22:40
  • Can you tell me how to set background image in table for printing in paper.I tried like this but not working please check and guide me,please see my SO question http://stackoverflow.com/questions/17973414/table-background-image-not-display-on-printing-paper – Monk L Aug 01 '13 at 14:58
19

In Firefox, go to File => Page Setup. There is a checkbox for "Print Background (colors & images)". Just check that and you should be all set.

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
18

In your print.css file change the background-image to a list item.

So:

.background {
  display: list-item;
  list-style-image: url(yourbackgroundimage.gif);
  list-style-position: inside;
}

This method is described more here: http://www.web-graphics.com/mtarchive/001703.php

sth
  • 222,467
  • 53
  • 283
  • 367
  • Seems to work in Firefox 5, but you need to use a separate rule for every image. CSS sprite maps are not possible. – DisgruntledGoat Jul 12 '11 at 19:49
  • I found this seemed to work in Chrome, but not Firefox... then I discovered that after I surrounded the image URL in quotes, it worked in Firefox as well. – Alex D Feb 20 '13 at 22:23
  • 3
    The link in the answer does not work, but I was able to find it on the Wayback Internet Archive: http://web.archive.org/web/20101205163530/http://web-graphics.com/mtarchive/001703.php – D.Tate Mar 08 '13 at 15:03
9

Actually I found the answer to be rather simple.

Situation: I had a div tag with a background image. Which would not printout when printing.

Solution:

  1. Create another style sheet called "print.css"

  2. Add the following line of code to your all your web pages right after your orginal css stylesheet link:

    <link rel="stylesheet" type="text/css" media="print" href="css/print_styles.css" />
    
  3. Immediately after your for the original non printing header, add the following:

    <div id="header"></div> <!-- YOUR NON PRINTING HEADER -->
    
    <div id="printheader"><img src="images/header_image.jpg" width="940" height="100" alt="header" /></div>
    
  4. In your style.css file, which is the main css style for you site, add the following line:

    #printheader {display: none; } /* Makes the print header not visible */
    
  5. In your print.css file, add the following code:

    #footer, #nav, #sidenav, .print, .search, .breadcrumb, .noprint {display: none;} /* Items from your page you DO NOT want to print */
    
    #container, #container2, #contentwide, #contentwide_tpsub, #contentwide_tp, #contentwide_open {width: 100%; margin: 0; float: none;} /* Clear widths to ensure all text is printed */
    
    #printheader {display: block; } /* Turns ON the div when printing */
    

What you are doing is essentially turning OFF the header on the normal "screen" page and turning the printheader ON when you make a print call.

** Please note: you will need to modify the print.css file to include other elements of your style.css file to format the fonts, colors, etc. Play around with "Print Preview" and add in the elements you need till you get the printout that you've been seeking.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
Chris Canada
  • 91
  • 1
  • 1
4

Don't use background-image to display printable images, use the normal <img> tag instead.

background-image is meant for unimportant images which most modern browsers tend to skip during printing (default setting in IE 11, Chrome 35, FF 30).

Why would you not want to use the img tag?

  • Alignment issues - Use absolute positioning to solve alignment issues.

  • Spriting - Spriting is possible using simple img and div tags.

  • Make it more difficult for users to save the image - That is also possible with simple img and div tags.

  • To "keep my HTML clean" - do any of the workaround solutions really make it cleaner for you? Give it up :)

Community
  • 1
  • 1
rustyx
  • 80,671
  • 25
  • 200
  • 267
  • 2
    This was the only option that worked for me. Not directly an answer to the question (which was about background images specifically) but a good alternative. As a bonus, it works on other browsers too, so you don't need multiple workarounds for multiple browsers. – RubberDuckRabbit Nov 14 '17 at 18:55
3

For IE http://support.microsoft.com/kb/980077

There must be something similar for FF.

p.s. you cannot set this for clients!

p.s.2. you can replace this stars with foreground pictures (absolute if needed) in css (media="print").

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
1

I had the same issue with IE not supporting the printing the background.

So I created 2 divs, one div had a higher Z and had the text content. The second div was immediately behind the front div but a lower Z index and had a image (img not background image) for width and height of 100%. So when I showed the 2 divs together it looked like one div because they perfectly overlapped. When I printed in IE Browser it shows with image because the image is not a background image but a normal img tag that fills a lower div.

some code.

<div id="survey" class="surveyResponseWindow" style="display:none;">Please logout and re-login, because your session has expired.</div>
<div id="surveyBackground" class="surveyBackgroundDiv" style="display:none;">
<!-- provides the background image for ie browser so that it does not show the lower level divs. -->
<img src="/rsm/jsp/public/images/contentGrad.gif" width="100%" height="100%" />
</div>

<script language="javascript" type="text/javascript">  
function showSurvey(surveyResponseId) {
    var e = document.getElementById("survey");
    var bkgd = document.getElementById("surveyBackground");
    var focusinput = document.getElementById('focusinput');
    var nh = 'data-nohide';

    if (e.style.display=='none') {
        e.style.display='block';//show div
        bkgd.style.display='block';//show div
    }
    focusinput.focus();//set focus so we know when they click outside

    e.onclick = function(e) {
        this.style.display='none';//hide div if they click on it
        bkgd.style.display='none';//show div
    };
    //if the user press ESC
    focusinput.onkeyup = function(e){
        if(e.keyCode === 27){
            var survey = document.getElementById("survey");
            var bkgd = document.getElementById("surveyBackground");
            //hide the div
            survey.style.display = 'none';
            bkgd.style.display = 'none';
            this.removeAttribute(nh);
        }else{
            //do something else with other keys(ie:down, up, enter)...
            focusinput.focus();
        }
    };
    //click somewhere else input onblur
    // was taken out because the browser print function would close the survey div page.
    //focusinput.onblur = function(){
    //    if(!e.getAttribute(nh)){
    //      //hide the div
    //        e.style.display = 'none';
    //    }
    //};     

    var params='<%=request.getContextPath()%>/request/dashboard/drilldown/callSurveyDetailAjax.html?surveyResponseId='+surveyResponseId;
    YAHOO.plugin.Dispatcher.fetch(e,params, {onLoad:showBackground});
}

var showBackground = function() {
    var e = document.getElementById("survey");
    var bkgd = document.getElementById("surveyBackground");
    bkgd.style.width = e.innerWidth();
    bkgd.style.height = e.innerHeight();
    bkgd.style.left = e.offsetWidth();
    bkgd.style.top = e.offsetHeight();
}

window.onload = function() {
    var focusinput = document.getElementById('focusinput');
    focusinput.focus();//set focus so we know when they click outside
}
</script>

in CSS put this

.surveyResponseWindow 
{
width:500px;
height:600px;
z-index: 2;
position: absolute;
top:100px;
left:150px;
border:1px solid #AAAAAA;   
border-bottom-left-radius:10px;
border-bottom-right-radius:10px;
border-top-left-radius:10px;
border-top-right-radius:10px;   
box-shadow: -1px 7px 15px -2px #000;
}

.surveyBackgroundDiv 
{
z-index: 1;
position: absolute;
top:100px;
left:150px;
width:500px;
height:600px;
border:1px solid #AAAAAA;   
border-bottom-left-radius:10px;
border-bottom-right-radius:10px;
border-top-left-radius:10px;
border-top-right-radius:10px;   
box-shadow: -1px 7px 15px -2px #000;
}
0

I believe this is a browser setting, not the backend of the web sites. I could be wrong however.

SD.
  • 3,089
  • 10
  • 26
  • 21