printing not displaying colors

Is there a way to maintain the colors while printing my HTML page to PDF?

<table class="table table-responsive table-striped">
        <thead>
            <tr>
                <th>Elev</th>
                <th>Session navn</th>
                <th>Spørgsmål</th>
                <th>Svar</th>
                <th>Rigtigt svar</th>
                <th>Dato</th>
            </tr>
        </thead>
        <tbody>
                <tr class="success">
                        <td>test</td>
                        <td>testing</td>
                        <td>test</td>
                        <td>test</td>
                        <td>test</td>
                        <td>test</td>
                </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5">
                    <button class="btn btn-block btn-default" onclick="deactivate">Deaktiver</button>
                </td>
                <td style="text-align:right">#3</td>
            </tr>
        </tfoot>
    </table>

I fetch data from the database and display it in the table above. Rows are colored green if correct and red if wrong. This works well on the webpage but not when printed. How can I solve this issue?

Answer №1

Printing the table colors is not possible due to a CSS rule in Bootstrap that specifically targets printing using @media print. There was once a workaround I had come across online, but unfortunately, I accidentally removed it when updating my Bootstrap version. This led me to search for a solution here. To address this issue, you can locate the problematic rule within your bootstrap.css file (specifically between lines 196-269 in Bootstrap Version 3.3.4, excluding the minimized bootstrap.min.css).

196 @media print {
197 *,
198 *:before,
199 *:after {
200 background: transparent !important;
...
264 }
265 .table-bordered th,
266 .table-bordered td {
267 border: 1px solid #ddd !important;
268 }
269 }

One approach is to either completely remove the rule or selectively eliminate certain directives. However, the first option may inadvertently affect other styles such as navbar and text shadows. Hence, it is advisable to consider the second option. Below is a revised version of the rule which I have modified; additional adjustments may be necessary based on your Bootstrap version.

@media print {
*, *:before, *:after {
-webkit-box-shadow: none !important;
box-shadow: none !important;
text-shadow: none !important;
}
a, a:visited {
text-decoration: underline;
}
a[href]:after {
content: " (" attr(href) ")";
}
abbr[title]:after {
content: " (" attr(title) ")";
}
a[href^="#"]:after, a[href^="javascript:"]:after {
content: "";
}
pre, blockquote {
border: 1px solid #999;
page-break-inside: avoid;
}
thead {
display: table-header-group;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
p, h2, h3 {
orphans: 3;
widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
select {
background: #fff !important;
}
.navbar {
display: none;
}
.btn> .caret, .dropup> .btn> .caret {
border-top-color: #000 !important;
}
}

I excluded lines 200, 201, and 255-268 from the original rule. In case you are utilizing the minified Bootstrap version, you would need to identify and modify the corresponding lines or minimize the uncompressed version accordingly. Several online tools are available for this purpose, or most IDEs include a built-in minimizer feature.

Answer №2

The ability to print background colors is determined by the browser settings, not by code manipulation.

Instructions on how to enable this feature can be found in the knowledge base articles for each respective browser:

Internet Explorer > https://support.microsoft.com/en-us/kb/296326
Google Chrome >
Mozilla Firefox >

Answer №3

Implemented the below CSS code, and saw successful results

tr.success td {
    background-color: #dff0d8 !important;
    -webkit-print-color-adjust: exact;
}

tr.danger td {
    background-color: #f2dede !important;
    -webkit-print-color-adjust: exact;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An error in PHP has been detected within the CodeIgniter framework

I have encountered an error that has been frustrating me lately: An unexpected PHP Error occurred with the following details: Severity: Warning Message: strip_tags() expects parameter 1 to be string, array given Filename: inscription/loginform3.php Line ...

Positives and negatives images for accordion menu

I have successfully created an accordion list using HTML, CSS, and JavaScript. However, I would like to enhance it by adding a plus and minus picture in the left corner of the heading. Is there a way to achieve this functionality? I have two images that I ...

Is there a way to convert inline styling to CSS using Material-ui within a React component?

In my development work with React, I initially utilized inline styling to customize the appearance of material-UI components. However, I have come to realize that utilizing CSS provides a more structured and efficient approach to styling components. Despit ...

Occasionally, the letters in Google Fonts get jumbled up when refreshing the page, but

During our development process on localhost, we've come across a peculiar issue with Google Fonts. Every now and then, when refreshing the page, the letters in the text become jumbled up. This seems to happen randomly, about once every 5 refreshes. Th ...

Dynamic value updates using jQuery input type formulas

I need help with a form that has two inputs: The first input allows the user to enter an amount, such as 1000. The second input is read-only and should display the value of the first input plus 1%. For example, if the user types in 1000 in the first fie ...

Retrieve JSON data by making a POST request to a website's API

Can you help me retrieve Json data from a website API using JavaScript? I'm trying to fetch quotes from the following website: for my quotes generator page. While I have some understanding of making GET requests, it seems that this API requires POST ...

Transform the appearance of a complex image by altering its color using CSS

In my quest to bring a unique functionality to life, I am looking to achieve the following: I want to layer two transparent images within a <div>: one representing an object and the other depicting a glowing effect around the object. When hovering ...

Calculating the depth of a webpage with Python

I need help determining the depth of a website through Python. The depth of a subwebsite is defined as the number of clicks needed to reach it from the main website (e.g. www.ualberta.ca). For example, if a subwebsite like www.ualberta.ca/beartracks requir ...

Tips for creating a clickable phone number on a WordPress classified website

Despite trying numerous solutions to make the phone number clickable, it still didn't work as expected <?php global $redux_demo;?> <?php $phoneon= $redux_demo['phoneon']; ?> <?php if($phoneon == 1){?> <?php $po ...

What is the best way to hide the background of an extension's HTML?

Currently, I am working on developing a Chrome extension for a university project. However, I am facing challenges in making the background or body of the extension's HTML completely transparent to achieve a cleaner interface. The issue specifically l ...

Is it possible to override CSS classes in Ionic without using app.scss?

I'm currently working on designing a dark theme for my ionic application by watching this tutorial. It's been effective for most of the classes, however, I've encountered some CSS classes that only respond to overrides in app.scss. If I try ...

Is it possible to achieve the lower link set in a pure CSS horizontal menu where the horizontal link set is positioned below horizontal text tabs?

I am attempting to design a pure CSS on-hover horizontal menu. I have made some progress on my design, as shown in http://jsfiddle.net/dq8z192L/11/ The goal is to have a menu that expands horizontally when hovered over. Each tab reveals a set of links be ...

A two-column bootstrap design with zero padding or gaps

This is the layout I am aiming for: Below is the code I currently have: <div class="row "> <div class="col-lg-6 row-eq-height push-right"> <img src="1.jpg" class="img-responsive"> </div> <d ...

What is the best method to make an email address distinct?

<body> <p>Please input your email address:</p> <input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email" placeholder="Email Address"> <input onclick= "validateEmail(email)" type="su ...

Updating Content Dynamically with AJAX, JavaScript, and PHP without requiring a page reload or user action

Currently, I'm delving into the world of JS/AJAX functions that operate without the need for a page refresh or button click. However, I've encountered an issue when trying to echo the value. Oddly enough, when I change this line $email = $_POST ...

Creating a child element to fully span the width of its parent using floating techniques

My dilemma involves a child element nested under a floated parent. Is there a way to make the child element full-width without affecting the width of the parent? The current situation The desired outcome, but without using float The workaround suggested ...

Customizing the appearance of an HTML element using user-input text styling

On my webpage, there are two text areas and a division. One of the text areas is meant for users to input HTML code, while the other is for CSS code. Whenever a button is clicked, a JavaScript function will be triggered to display the combined HTML and CSS ...

Using XSLT to convert HTML into the desired text format

It has been almost two decades since my last encounter with XSLT. I am attempting to transform documents, like the HTML snippet below, into the desired output. <p> おばあさんは、とても <ruby><rb>喜</rb><rp>(</rp ...

Prevent automatic submission of forms when selecting attributes in HTML forms using AngularJS

I need to create a form where users can select their branch of study. Here is the form I have designed- <form method="post" [formGroup]="formData" (click)="dataSubmit()" > <div class="form-group"> <label for="branch">Selec ...

Retrieving text that has been HTML-escaped from a textarea using jQuery

Within my HTML file, I have a <textarea id="mytextarea"></textarea>. Suppose a user inputs the following text: <hello>, world! How can I retrieve res = "&lt;hello&gt;, world!"; based on the user's input? The current code s ...