Is there a way to print only the nested "id=printarea" div (with styling) using only css (not javascript), in IE8 on Windows?
<div id="main">
This should NOT be shown in Print Preview
<div id="printarea">ONLY this should be shown in Print Preview
<table><tr><th>one</th><th>one</th></tr><tr><td>one</td><td>one</td></tr></table></div>
</div>
I've tried using css, but it displays nothing (obviously) due to inheritance. The following example shows my intention.
@media print {
* { display:none; }
#printarea { display:block; }
}
I've successfully used javascript (which works), but I don't consider it an elegant solution, as I'd have to pull in all css imports and style blocks in the document.write.
function printDiv(divId){
var divToPrint = document.getElementById(divId);
newWin= window.open();
newWin.document.write('<style>table,tr,td,th{border-collapse:collapse;border:1px solid black;}</style>');
newWin.document.write(divToPrint.innerHTML);
newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();
}
Example: http://jsfiddle.net/D7ZWh/2/