简体   繁体   English

如何应用css打印div

[英]How to apply css to print div

I am currently working on a project which was already done now it requires some enhancement.I will give you a slight jist of it.It has a search functionality and after clicking that name displayed in the result it displays additional details.Now the client has asked to add the print option also,which will print the displayed .I have also implement this functionality.Now the main problem is that the print preview page does not take any css of the last page.I have used css int the HTML page only.Please see the code.I have referred Print the contents of a DIV question .But it uses css externally.How could I use it when I have defined it internally. 我目前正在开发一个已经完成的项目,现在它需要一些增强。我会给你一个轻微的jist。它有一个搜索功能,点击结果中显示的名称后显示其他详细信息。现在客户端有要求添加打印选项,它将打印显示的。我也实现了这个功能。现在的主要问题是打印预览页面没有采取最后一页的任何CSS。我只在HTML页面使用了css请看代码。我已经参考了打印DIV问题的内容 。但是它在外部使用了css。当我在内部定义它时,我怎么能用它。 I know this might be a very basic question But I am very new to the CSS world. 我知道这可能是一个非常基本的问题但我对CSS世界很新。 Thanks in advance...` 提前谢谢......`

Edit: I have applied the @print tag.But it was not taking the css even after that.Thats why I have voted to remain this question as unique 编辑:我已经应用了@print标签。但是,即使在那之后它也没有采用css。这就是为什么我投票保持这个问题的独特性

PS I didnt code this piece of shit, someone else did PS我没有编码这件狗屎,其他人做了 1图像显示单击打印按钮后css.2nd图像显示页面的结果。并且没有css .

  <script type="text/javascript"> function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; '.td{'+ ' width:205px;'+ 'margin-top:10px;'+ 'margin-bottom:10px;'+ 'margin-left:10px;'+ '}'+ 'table{'+ 'border-collapse:collapse;'+ 'border:1px solid #FF0000;'+ '}'+ 'table td{'+ 'border:1px solid #000;'+ '}' '</style>'; w=window.open(); w.document.write(printContent); w.print(); w.close(); } </script> 
  <style type="text/css"> body { margin: 0; padding: 0; background: #FFFFFF; color: #000000; font: 85% "Open Sans", Tahoma, sans-serif; } .dataGrid { border: 1px solid #48627A; padding: 0; width: 99%; height: 100%; background: #FFFFFF; text-align: left; } .title { color: #004e90; font-weight: bold; font-family: "Open Sans", Tahoma, sans-serif; font-size: 12px; } .res td{ font-size: 12px; font-family: open sans, arial; border-top: 1px solid #ddd; width: auto; padding: 4px; } .dataGridTitle{ border: 1px solid #0D1115; padding-top: 1px; padding-right: 3px; padding-bottom: 1px; padding-left: 3px; background: #48627A; color: #FFFFFF; text-align: center; font: bold 0.8em verdana, arial, helvetica, sans-serif; } .dataGridElement{ padding-top: 1px; padding-right: 3px; padding-bottom:1px; padding-left: 3px; background: #E8EAEA; color: #000000; font: 0.8em verdana, arial, helvetica, sans-serif; } .td{ width:205px; font: verdana, arial, helvetica, sans-serif; } <form> <div id="print"> <div class="dataGrid" style="margin-top:10px;margin-bottom:10px;margin-left:10px;" > <h1 style="margin-top:10px;margin-bottom:10px;margin-left:10px;" >Partner Details</h1> <table class="res" style="table-layout:fixed; margin-top:10px;margin-bottom:10px;margin-left:10px;" > <tr> <td class = "td title" ><label for="username">Name</label> </td> <td ><label for="username">{$item.partnername}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Address</label> </td> <td><label for="surname">{$item.address}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Contact Number</label> </td> <td><label for="surname">{$item.cnumber}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Contact Person</label> </td> <td class = "td res" ><label for="surname">{$item.cperson}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Email</label> </td> <td class = "td res" ><label for="surname">{$item.email}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">School Center</label> </td> <td class = "td res" ><label for="surname">{$item.school_center}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Training Date</label> </td> <td class = "td res" ><label for="surname">{$item.t_date}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Certification Date</label> </td> <td class = "td res" ><label for="surname">{$item.c_date}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">No.Teachers</label> </td> <td class = "td res" ><label for="surname">{$item.no_teacher}</label> </td> </tr> <tr> <td class = "td title" ><label for="surname">Teachers Certified</label> </td> <td class = "td res" ><label for="surname">{$item.cert_teacher}</label> </td> </tr> <tr> <td class = "td title" ><label </form> </div> </div> 

I forgot to mention that you need to link media query print tag in tag 我忘了提到你需要在标签中链接媒体查询打印标签

<link rel="stylesheet" type="text/css" href="print.css" media="print">

I prefer to use external css file 我更喜欢使用外部css文件

Use media tag in css file: 在css文件中使用媒体标记:

All your print styles go here inside @media print{...} 所有的打印样式都在@media print {...}里面

@media print {

    .header, .footer, .navigation{

    display: none !important; 

    } 
}

There are some rules, keep in mind that you don't need bunch of things while printing, menus, sidebars etc... 有一些规则,请记住,在打印,菜单,侧边栏等时,您不需要大量的东西......

at last I found an answer. 最后我找到了答案。 I don't know the reason why @media was not working correctly. 我不知道为什么@media无法正常工作。 So I used css styling in the print function itself. 所以我在打印功能本身使用了css样式。

w.document.write('<style>h1{font-size:20px;color:#76C04E;width:90%;}</style>');
w.document.write('<style>body{background: #FFFFFF; color: #000000; font: 85% arial, verdana, helvetica, sans-serif; }</style>');

w.document.write('<style>.res td{font-size: 12px;font-family: open sans, arial; border-top: 1px solid #ddd; width: auto;padding: 4px;} </style>');

Now the css is correctly taken in the print preview page. 现在css在打印预览页面中正确显示。 But it works only in firefox.Will update the answer accordingly when I find the solution for chrome 但它只适用于firefox。当我找到chrome的解决方案时,会相应地更新答案

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM