Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scale html table before printing using css

Tags:

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;} 
like image 919
techtheatre Avatar asked Feb 27 '15 04:02

techtheatre


People also ask

How do you scale in CSS?

scale() The scale() CSS function defines a transformation that resizes an element on the 2D plane. Because the amount of scaling is defined by a vector, it can resize the horizontal and vertical dimensions at different scales. Its result is a <transform-function> data type.

How do I use print media in CSS?

HTML, CSS and JavaScript You can use CSS to change the appearance of your web page when it's printed on a paper. You can specify one font for the screen version and another for the print version. You have seen @media rule in previous chapters. This rule allows you to specify different style for different media.


2 Answers

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

like image 50
Dmitry Avatar answered Sep 23 '22 06:09

Dmitry


I know I'm raising the dead, but for future search results reference:

I ran into a problem with super wide tables causing all the browsers to calculate the height incorrectly and repeat the thead multiple times on single pages - my solution was to apply zoom: 80% to the body (% varies based on your needs) which forced our page to effectively fit for print and the thead then was repeated properly at the top of every page. Perhaps trying zoom will work where transform did not.

Example CSS

@media print {   body {     zoom: 80%;   } } 
like image 26
TerraElise Avatar answered Sep 22 '22 06:09

TerraElise