Bootstrap 3.x打印预览背景色与文字显示异常的解决
我首先测试了一段如下的代码,发现打印预览时的确无法显示背景色。
<!DOCTYPEhtml> <htmllang="en"> <head> <metacharset="utf-8"> <title>Regonline</title> <linkhref="css/bootstrap.css"rel="stylesheet"/> <linkhref="css/font-awesome.min.css"rel="stylesheet"/> </head> <body> <style> .main{ overflow:hidden; padding:40px; } .one,.two,.three{ float:left; height:40px; } .one{ width:40%; background-color:red; } .two{ width:30%; background-color:green; } .three{ width:30%; background-color:pink; } </style> <divclass="main"> <divclass="one"></div> <divclass="two"></div> <divclass="three"></div> </div> </body> </html>
比较奇怪的是,如果我删除bootstrap的样式引用,就可以正常打印预览了。想来必定是bootstrap3设置了@mediaprint相关属性导致。
果然,翻开源码,发现如下代码:
@mediaprint{ *{ color:#000!important; text-shadow:none!important; background:transparent!important; box-shadow:none!important; } a, a:visited{ text-decoration:underline; } a[href]:after{ content:"("attr(href)")"; } abbr[title]:after{ content:"("attr(title)")"; } a[href^="javascript:"]:after, a[href^="#"]:after{ content:""; } pre, blockquote{ border:1pxsolid#999; page-break-inside:avoid; } thead{ display:table-header-group; } tr, img{ page-break-inside:avoid; } img{ max-width:100%!important; } p, h2, h3{ orphans:3; widows:3; } h2, h3{ page-break-after:avoid; } select{ background:#fff!important; } .navbar{ display:none; } .tabletd, .tableth{ background-color:#fff!important; } .btn>.caret, .dropup>.btn>.caret{ border-top-color:#000!important; } .label{ border:1pxsolid#000; } .table{ border-collapse:collapse!important; } .table-borderedth, .table-borderedtd{ border:1pxsolid#ddd!important; } }
注意代码里面的color:#000!important;以及background-color:transparent!important; 。它表示打印时,页面中的文字都为黑色,并且背景色都设置为透明。因为有了这样的样式,我们的背景色就打印
不出来了。去掉这两段代码,一切OK!
值得一提的说:如果页面中有超链接,打印时会显示超链接的地址,这样比较难看。我们删除对应的样式即可。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。