HTML Element Style How to - Show/hide image on print








Question

We would like to know how to show/hide image on print.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.print-image {<!-- w w w.j a va  2 s.  c  o  m-->
  display: none;
}

@media print {
  .screen-image {
    display: none;
  }
  .print-image {
    display: block;
  }
}
</style>
</head>
<body>
  <div class="screen-image">
    <img src="http://www.java2s.com/style/download.png" />
    <p>Image for normal viewing.</p>
  </div>
  <div class="print-image">
    <img src="http://www.java2s.com/style/download.png" />
    <p>Image for print view.</p>
  </div>
</body>
</html>

The code above is rendered as follows: