Table of Contents
语法
@media
@page
使用
像素单位
页边距
换页打印
背景图片和颜色
Home Web Front-end HTML Tutorial Print Css控制打印效果_html/css_WEB-ITnose

Print Css控制打印效果_html/css_WEB-ITnose

Jun 24, 2016 am 11:17 AM

不经过任何处理而直接打印网站上的页面会得到一个不理想的效果。显示器(screen)和打印机(printer)是两种差别很大的设备,所以从浏览器里看到的页面,打印出来也许和你看到的样子有很大的差距。因此如果要精确的控制打印效果就应该使用print css。接下来,我们来先了解一些语法。

语法

@media

  • 描述: @media用于指定的设备类型使用不同的样式。
  • 语法:@media sMedia { sRules }
  • 说明:sMedia:设备类型,sRules:样式表定义
  • 兼容性:IE5+(这里只是指CSS2,C33@media的功能更加强大,但是只有IE9+支持)
设备类型 CSS版本 兼容性 简介
all CSS2 IE4+ 用于所有设备类型
aural CSS2 NONE 用于语音和音乐合成器
braille CSS2 NONE 用于触觉反馈设备
embossed CSS2 NONE 用于凸点字符(盲文)印刷设备
handheld CSS2 NONE 用于小型或手提设备
print CSS2 IE4+ 用于打印机
projection CSS2 NONE 用于投影图像,如幻灯片
screen CSS2 IE4+ 用于计算机显示器
tty CSS2 NONE 用于使用固定间距字符格的设备。如电传打字机和终端
tv CSS2 NONE 用于电视类设备

@page

  • 描述: 用于指定打印页面的一些属性,包括纸张尺寸,方向,页边距,分页等特性。
  • 语法:@page { sRules }
  • 说明::页面标识符 :打印伪类:first, :left, :right
  • 兼容性:IE6+

使用

重新针对打印写CSS样式是没有必要的,我们只需要针对差异设置打印的样式覆盖掉之前的默认样式。

// 设置显示器用字体尺寸@media screen {body {font-size:12pt; }}// 设置打印机用字体尺寸@media print {body {font-size:8pt;}}
Copy after login

像素单位

在编写打印样式表的时候,你要注意要使用厘米或者英寸作为单位而不是屏幕像素单位,实际的单位对打印非常有用。常见的DPI(Dot Per Inch)为96的screen,px与cm的换算关系如下:

  • 1 inch = 2.54 cm

  • 1cm = 96/2.54 ≈ 37.80 px

  • 1px = 2.54/96 ≈ 0.0265 cm

  • 100px = 2.65 cm

A4纸的标准尺寸为:

  • 21.0cm * 29.7 cm

在96DPI分辨率下,其对应的像素尺寸大约为:

  • 794px * 1123px

页边距

为了保证打印样式有用,写CSS样式使打印的内容距离纸张边缘,看起来更好,需要使用 @page 这个语法:

@media print {   @page {      margin: 2cm;   }}
Copy after login

换页打印

  • 为了保证不被跨页打印,如防止一个标题和内容在页面底部被分开:

h2, h3 { page-break-after: avoid; }
Copy after login

  • 确保 articles 文章标签的内容能在新的一页开始:

article {   page-break-before: always;}
Copy after login

  • 防止列表和图片分开在不同的页:

ul, img {   page-break-inside: avoid;}
Copy after login

  • 属性介绍
属性 描述 CSS
page-break-after 设置元素后的分页行为 2
page-break-before 设置元素前的分页行为 2
page-break-inside 设置元素内部的分页行为 2
描述
auto 默认。如果必要则在元素后插入分页符。
always 在元素后插入分页符。在遇到特定的组件时,打印机会重新开始一个新的打印页
avoid 避免在元素后插入分页符。
left 在元素之后足够的分页符,一直到一张空白的左页为止。
right 在元素之后足够的分页符,一直到一张空白的右页为止。
inherit 规定应该从父元素继承 page-break-after 属性的设置。

背景图片和颜色

如果用户是在 webkit 内核浏览器上打印的话,我们可以强制打印机打印屏幕上所看到的颜色(即强制在打印页面上出现任何的背景图和颜色),一般来说彩色打印机可以做到这点,我们需要一个单独的媒体查询:

@media print and (color) {   * {      -webkit-print-color-adjust: exact;      print-color-adjust: exact;   }}
Copy after login

注意:在firefox opera 和IE,不能使用背景图和颜色。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles