Bootstrap 3.x打印預(yù)覽背景色與文字顯示異常的解決
我首先測試了一段如下的代碼,發(fā)現(xiàn)打印預(yù)覽時(shí)的確無法顯示背景色。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Regonline</title> <link href="css/bootstrap.css" rel="stylesheet" /> <link href="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> <div class="main"> <div class="one"></div> <div class="two"></div> <div class="three"></div> </div> </body> </html>
比較奇怪的是,如果我刪除bootstrap的樣式引用,就可以正常打印預(yù)覽了。想來必定是bootstrap3 設(shè)置了@media print
相關(guān)屬性導(dǎo)致。
果然,翻開源碼,發(fā)現(xiàn)如下代碼:
@media print { * { 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: 1px solid #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; } .table td, .table th { background-color: #fff !important; } .btn > .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px solid #000; } .table { border-collapse: collapse !important; } .table-bordered th, .table-bordered td { border: 1px solid #ddd !important; } }
注意代碼里面的 color:#000 !important;
以及 background-color:transparent !important;
。它表示打印時(shí),頁面中的文字都為黑色,并且背景色都設(shè)置為透明。因?yàn)橛辛诉@樣的樣式,我們的背景色就打印
不出來了。去掉這兩段代碼,一切OK!
值得一提的說:如果頁面中有超鏈接,打印時(shí)會(huì)顯示超鏈接的地址,這樣比較難看。我們刪除對(duì)應(yīng)的樣式即可。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
如何利用Three.js實(shí)現(xiàn)跳一跳小游戲
最近在公司寫H5的3D游戲,選擇了ThreeJS去做,做的過程中遇到了很多問題,下面這篇文章主要給大家介紹了關(guān)于如何利用Three.js實(shí)現(xiàn)跳一跳小游戲的相關(guān)資料,需要的朋友可以參考下2022-04-04JS實(shí)現(xiàn)時(shí)間校驗(yàn)的代碼
這篇文章主要介紹了JS實(shí)現(xiàn)時(shí)間校驗(yàn)的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05JavaScript中數(shù)組flat方法的使用與實(shí)現(xiàn)方法
在Array的顯示原型下有一個(gè)flat方法,可以將多維數(shù)組,降維,傳的參數(shù)是多少就降多少維,下面這篇文章主要給大家介紹了關(guān)于JavaScript中數(shù)組flat方法的使用與實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-08-08解決微信小程序云開發(fā)中獲取數(shù)據(jù)庫的內(nèi)容為空的方法
這篇文章主要介紹了解決微信小程序云開發(fā)中獲取數(shù)據(jù)庫的內(nèi)容為空的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05一文詳解GoJs中g(shù)o.Panel的itemArray屬性
這篇文章主要為大家介紹了一文詳解GoJs中g(shù)o.Panel的itemArray屬性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05