简体   繁体   English

如何使用CSS格式化HTML表格进行打印?

[英]How to format an HTML table using CSS for printing?

I want to format table using CSS to show an attendance sheet. 我想使用CSS格式化表格以显示考勤表。 How do I add CSS styles to the table so that it can be printed? 如何将CSS样式添加到表中以便可以打印?

Within your style sheet, use 在您的样式表中,使用

@media print {
  /* put your CSS rules if they are to apply in print only */
}

For example, 例如,

@media print {
   * { color: black; background: white; }
   table { font-size: 80%; }
}

The details depend on the types of problems you expect to have with printing your specific table. 详细信息取决于打印特定表格时所需的问题类型。 Generally, if you have set fixed column widths (eg in pixels), you probably need to reconsider this part of your design. 通常,如果您设置了固定列宽(例如以像素为单位),则可能需要重新考虑设计的这一部分。 If you are using colors to convey essential information (eg, red cell standing for absence, green for presence), you need to reconsider this decision. 如果您使用颜色来传达基本信息(例如,红细胞代表缺席,绿色代表存在),您需要重新考虑此决定。

使用CSS样式表的media="print"属性具有特定于打印的布局。

use following css 使用以下css

table, th, td
{
  border-collapse:collapse;
  border: 1px solid black;
  width:100%;
  text-align:right;
}

also you can use media="print" for print specific layout 您也可以使用media =“print”进行打印特定布局

you can add media specific css styles to a page using the @media Rule 您可以使用@media规则向页面添加媒体特定的CSS样式

for example , @media screen will use the styles for computer screens and @media print is used for printers. 例如,@ @media screen将使用计算机屏幕的样式,@ @media print用于打印机。

using @media all will make sure your styles are applied to all media type devices 使用@media all将确保您的样式应用于所有媒体类型设备

follow link for more on media types 关注媒体类型的更多链接

You can also use 你也可以使用

body {
-webkit-print-color-adjust:exact;
}

Correct all good answers. 纠正所有好的答案。

What I would do is to make a specific css file just for print and attach it to the page: 我要做的是制作一个特定的css文件,仅用于打印并将其附加到页面:

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

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

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