36

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
  • 33,529
  • 30
  • 159
  • 234
  • I suggest @musa answer should be accepted instead of the current one, as it really solves the problem. – Namatullah Shahbazi Aug 24 '16 at 10:45
  • 1
    Hmmm ... timing's answer came almost two years before mursa's, and both answers use the same basic principle (ie. use something besides `background-color` to fake a background color). Timing's answer has more explanation, but mursa's is simpler ... I wish one was clearly better, but given all that I'm honestly not sure which one I should accept. For now I have just removed the checkmark from Daniel A. White's answer (while it's technically accurate, people on SO are looking for solutions). – machineghost Aug 24 '16 at 22:42
  • 3
    Firefox will [now recognize the `color-adjust` property](https://stackoverflow.com/questions/35004690/force-background-color-printing-on-firefox/39710495#39710495). 'color-adjust: exact'; There is an answer below that mentions that which should now be selected as the correct answer. – RustyToms May 08 '17 at 01:22

8 Answers8

45

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
31

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;
  }
}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
timing
  • 6,340
  • 1
  • 17
  • 16
  • 1
    If you set `z-index: -1` instead of `0` you could save up the `.redBox *` part. – kernel Jan 07 '15 at 15:07
  • @kernel That worked for me, but I think you need to set the background of `.redbox` transparent for print as well. – Blazemonger Apr 17 '15 at 16:24
  • 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 May 12 '15 at 11:15
  • 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 http://jsfiddle.net/67k4Lvn9/2/. At least worked on Google Chrome 42.0.2311.135 and Firefox 37.0.2. – Ismael Miguel May 12 '15 at 11:31
  • @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 – John Demetriou May 25 '15 at 08:04
25

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.
Musa Haidari
  • 2,109
  • 5
  • 30
  • 53
23

For show background colors in firefox & IE

@media print {
  body{
    -webkit-print-color-adjust: exact; /*chrome & webkit browsers*/
    color-adjust: exact; /*firefox & IE */
  } 
}
DhineshYes
  • 1,008
  • 11
  • 12
20

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;
    }
}
Ricky Boy
  • 723
  • 7
  • 7
  • 3
    This is a new feature in Firefox, and this is now the correct answer. [Using the color-adjust property](https://stackoverflow.com/questions/35004690/force-background-color-printing-on-firefox/39710495#39710495) – RustyToms May 08 '17 at 01:20
2

For chrome you can use this:

-webkit-print-color-adjust:exact;

Or in print preview, tick the More settings->Background Graphics

For Firefox, you can't enable it using any CSS. You can enable it as follow (If you don't see the File, press Alt key once).

File->Page setup and tick the Print Background(color & images)

Saadat
  • 461
  • 6
  • 9
1

I've hacked this by using SVG element

.legendItem {
  position: relative;
}

.legentItemText {
  position: relative;
  z-index: 1;
}

.printBackground {
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  position: absolute;
  z-index: 0;
}
  <div class="legendItem">
    <div class="legentItemText">Some text.....<div>
  <svg class="printBackground">
    <rect width="100%" height="100%" fill="#000" />
  </svg>
</div>
Kirill
  • 180
  • 4
-1

Maybe not the answer you're looking for, but here goes:

I'd rather add a separate stylesheet for printing the page. Typically, you would want to remove things like navigation menus, breadcrumbs, ads, and maybe do some small changes in margins, paddings, borders and fonts compared to the on-screen stylesheet.

Even thinking about forcing the user to fill a whole page with black ink with white text seems silly to me.

To add a separate print stylesheet, add another stylesheet to the head of your page.

<link rel="stylesheet" href="print.css" type="text/css" media="print">
Arve Systad
  • 5,471
  • 1
  • 32
  • 58
  • Heh, a whole page would be excessive; I only wanted a small area. – machineghost Apr 19 '09 at 06:19
  • 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? ;-) – Arve Systad Apr 19 '09 at 12:31
  • 7
    how does this solve the generic background color issue? – Ben Sewards Nov 17 '15 at 14:23