Background color not showing in print preview

380,459

Solution 1

The Chrome CSS property -webkit-print-color-adjust: exact; works appropriately.

However, making sure you have the correct CSS for printing can often be tricky. Several things can be done to avoid the difficulties you are having. First, separate all your print CSS from your screen CSS. This is done via the @media print and @media screen.

Often times just setting up some extra @media print CSS is not enough because you still have all your other CSS included when printing as well. In these cases you just need to be aware of CSS specificity as the print rules don't automatically win against non-print CSS rules.

In your case, the -webkit-print-color-adjust: exact is working. However, your background-color and color definitions are being beaten out by other CSS with higher specificity.

While I do not endorse using !important in nearly any circumstance, the following definitions work properly and expose the problem:

@media print {
    tr.vendorListHeading {
        background-color: #1a4567 !important;
        -webkit-print-color-adjust: exact; 
    }
}

@media print {
    .vendorListHeading th {
        color: white !important;
    }
}

Here is the fiddle (and embedded for ease of print previewing).

Solution 2

That CSS property is all you need it works for me...When previewing in Chrome you have the option to see it BW and Color(Color: Options- Color or Black and white) so if you don't have that option, then I suggest to grab this Chrome extension and make your life easier:

https://chrome.google.com/webstore/detail/print-background-colors/gjecpgdgnlanljjdacjdeadjkocnnamk?hl=en

The site you added on fiddle needs this in your media print css (you have it just need to add it...

media print CSS in the body:

@media print {
body {-webkit-print-color-adjust: exact;}
}

UPDATE OK so your issue is bootstrap.css...it has a media print css as well as you do....you remove that and that should give you color....you need to either do your own or stick with bootstraps print css.

When I click print on this I see color.... http://jsfiddle.net/rajkumart08/TbrtD/1/embedded/result/

Solution 3

Chrome will not render background-color, or several other styles, when printing if the background graphics setting is turned off.

This has nothing to do with css, @media, or specificity. You can probably hack your way around it, but the easiest way to get chrome to show the background-color and other graphics is to properly check this checkbox under More Settings.

enter image description here

Solution 4

I just needed to add the !important attribute onto the the background-color tag in order for it to show up, did not need the webkit part:

background-color: #f5f5f5 !important;

Solution 5

Your CSS must be like this:

@media print {
   body {
      -webkit-print-color-adjust: exact;
   }
}

.vendorListHeading th {
   background-color: #1a4567 !important;
   color: white !important;   
}
Share:
380,459

Related videos on Youtube

ghost_dad
Author by

ghost_dad

I am a self-taught, self-propelled front end developer always looking to expand my knowledge base and mostly, build cool things. I'm lucky enough to do what I love for a living, and share that passion with other people!

Updated on July 08, 2022

Comments

  • ghost_dad
    ghost_dad almost 2 years

    I am trying to print a page. In that page I have given a table a background color. When I view the print preview in chrome its not taking on the background color property...

    So I tried this property:

    -webkit-print-color-adjust: exact; 
    

    but still its not showing the color.

    http://jsfiddle.net/TbrtD/

    .vendorListHeading {
      background-color: #1a4567;
      color: white;
      -webkit-print-color-adjust: exact; 
    }
    
    
    <div class="bs-docs-example" id="soTable" style="padding-top: 10px;">
      <table class="table" style="margin-bottom: 0px;">
        <thead>
          <tr class="vendorListHeading" style="">
            <th>Date</th>
            <th>PO Number</th>
            <th>Term</th>
            <th>Tax</th>
            <th>Quote Number</th>
            <th>Status</th>
            <th>Account Mgr</th>
            <th>Shipping Method</th>
            <th>Shipping Account</th>
            <th style="width: 184px;">QA</th>
            <th id="referenceSO">Reference</th>
            <th id="referenceSO" style="width: 146px;">End-User Name</th>
            <th id="referenceSO" style="width: 118px;">End-User's PO</th>
            <th id="referenceSO" style="width: 148px;">Tracking Number</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td>22</td>
            <td>20130000</td>
            <td>Jim B.</td>
            <td>22</td>
            <td>510 xxx yyyy</td>
            <td>[email protected]</td>
            <td>PDF</td>
            <td>12/23/2012</td>
            <td>Approved</td>
            <td>PDF</td>
            <td id="referenceSO">12/23/2012</td>
            <td id="referenceSO">Approved</td>
          </tr>
        </tbody>
      </table>
    </div>
    
  • Admin
    Admin over 11 years
    extension works for all the pages but it does not work for my code why jsfiddle.net/rajkumart08/TbrtD/1/embedded/result defie.co/testing/twitter-bootstrap-558bc52/docs/examples/…
  • Riskbreaker
    Riskbreaker over 11 years
    if thats the case make sure your bootstrap has webkit-print-color-adjust: exact; on your body is another way to check...in print media css of course
  • Admin
    Admin over 11 years
    its not working defie.co/testing/twitter-bootstrap-558bc52/docs/examples/… i gave it in this code
  • Riskbreaker
    Riskbreaker over 11 years
    You did not add it in the print media as I stated in your html inner css: yout html line 1154: @media print { body {margin:0; padding:0; line-height: 1.4em; word-spacing:1px; letter-spacing:0.2px; font: 12px Arial, Helvetica,"Lucida Grande", serif; color: #000;}.... you need to add it in there...
  • Admin
    Admin over 11 years
    I have edited it again but still not working defie.co/testing/twitter-bootstrap-558bc52/docs/examples/…
  • Admin
    Admin over 11 years
    jsfiddle.net/rajkumart08/TbrtD/1/embedded/result but i dont see the table heading colors in chrome
  • Riskbreaker
    Riskbreaker over 11 years
    you have the color set my friend. The answer is done.... for the other stuff...it requires more print css styling, that is a NEW TOPIC, but you got color and your answer :)
  • Admin
    Admin over 11 years
    in this fiddle i ipdate but i am not getting my table color jsfiddle.net/rajkumart08/TbrtD/3/embedded/result .vendorListHeading { background-color: #1a4567; color: white; -webkit-print-color-adjust: exact; }
  • piotr_cz
    piotr_cz over 8 years
    When opening the fiddle, I'm getting the Print preview failed. message in Chrome v47.0.2526.106
  • Mike Young
    Mike Young over 8 years
    Mozilla has marked this as non-standard, so the results are inconsistent and shouldn't be used in production sites developer.mozilla.org/en-US/docs/Web/CSS/…
  • big_water
    big_water over 7 years
    this could be done with media="all" instead of two link tags
  • Corgalore
    Corgalore about 7 years
    Yes, this works in Chrome. Bootstrap css was overriding it for me using print media. You can also preview and inspect print mode using the Rendering tab at bottom of Chrome Elements inspector. There's an option for Emulate CSS Media in case you want to see the override stack.
  • adelriosantiago
    adelriosantiago over 6 years
    It works, thanks (note that you will need to remove var from window on your last code snippet)
  • vimal kumar
    vimal kumar about 6 years
    That's fine..May i know the reason why the color is not shown without giving important @Flea
  • Munim Munna
    Munim Munna over 5 years
    Why remove all print CSS while printing?
  • Mohd. Shaizad
    Mohd. Shaizad over 5 years
    This is to make color visible for print
  • codemirror
    codemirror over 5 years
    Works for me with !important in background
  • Eric
    Eric over 5 years
    Hey, I've finally got around to making a fiddle jsfiddle.net/qm7shrvf for some reason, I cannot get chrome to render the green or red backgrounds in the print preview (I haven't tried to actually print it though) Thanks! (I did observe that the black background of jsfiddle.net does get rendered based on that chrome setting though)
  • Shrikant
    Shrikant about 4 years
    Well don't understand why this is happening but it solved my problem :-)
  • yougotiger
    yougotiger almost 4 years
    Adding the important description after my settings helped. I had to have the -webkit-print-color-adjust set at body and then in each of my elements that needed color, I had to declare them important for this to work.
  • jazeb007
    jazeb007 over 3 years
    I have used css in this way but does not work for me
  • Miguel Angel Mendoza
    Miguel Angel Mendoza over 3 years
    This is a non-standard CSS extension that can be used to force printing of background colors and images in browsers based on the WebKit engine, there is an alternative for Firefox, please try using also color-adjust: exact; for more info visit stackoverflow.com/questions/35625178/…
  • Dexter
    Dexter over 3 years
    Remember, if you add media="screen" to bootstrap css, then every style from bootstrap will be striped out.
  • Parmjeet Singh
    Parmjeet Singh about 3 years
    The reason is bootstrap.css, It has a media print css that is taking importance over your css. Following are solutions in this case: - A. override bootstrap media print styles by writing your styles in media print in your css file. - B. give !important to your original styles so that bootstrap media print css cannot override it. In Your case !important have more priority than bootstrap's media print css, That's the reason why !important worked in your case.
  • Ousama
    Ousama about 3 years
    It works only when we use style attr example: <td width="35%" style="background-color: #DAEEF3 !important;"> and the webkit is mandatory
  • Ousama
    Ousama about 3 years
    It works only when we use style attr example: <td width="35%" style="background-color: #DAEEF3 !important;">
  • 10101010
    10101010 over 2 years
    saved my day!!!!
  • evnp
    evnp about 2 years
    Miguel you rock!! only person in this thread to mention Firefox, saving my day