javascript - @media print css not formatting table on printing -
i have simple table button below it. in body section of jsp below:
<div id="mydivforprint"> <table class="t1"> <tbody> <tr> <th>one</th> <th>two</th> <th>three</th> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table> </div> <button onclick="printdiv()">print</button>
below css @media rule supposed format table. css in head section of jsp:
@media print,screen{ table.t1 { margin: 1em auto; border-collapse: collapse; font-family: arial, helvetica, sans-serif; } .t1 th, .t1 td { padding: 4px 8px; } .t1 thead th { background: #4f81bd; text-transform: lowercase; text-align: left; font-size: 15px; color: #fff; } .t1 tr { border-right: 1px solid #95b3d7; } .t1 tbody tr { border-bottom: 1px solid #95b3d7; } .t1 tbody tr:nth-child(odd) { background: #dbe5f0; } .t1 tbody th, .t1 tbody tr:nth-child(even) td { border-right: 1px solid #95b3d7; } .t1 tfoot th { background: #4f81bd; text-align: left; font-weight: normal; font-size: 10px; color: #fff; } .t1 tr *:nth-child(3), .t1 tr *:nth-child(4) { text-align: right; } } </style>
below javascript function print div containing table. in body section of jsp:
<script type="text/javascript"> function printdiv() { var divtoprint=document.getelementbyid("mydivforprint"); newwin= window.open(""); newwin.document.write(divtoprint.outerhtml); newwin.document.close(); newwin.document.focus(); newwin.print(); newwin.close(); } </script>
the page comes nicely formatted on screen. however, when click on print button , print page xps document, css formatting lost , content of table gets printed looks bad.
what doing wrong ? using ie8 , can not move other browser or newer version of ie.
you can use different styles screen , print media in style sheet, i.e.
@media screen { table.test { font-family:"times new roman",georgia; font-size:10px; // more } } @media print { table.test { font-family:verdana, arial; font-size:10px; // more } }
when you'll print table styles defined in print media applied.
Comments
Post a Comment