美文网首页
浏览器 自带打印调用以及样式修改与调试

浏览器 自带打印调用以及样式修改与调试

作者: storyWrite | 来源:发表于2023-10-11 14:10 被阅读0次

    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.效果

    • 正常浏览网页样式
    • 打印预览样式

    3.如何调试打印样式

    • 如何打开打印样式
    • 打开后效果
      其中背景色样式在实际进行打印输出时不起作用,并没有对应的背景色,虽然预览的时候存在背景色

    相关文章

      网友评论

          本文标题:浏览器 自带打印调用以及样式修改与调试

          本文链接:https://www.haomeiwen.com/subject/tvogbdtx.html