Bootstrap 3.x預覽列印背景色與文字顯示異常的解決_javascript技巧

來源:互聯網
上載者:User

我首先測試了一段如下的代碼,發現預覽列印時的確無法顯示背景色。

<!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的樣式引用,就可以正常預覽列印了。想來必定是bootstrap3 設定了@media print相關屬性導致。

果然,翻開源碼,發現如下代碼:

@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;  。它表示列印時,頁面中的文字都為黑色,並且背景色都設定為透明。因為有了這樣的樣式,我們的背景色就列印

不出來了。去掉這兩段代碼,一切OK!

值得一提的說:如果頁面中有超連結,列印時會顯示超連結的地址,這樣比較難看。我們刪除對應的樣式即可。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.