如何打印一个网页,其所有 css 样式都附加到页面上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3384985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:29:56  来源:igfitidea点击:

how can I print a web page with all its css style attached to the page?

cssprintingwebpage

提问by newguy

I want to add a print button to enable users to print my web page. However when I print all the css styles are lost. It seems the printer only keeps the basic html elements. If I also want to print out the page exactly as it looks like in a browser, what should I do? I mean if I use a color printer it will print a colorful page with css styles on it. When I use a black and white printer it should print light colors as white and dark colors as grey or black.

我想添加一个打印按钮,使用户能够打印我的网页。但是,当我打印所有 css 样式时都丢失了。似乎打印机只保留基本的 html 元素。如果我还想完全按照浏览器中的样子打印页面,我该怎么办?我的意思是,如果我使用彩色打印机,它会打印出带有 css 样式的彩色页面。当我使用黑白打印机时,它应该将浅色打印为白色,将深色打印为灰色或黑色。

回答by Daniel O'Hara

If you have media="screen"on your linkelement, try to remove it or replace with media="all".

如果您media="screen"link元素上有,请尝试将其删除或替换为media="all".

回答by Dan

Take a look at thisand this, that should help.

看看这个这个,应该会有所帮助。

Basically you need to add a css print statement.

基本上你需要添加一个css打印语句。

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

回答by ankitjaininfo

CSS style-sheets have an attribute media. It specifies to what media this css is applicable. Possible media types are listed here.

CSS 样式表有一个属性media。它指定此 css 适用于哪些媒体。此处列出可能的媒体类型。

<style type="text/css" media="all">
   @import "myCss.css";
</style>

OR

或者

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