javascript - Disabling browser print options (headers, footers, margins) from page?

ID : 20288

viewed : 19

Tags : javascripthtmlcssbrowserprintingjavascript

Top 5 Answer for javascript - Disabling browser print options (headers, footers, margins) from page?

vote vote

93

The CSS standard enables some advanced formatting. There is a @page directive in CSS that enables some formatting that applies only to paged media (like paper). See http://www.w3.org/TR/1998/REC-CSS2-19980512/page.html.

Downside is that behavior in different browsers is not consistent. Safari still does not support setting printer page margin at all, but all the other major browsers now support it.

With the @page directive, you can specify printer margin of the page (which is not the same as normal CSS margin of an HTML element):

<html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>Print Test</title>     <style type="text/css" media="print">     @page      {         size:  auto;   /* auto is the initial value */         margin: 0mm;  /* this affects the margin in the printer settings */     }      html     {         background-color: #FFFFFF;          margin: 0px;  /* this affects the margin on the html before sending to printer */     }      body     {         border: solid 1px blue ;         margin: 10mm 15mm 10mm 15mm; /* margin you want for the content */     }     </style> </head> <body>   <div>Top line</div>   <div>Line 2</div> </body> </html> 

Note that we basically disables the page-specific margins here to achieve the effect of removing the header and footer, so the margin we set on the body will not be used in page breaks (as commented by Konrad) This means that it will only work properly if the printed content is only one page.

This does not work in Firefox 3.6, IE 7, Safari 5.1.7 or Google Chrome 4.1.

Setting the @page margin does have effect in IE 8, Opera 10, Google Chrome 21 and Firefox 19.
Although the page margins are set correctly for your content in these browsers, the behavior is not ideal in trying to solve the hiding of the header/footer.

This is how it behaves in different browsers:

  • In Internet Explorer, the margin is actually set to 0mm in the settings for this printing, and if you do Preview you will get 0mm as default, but the user can change it in the preview.
    You will see that the page content actually are positioned correctly, but the browser print header and footer is shown with non-transparent background, and so effectively hiding the page content at that position.

  • In Firefox newer versions, it is positioned correctly, but both the header/footer text and content text is displayed, so it looks like a bad mix of browser header text and your page content.

  • In Opera, the page content hides the header when using a non-transparent background in the standard css and the header/footer position conflicts with content. Quite good, but looks strange if margin is set to a small value that causes the header to be partially visible. Also the page margin is not set properly.

  • In Chrome newer versions, the browser header and footer is hidden if the @page margin is set so small that the header/footer position conflicts with content. In my opinion, this is exactly how this should behave.

So the conclusion is that Chrome has the best implementation of this in respect to hiding the header/footer.

vote vote

80

Any recent version of Chrome and Opera, as well as Firefox 48 alpha 1 and greater

You can set the page margin to a size that's too small to contain the text in order to disable this (borrowing from awe's answer):

@page {   size: auto;  /* auto is the initial value */   margin: 0mm; /* this affects the margin in the printer settings */ } html {   background-color: #FFFFFF;   margin: 0px; /* this affects the margin on the HTML before sending to printer */ } body {   border: solid 1px blue;   margin: 10mm 15mm 10mm 15mm; /* margin you want for the content */ }
<ol>   <li>     <a href="data:,No Javascript :-(" target="_blank">Middle-click to open in new tab</a>   </li>   <li>     <a href="javascript:print()">Print</a>   </li> </ol><!-- Hack to work around stack snippet restrictions --><script type=application/javascript>document.links[0].href="data:text/html;charset=utf-8,"+encodeURIComponent('<!doctype html>'+document.documentElement.outerHTML)</script>

For versions of Firefox up to 48 alpha 1

You can add a mozNoMarginBoxes attribute to the <html> tag to prevent the URL, page numbers and other things Firefox adds to the page margin from being printed.

It is working in Firefox 29 and onwards. You can see a screen shot of the difference here, or see here for a live example.

Note that the mozDisallowSelectionPrint attribute in the example is not required to remove the text from the margins; see What does the mozdisallowselectionprint attribute in PDF.js do?.

Other browsers

Unfortunately, there seems to be no way to resolve this problem in Internet Explorer, so you'll have to resort to PDF or ask users to disable margin texts.

The same goes for Safari; according to a comment by @Luiz Perez, the most recent versions of Safari (8, 9.1 and 10) still do not support @page for suppressing margin texts.

I can't find anything on Edge and I don't have a Windows 10 installation available to test.

vote vote

72

As @Awe had said above, this is the solution, that is confirmed to work in Chrome!!

Just make sure this is INSIDE the head tags:

<head> <style media="print">     @page      {         size: auto;   /* auto is the initial value */         margin: 0mm;  /* this affects the margin in the printer settings */     }      body      {         background-color:#FFFFFF;          border: solid 1px black ;         margin: 0px;  /* this affects the margin on the content before sending to printer */    } </style> </head> 
vote vote

60

I have a similar request from a client who wants to have the header, page numbers, and html footer removed. In this case, the client is presenting an HTML page that can double as a formal certificate. The added URL, page, and, header, are irrelevant and lead to a less-than-pleasing final product. In some ways, it just looks cheap.

Media=Print has not been able to disable these browser defaults. The only workaround is to tell the user to click the "Gear" button and toggle those items on/off. Seriously, I had no idea I could do that for 20 years (and we think the typical user will have a clue to click the toggle button?).

If CSS supports Media=Print, it should support the ability to control the entire end-user print experience. I appreciate that the browsers provide the added fields, but, why not allow CSS to control the overall print experience-if that is what's desired. A 90% solution could be 100% with three more fields! A simple:

#BrowserPrintDefaults{display:none}  

would suffice.

Again, it's not a matter whether or not the end-user wants to print it out or not (maybe your client is very private and doesn't want printed URLs floating around. Or maybe a executive team uses a private collaboration sites?). Glad to defend the end-user, but if somebody is seeking an answer, don't respond saying it's the right of the end-user to show or hide. Sometimes it's the right of the client paying the bills.

vote vote

54

Try this code, works 100% for me:
FOR Landscape:

<head> <style type="text/css">  @page{   size: auto A4 landscape;   margin: 3mm; }  </style> </head> 

FOR Portait:

<head> <style type="text/css">  @page{   size: auto;   margin: 3mm; }  </style> </head> 

Top 3 video Explaining javascript - Disabling browser print options (headers, footers, margins) from page?

Related QUESTION?