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
  • 13,635
  • 28
  • 115
  • 220
  • 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

33

You need some css for that

#printOnly {
   display : none;
}

@media print {
    #printOnly {
       display : block;
    }
}
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
8
@media screen
{
    #printOnly{display:none;}
}

@media print
{
    #printOnly{}
}
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84
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,213
  • 9
  • 21
1

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

<link rel="stylesheet" type="text/css" media="print" href="print.css">
hsz
  • 148,279
  • 62
  • 259
  • 315
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,491
  • 4
  • 29
  • 56
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
  • 2,034
  • 2
  • 15
  • 16
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