558

I have a link on my webpage to print the webpage. However, the link is also visible in the printout itself.

Is there javascript or HTML code which would hide the link button when I click the print link?

Example:

 "Good Evening"
 Print (click Here To Print)

I want to hide this "Print" label when it prints the text "Good Evening". The "Print" label should not show on the printout itself.

TRiG
  • 10,148
  • 7
  • 57
  • 107

11 Answers11

912

In your stylesheet add:

@media print
{    
    .no-print, .no-print *
    {
        display: none !important;
    }
}

Then add class='no-print' (or add the no-print class to an existing class statement) in your HTML that you don't want to appear in the printed version, such as your button.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • 1
    media="print" did not work on a certain site for some reason, but this trick did. Kudos! – mwilcox Jan 31 '12 at 16:18
  • 24
    This answer is not complete. You have to have two media sections. `@media` print and `@media` screen. In the print section, you place your styles for printing. In the screen screen section, you place your styles for screen printing. You can even have multiple screen sections for different resolutions. Basically if you just add what's given in this answer, it will not work. Believe me I've tried. So how did this get 69 upvotes? – Codeguy007 Nov 16 '12 at 21:39
  • 6
    actually, it does work if you just use the code that is give. i just tried in firefox. so, at least in modern browsers this is the solution. – andyrandy Feb 21 '13 at 10:21
  • 8
    I did try to hide a
    with several sub-elements inside. Problem was that some elements were still visible on print. Solution was to use this css: `@media print { .no-print, .no-print * { display: none !important; } }`
    – Philipp Apr 09 '13 at 17:17
  • 6
    This may or may not work depending on the media specified. For example, to combine both screen and print styles in one stylesheet, then use media queries within that stylesheet, as this answer suggests, you must specify both "screen" and "print" media types: `` – Clint Pachl May 27 '13 at 21:56
  • 9
    @Codeguy007: I don't understand your point. You only need `@media screen` if you want to define screen-only styles but this is not the case here: By default all elements are displayed, and by defining a print-only styling which hides the element is perfectly sufficient to make it still appear on the screen. – chiccodoro Aug 30 '13 at 12:42
  • 2
    @Chiccodoro we had problems with some browsers when when we didn't do it that way. Maybe the spec says you shouldn't have to define '@media screen' but as soon as we added '@media print' the page stopped working properly in certain browsers. – Codeguy007 Aug 30 '13 at 15:28
233

Bootstrap 3 has its own class for this called:

hidden-print

It is defined like this:

@media print {
  .hidden-print {
    display: none !important;
  }
}

You do not have to define it on your own.


In Bootstrap 4 and Bootstrap5 this has changed to:

.d-print-none
Joe Sadoski
  • 586
  • 2
  • 7
  • 22
Bijan
  • 25,559
  • 8
  • 79
  • 71
  • is it class=".d-print-none" ? Or how do i use? Cause i tried that and didnt work, i use bootstrap 5. I used window.print in a .js file to print. – morknox Nov 22 '22 at 12:51
  • @morknox do not use the `.`, just write `class="d-print-none"` or even `class="class-foo d-print-none class-bar class-baz"`. The `.` means that the css selects elements with _class_ `d-print-none`. Similarly, `#d-print-none` would select element with _id_ `d-print-none`. – student91 Jan 24 '23 at 16:43
180

The best practice is to use a style sheet specifically for printing, and set its media attribute to print.

In it, show/hide the elements that you want to be printed on paper.

<link rel="stylesheet" type="text/css" href="print.css" media="print" />
MR AND
  • 376
  • 7
  • 29
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 9
    In here you should also have css to not display other wrapper content, change the font-family to a better value, remove widths to allow for complete use of the page. If you set your main stylesheet to media="screen" you can treat your print stylesheet as a new playgound. – Steve Perks Dec 29 '08 at 17:54
  • 2
    Absolutely. And with this approach, you can either just remove "Click here for printable version" or add "This page is printer-friendly" or similar. Good practice is, as Steve Perks said, remove all unnessecary content like navigation, ads and the like. Include only the primary content of the page. – Arve Systad Sep 03 '09 at 15:51
73

Here is a simple solution put this CSS

@media print{
   .noprint{
       display:none;
   }
}

and here is the HTML

<div class="noprint">
    element that need to be hidden when printing
