12

Let's say that I have

<div id="printOnly">
     <b>Title</b>
     <p>
        Printing content 
     </p>
</div>

Is it possible to hide this div when page rendering and to show only when printing this div?

user1765862
  • 12,589
  • 27
  • 102
  • 186
  • 2
    possible duplicate of [Print
    only?](http://stackoverflow.com/questions/468881/print-div-id-printarea-div-only)
    – S.Krishna Jun 22 '15 at 07:46

8 Answers8

31

You need some css for that

#printOnly {
   display : none;
}

@media print {
    #printOnly {
       display : block;
    }
}
Nikhil Aggarwal
  • 27,657
  • 4
  • 40
  • 56
8
@media screen
{
    #printOnly{display:none;}
}

@media print
{
    #printOnly{}
}
Kiran Dash
  • 4,474
  • 11
  • 47
  • 80
5

You should use media query.

In your case:

#printOnly {
    display: none;
}

@media print { 
    #printOnly {
        display: block;
    }
}

PS take a look here http://www.joshuawinn.com/css-print-media-query/ for browser support

alberto-bottarini
  • 1,197
  • 8
  • 21
1

You can attach external css stylesheet with media="print" attribute:

<link rel="stylesheet" type="text/css" media="print" href="print.css">
hsz
  • 143,040
  • 58
  • 252
  • 308
1

I think the best solution would be to create a wrapper around the non-printable stuff:

<head>
    <style type="text/css">

    #printable { display: none; }

    @media print
    {
        #non-printable { display: none; }
        #printable { display: block; }
    }
    </style>
</head>
<body>
    <div id="non-printable">
        Your normal page contents
    </div>

    <div id="printable">
        Printer version
    </div>
</body>
Shrinivas Pai
  • 7,185
  • 4
  • 26
  • 55
1
/*for printer*/
@media print {
    #printOnly { }
        /* write your css rules*/

}
/*for desktop*/
    @media screen {
    #printOnly { display: none;}
             /*for all display view*/                 
}
GabrieleU
  • 51
  • 1
  • 2
  • 8
1
@media screen { #printOnly:{display:none;} }
@media print{ #printOnly:{display:block;} }
Abdullah Al Shakib
  • 1,954
  • 2
  • 13
  • 15
0

You need media query for switching between print and screen option

@media screen { /* for screen option*/

p {

      font-family: verdana, sans-serif;

      font-size: 17px;

  }

   }

@media print { /* for print option*/

 p {

     font-family: georgia, serif;
     font-size: 14px;
     color: blue;
   }

  } 

http://www.w3schools.com/css/css_mediatypes.asp

sach
  • 271
  • 1
  • 7