scale html table before printing using css

techtheatre picture techtheatre · Feb 27, 2015 · Viewed 68k times · Source

I have a table as the entire content of an HTML document (for legitimate table purposes...it is table data, not for layout). Some cells have widths and heights specified (not through css but using the old sizing inline in a table structure), but the overall table does not have a width or height specified. This is a fairly large table, but with proper scaling (about 70%) it can be printed to fit nicely on a single sheet of paper. This can be accomplished by using the scale page function within the printer settings of IE, Firefox, and Chrome. Is there a way to scale the whole page (which in my case is just this table) as part of a print stylesheet (I know about @media print {} but the statements there are being ignored...the issue is with proper commands for scaling, not with setting up the print css)? I can scale the on-screen view with this:

body {
   transform: scale(.7);
}

however when printing, Chrome (and others) disregard my scale and automatically scale the table to fit the width of the paper and that results in my table being split onto a second page. I have tried applying this as well with no success:

table {page-break-inside: avoid;}

Answer

Dmitry picture Dmitry · Feb 27, 2015

You should use the media types

@media print {
    body {transform: scale(.7);}
    table {page-break-inside: avoid;}
}

So, this styles will by applying only in print preview mode. http://www.w3.org/TR/CSS21/media.html