简体   繁体   中英

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>

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.

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;
    }
}

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.

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;
}

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

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

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>

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).

eg

...
<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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM