Printing background colour in an HTML table

Translation of the original in Catalan. Read it in: CatalàCastellano

As many of you will know, especially those who have tried it. If we have a web page with a table that has some background colour or image, when we send it to the printer it is not printed. It is only possible to print the text and the outline of the table. Well then, sometimes there may be a need for that to happen. Here is where the problem starts and where I try to explain the solution that Oriol and Sara gave me.

The idea is, for example, to have a table as simple as this one:

screen.png

Thanks to the following HTML and CSS code. We could have the previous view on screen and the bluish background colour seen in the screenshot at the end of the article when printing:

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
	<head>
		<title>PROVA</title>
		<style media='screen'>
		
		</style>
		<style media='print'>
		
		</style>
	</head>
	<body>
		<table>
			<tr><td class='capçalera'>
				<div><span>TITULO</span></div>
			</td></tr>
			<tr><td >
				alsjfaljfaljflask
			</td></tr>
			<tr><td >
				Salñsjfalñjfdalñskf
			</td></tr>
		</table>
	</body>
</html>

Download the code.

The printed table would look like this:

print.png
Printing background color in a HTML table

As you can see, what the CSS code does is play with the corners of the table, which are indeed printed, then it gives them enough thickness to paint the whole header and finally places the text on top of the corners so that it gives the feeling that it is in its place. A great hack really, a shame there is nothing more elegant to do it.