Is there a way to print only the nested "id = printarea" div (with style) 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 tried using css but it doesn't display anything (obviously) due to inheritance. The following example shows my intention.
@media print {
* { display:none; }
#printarea { display:block; }
}
I have successfully used javascript (which works), but I do not think this is an elegant solution, since I would have to use all the importers and style blocks in the document.write document.
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/
Related:
Overriding the parent CSS display property
source
share