Most web designers are not familiar with print control, and they tend to be more obsessed with pixels than printers. In the real world, many people rely on Web pages for reference: In this digital age, in a number of special occasions, many people hold the paper in their hands. Web developers can take steps to compensate for the gap between the printer and the LCD screen.
Style for printer instead of screen design
12345 |
/* 样式将只应用于打印 */ @media print { } |
Note * You can also use a separate CSS file, set Link's media= "Print" property to specify that this style is dedicated to printing
1 |
<link type="text/css" rel="stylesheet" href="css/print.css" media="print"> |
It is not necessary to reshape the entire CSS for your site, as a whole, by printing to inherit the default sample; In order to save toner when printing, most browsers will automatically invert the color. In order to achieve the best results, the color changes should be obvious:
1234567 |
/*白纸黑字*/ @media print { body { color: #000; background: #fff; } } |
We're not creating the entire page, just to show a well-designed, readable website:
12345678910111213141516171819 |
/*去除背景图片, 节约笔黑 */ h1 {
color: #fff;
background: url(banner.jpg);
}
@media print {
h1 {
color: #000;
background: none;
}
nav, aside {
display: none;
}
}
|
To make the printer more efficient, you should only show the body content, remove the header and footer navigation bar
1234567891011121314151617181920212223 |
@media print {
h1 {
color: #000;
background: none;
}
nav, aside {
display: none;
}
body, article {
width: 100%;
margin: 0;
padding: 0;
}
@page {
margin: 2cm;
}
}
|
Processing of links
Hyperlinks are not visible on the printer and should be extended
123456789101112 |
/*在超链接后面添加带@media print { article a { font-weight: bolder; text-decoration: none; } article a[href^=http]:after { content:" <" attr(href) "> "; } } |
The display may be the same
Control Print Setup Options
The @page rule allows you to specify various aspects of the page. For example, you will specify the dimensions of the page. Margins, header footers, and so on are all important. [Many browsers are supported]
@PAGE Regular paper size settings
With this CSS you can set the paper size, 5.5 inches wide and 8.5 inches high.
123 |
@page { size: 5.5in 8.5in; } |
You can also control the paper size by alias, such as "A4" or "legal."
You can also control the direction of printing, Portrait: Portrait print, Landscape: landscape
123 |
@page { size: A4 landscape; } |
Page Model the page models
In a paged media format model, documents are transferred to one or more page boxes. The page box is mapped to a rectangular plane. This is roughly similar to the CSS box model.
Note * Fewer browsers supported
Page Margin Model Page-margin Boxes
Before further discussion, we should understand the box model of the page, because its behavior is somewhat different from how it works on the screen.
The page model defines the page area and then divides the 16-week edge box. You can control the size of the page area and the margin between the edge of the page area and the end of the page itself.
Left and right margins
12345678 |
@page :left { margin-left: 30cm; } @page :right { margin-left: 4cm; } |
The following CSS will show the title on the bottom left, the page counter in the lower right corner, and a chapter title in the upper right corner.
12345678910111213141516171819202122232425 |
@page:right{
@bottom-left {
margin: 10pt 0 30pt 0;
border-top: .25pt solid #666;
content: "Our Cats";
font-size: 9pt;
color: #333;
}
@bottom-right {
margin: 10pt 0 30pt 0;
border-top: .25pt solid #666;
content: counter(page);
font-size: 9pt;
}
@top-right {
content: string(doctitle);
margin: 30pt 0 10pt 0;
font-size: 9pt;
color: #333;
}
}
|
The display results are as follows:
Note * This article is organized from: Tips and Tricks for print Style Sheets and designing for print with CSS
CSS style designed for control of print design