jootl
jootl

Reputation: 719

Print HTML table background color with Bootstrap

If I use Bootstrap table class on a table, print preview doesn't show background color for tr.

My code

@media print {
  .bg-danger {
    background-color: #f2dede !important;
  }
}
<body>
  <div class="bg-danger">
    <td>DIV</td>
  </div>

  <table class="table">
    <tr class="bg-danger">
      <td>TR 1</td>
    </tr>
    <tr class="danger">
      <td>TR 2</td>
    </tr>
    <tr style="background-color: #f2dede !important;">
      <td>TR 3</td>
    </tr>
  </table>
</body>

On screen, all is red but on print preview only the div element is red.

If I remove the table class, everything works (except I need table style).

My configuration : IE11 and Windows 7.

Is there a trick to print the background color ?

Note: The indicated duplicate (CSS @media print issues with background-color;) is not the issue here, my settings are checked to print background colors. Also, I can print color for several other elements.

Answer :

Thanks to @Ted comments, I overrided td style for table class :

<style type="text/css">
  @media print {
    .bg-danger {
      background-color: #f2dede !important;
    }
    
    .table td {
      background-color: transparent !important;
    }
  }
</style>

Upvotes: 26

Views: 31332

Answers (7)

haptn
haptn

Reputation: 987

I tried all suggested solutions here (and in many other questions), such as applied background-color: #000 !important; both in stylesheet and inline, or set

@media print {
  * {
    color-adjust: exact !important;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
}

, even combined them together, but nothing worked.

As you said above, everything works except <table>, so the easiest trick is to wrap-up anything inside <th> or <td> with a <div> (you can adjust padding to make it display same as prior).

e.g.

...
<th><div>Col title here...</div></th>
...
<td><div>Content here...</div></td>
...

For me, problem was solved by doing this "hack".

Hope this help!

Upvotes: 0

Zanyar Jalal
Zanyar Jalal

Reputation: 1874

Replace table class to table-print

By using this CSS code

.table-print {
    width: 100%;
    margin-bottom: 1rem;
    color: #212529;
}

    .table-print thead th {
        vertical-align: bottom;
        border-bottom: 2px solid #dee2e6;
    }

    .table-print th, .table-print td {
        padding: 0.75rem;
        vertical-align: top;
        border-top: 1px solid #dee2e6;
    }
 .table-print .thead-dark th {
    color: #fff;
    background-color: #343a40;
    border-color: #454d55;
}

Upvotes: 1

The Aelfinn
The Aelfinn

Reputation: 16928

Adding onto @Ted's answer, the following will override the default bootstrap background-color !important rule:

@media print {
    table tr.highlighted > td {
        background-color: yellow !important;
    }
}

Upvotes: 2

pszafer
pszafer

Reputation: 388

I think better way is to use more specific css selector

<style>
@media print {
   .table td.cell {
     background-color: blue !important;
   }
}
</style>

<table class="table">
  <tr>
    <td class="cell">TR 1</td>
  </tr>
</table>

Upvotes: 0

tfa
tfa

Reputation: 1699

I used a table th header row and styled it like this

.table th {
        background-color: #2D3347 !important;
        color: #fff !important
    }

Upvotes: 0

Ted
Ted

Reputation: 14927

Bootstrap explicitly sets the background to white for printing--this is in their CSS:

@media print { 
    .table td, .table th { 
        background-color: #fff !important; 
    } 
}

Write your override like theirs and you should be good to go.

Upvotes: 38

Joe Swindell
Joe Swindell

Reputation: 671

There is not. By default they don't print. It's a browser option that can't be overcome by CSS/JS etc.

There IS this, which force colors...allegedly in Chrome

-webkit-print-color-adjust

Which is listed as BUGGY support ONLY for chrome, and not supported in other browsers.

Upvotes: 2

Related Questions