瀏覽器 自帶打印調(diào)用以及樣式修改與調(diào)試

1. 代碼

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>print</title>
</head>

<body>
  <style>
    .border {
      border: 1px solid red;
    }
    .box1 {
      width: 300px;
      height: 400px;
      background: red;
    }
    .box2 {
      width: 300px;
      height: 400px;
      border: 1px solid blue;
    }

    @media print {
      .box2 {
        display: none;
      }
      .box1 {
        /* 無效果 */
        background-color: aqua;
        border-color: blueviolet;
      }
      .print-button {
        display: none;
      }
    }
  </style>
  <div class="box1 border">
    box1 打印顯示
  </div>
  <div class="box2">
    box2 打印隱藏
  </div>
  <button onclick="printDocument()" class="print-button">打印整個文檔</button>

  <script>
    function printDocument () {
      window.print()
    }
  </script>
</body>

</html>

2.效果

  • 正常瀏覽網(wǎng)頁樣式
  • 打印預(yù)覽樣式

3.如何調(diào)試打印樣式

  • 如何打開打印樣式
  • 打開后效果

    其中背景色樣式在實際進(jìn)行打印輸出時不起作用,并沒有對應(yīng)的背景色,雖然預(yù)覽的時候存在背景色
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容