</div>
Coder
  • 1,175
  • 1
  • 12
  • 32
  • 3
    A class would be a better choice than an id here, for the simple reason that if you use an id then you can only apply this rule to one thing per page. A class would allow you to apply it wherever needed. – Nick F Aug 01 '17 at 16:55
  • And I believe you can change display:none to visibility:hidden if you want it items to take up the same space, just not show. – alfadog67 Jul 07 '20 at 19:48
  • So, a simple and workable solution. Before I thought we can not affect the print version of the page. – Victor Sokoliuk Sep 08 '20 at 07:01
  • 2
    this should me marked as approved answer. – Rana Nadeem May 24 '21 at 04:03
15

CSS FILE

@media print
{
    #pager,
    form,
    .no-print
    {
        display: none !important;
        height: 0;
    }


    .no-print, .no-print *{
        display: none !important;
        height: 0;
    }
}

HTML HEADER

<link href="/theme/css/ui/ui.print.css?version=x.x.x" media="print" rel="stylesheet" type="text/css" >

ELEMENT

<div class="no-print"></div>
user3806549
  • 1,428
  • 1
  • 17
  • 25
13

You could place the link within a div, then use JavaScript on the anchor tag to hide the div when clicked. Example (not tested, may need to be tweaked but you get the idea):

<div id="printOption">
    <a href="javascript:void();" 
       onclick="document.getElementById('printOption').style.visibility = 'hidden'; 
       document.print(); 
       return true;">
       Print
    </a>
</div>

The downside is that once clicked, the button disappears and they lose that option on the page (there's always Ctrl+P though).

The better solution would be to create a print stylesheet and within that stylesheet specify the hidden status of the printOption ID (or whatever you call it). You can do this in the head section of the HTML and specify a second stylesheet with a media attribute.

Lucky
  • 16,787
  • 19
  • 117
  • 151
Justin Scott
  • 865
  • 4
  • 10
8

@media print {
  .no-print {
    visibility: hidden;
  }
}
<div class="no-print">
  Nope
</div>

<div>
  Yup
</div>
Mike Rapadas
  • 4,613
  • 2
  • 28
  • 21
5

The best thing to do is to create a "print-only" version of the page.

Oh, wait... this isn't 1999 anymore. Use a print CSS with "display: none".

  • 1
    there is still some value in printable versions because it clearly displays to the user what they'll get when they print which can be abused with CSS techniques – annakata Dec 23 '08 at 15:50
  • 1
    added to that, you can include additional content on a print version like link references, footers, etc. In days to come, a lot of this will be achievable through css too though. – Steve Perks Dec 29 '08 at 17:50
  • 9
    @annakata: The user can already get that using "Print Preview" on their browser. As a bonus, that's actually what it's for. – recursive May 07 '12 at 21:53
5

The accepted answer by diodus is not working for some if not all of us. I could not still hide my Print this button from going out on to paper.

The little adjustment by Clint Pachl of calling css file by adding on

      media="screen, print" 

and not just

      media="screen"

is solving this problem. So for clarity and because it is not easy to see Clint Pachl hidden additional help in comments. The user should include the ",print" in css file with the desired formating.

     <link rel="stylesheet" href="my_cssfile.css" media="screen, print"type="text/css">

and not the default media = "screen" only.

    <link rel="stylesheet" href="my_cssfile.css" media="screen" type="text/css">

That i think solves this problem for everyone.

webs
  • 528
  • 5
  • 11
5

If you have Javascript that interferes with the style property of individual elements, thus overriding !important, I suggest handling the events onbeforeprint and onafterprint. https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint

Elias Hasle
  • 637
  • 7
  • 15
1

As Elias Hasle said, JavaScript can override !important. So, I extended his answer with a theoretical implementation.

This code identifies all elements with the class no-print, hides them with CSS before printing, and restores the original style after printing:

var noPrintElements = [];

window.addEventListener("beforeprint", function(event) {
   var hideMe = document.getElementsByClassName("no-print");
   noPrintElements = [];
   Array.prototype.forEach.call(hideMe, function(item, index) {
      noPrintElements.push({"element": item, "display": item.style.display });
      item.style.display = 'none'; // hide the element
   });   
});

window.addEventListener("afterprint", function(event) {
   Array.prototype.forEach.call(noPrintElements, function(item, index) {
      item.element.style.display = item.display; // restore the element
   });      
   noPrintElements = []; // just to be on the safe side
});
Joseph Didion
  • 142
  • 1
  • 6
BogisW
  • 401
  • 2
  • 16