How do I Make Firefox Print a Background-Color Style?

42,916

Solution 1

Its a browser setting. There is nothing you can do in your CSS. In Windows - File > Page Setup... > Print Background.

Solution 2

I found a solution, it's a bit hacky, but with CSS pseudo elements you can create backgrounds using fat borders. Borders are printed even when "printing backgrounds" is off, just make them really thick! One note, Firefox sets all white font colors to black, so when you create a fake black background, Firefox still makes the text black, rendering the text invisible. Anyway here it is:

The HTML:

<div class="redBox">
  <div class="content">Black on red</div>
</div>

The css:

.redBox {
  /* o no, does not work with print */
  background-color: red;
}

@media print {
  .redBox {
     position: relative;
     overflow: hidden; /* this might not work well in all situations */
  }
  .redBox:before {
     content: '';
     position: absolute;
     top: 0;
     right: 0;
     left: 0;
     bottom: 0;
     /* and here it is, the background color */
     border: 99999px red solid;
     z-index: 0; /* was required in my situation */
  }
  .redBox * {
    /* was required in my situation */
    position: relative;
    z-index: 1;
  }
}

Solution 3

There is a simple solution for this.

For background-color, instead of using:

background-color: red

Use:

background-color: unset;
box-shadow: inset 0 0 0 1000px red /* 1000px is a random high 
                                     number that is definitely 
                                     larger than the box dimension*/

Also for color, instead of:

color: grey;

Use:

color: unset;
text-shadow: 0 0 grey;

You may restrict them to print only by using @media print, but you do not have to!


Note: Sometimes you should use background-color: transparent; or color: transparent; if the color or background-color are inherited from parent elements.

Solution 4

For show background colors in firefox & IE

@media print {
  body{
    -webkit-print-color-adjust: exact; /*chrome & webkit browsers*/
    color-adjust: exact; /*firefox & IE */
  } 
}

Solution 5

This is how I made it to work in my case by adding the below two lines to the particular div. "@@supports (-moz-appearance:meterbar)" is helpful to add styles specific to Firefox.

-webkit-print-color-adjust: exact; color-adjust: exact;

@@supports (-moz-appearance:meterbar) {
    .container {
        margin: 0;
        font-size: 0.85em;
        width: 100%;
        -webkit-print-color-adjust: exact;
        color-adjust: exact;
    }
}
Share:
42,916
machineghost
Author by

machineghost

Living proof that (some) humanities majors can program. SOreadytohelp Note to Recruiters: While I live in Mountain View I am only looking for remote positions at this time (not positions in the Bay Area).

Updated on June 14, 2020

Comments

  • machineghost
    machineghost about 4 years

    I have some simple CSS:

    #someElement {
        background-color:black;
        color:white;
    }
    

    It looks ok in the browser, but when I go to print it in Firefox it comes out as black text on a white background. I imagine this is some sort of ink-saving feature, but is there any way around it?

  • machineghost
    machineghost about 15 years
    Heh, a whole page would be excessive; I only wanted a small area.
  • Arve Systad
    Arve Systad about 15 years
    That might just be, but keep in mind it's the exact same mechanism that controls it. Should the browser detect the surface area in the website, check it against desired DPI settings somewhere for print and THEN decide wheather or not to apply the BG-color? ;-)
  • jdavis
    jdavis almost 10 years
    Is there a way with javascript to detect if this setting is on or off?
  • Daniel A. White
    Daniel A. White almost 10 years
    @jdavis no i do not think so.
  • kernel
    kernel over 9 years
    If you set z-index: -1 instead of 0 you could save up the .redBox * part.
  • Blazemonger
    Blazemonger about 9 years
    @kernel That worked for me, but I think you need to set the background of .redbox transparent for print as well.
  • Ismael Miguel
    Ismael Miguel about 9 years
    I know that this is an old answer, but try using 9999cm instead of px. (I experimented with this and that's the only unit that worked. 10000cm won't work.)
  • Ismael Miguel
    Ismael Miguel about 9 years
    After some testing, I've found that the absolute maximum (I spent my time to find) was 5585.70776367in (~14299.4118749952cm). You can try on jsfiddle.net/67k4Lvn9/2. At least worked on Google Chrome 42.0.2311.135 and Firefox 37.0.2.
  • John Demetriou
    John Demetriou about 9 years
    @timing although your method works and the "background" is printed, the content of the div is not shown, I tried using z-index but still not visible, It is shown on screen though
  • Ben Sewards
    Ben Sewards over 8 years
    how does this solve the generic background color issue?
  • Poelinca Dorin
    Poelinca Dorin almost 8 years
    May not work on all browsers, but fixes FF text colors.
  • userlond
    userlond almost 8 years
    Works for background, don't forget to add : after box-shadow in style definition
  • Musa Haidari
    Musa Haidari almost 8 years
    @userlond Thank you for the fix.
  • Musa Haidari
    Musa Haidari almost 8 years
    As I have mentioned in my answer, there is a better solution to do it
  • leemes
    leemes almost 8 years
    I like this CSS-only solution since it doesn't need me to alter the DOM.
  • Michal Skop
    Michal Skop over 7 years
    It is now directly in the Print... -> Options (tab) -> Print Background Colors. It seems to me more universal than what Musa or timing suggest, because it can be applied also for webpages you cannot edit (you are not their owner).
  • roflmyeggo
    roflmyeggo about 7 years
    Note this does not currently work in Chrome (the box shadow is printed as solid black).
  • roflmyeggo
    roflmyeggo about 7 years
    For now you can work around this issue in Chrome by using -web-filter: opacity(1) but this will result in some pixelation.
  • RustyToms
    RustyToms about 7 years
    This is a new feature in Firefox, and this is now the correct answer. Using the color-adjust property
  • Naxos84
    Naxos84 about 7 years
    Is there a way to set this up when I use onload="window.print();? cause the browser settings only work when I use the browser "print" button
  • Daniel A. White
    Daniel A. White about 7 years
    @Naxos84 i dont think so. thats all controlled by the browser
  • Euri Pinhollow
    Euri Pinhollow almost 6 years
    This changed nothing for me unfortunately.