Removing page title and date when printing web page (with CSS?)

JavascriptHtmlCss

Javascript Problem Overview


By default, when you print a web page, the page title and and URL are printed at the top of the page, and likewise the date and time are printed at the bottom.

It is possible to remove this additional as you are printing through the PAGE SETUP menu (under FILE in Internet Exp)

Does anyone know of a way of doing this via CSS or javascript?

Javascript Solutions


Solution 1 - Javascript

Historically, it's been impossible to make these things disappear as they are user settings and not considered part of the page you have control over.

However, as of 2017, the @page at-rule has been standardized, which can be used to hide the page title and date in modern browsers:

@page { size: auto;  margin: 0mm; }

> Print headers/footers and print margins

> When printing Web documents, margins are set in the browser's Page Setup (or Print Setup) dialog box. These margin settings, although set within the browser, are controlled at the operating system/printer driver level and are not controllable at the HTML/CSS/DOM level. (For CSS-controlled printed page headers and footers see Printing Headers .)

> The settings must be big enough to encompass the printer's physical non-printing areas. Further, they must be big enough to encompass the header and footer that the browser is usually configured to print (typically the page title, page number, URL and date). Note that these headers and footers, although specified by the browser and usually configurable through user preferences, are not part of the Web page itself and therefore are not controllable by CSS. In CSS terms, they fall outside the Page Box CSS2.1 Section 13.2.

... i.e. setting a margin of 0 hides the page title because the title is printed in the margin.

Credit to Vigneswaran S for this tip.

Solution 2 - Javascript

Its simple. Just use css.

<style>
@page { size: auto;  margin: 0mm; }
</style>

Solution 3 - Javascript

A possible workaround for the page title:

  • Provide a print button,
  • catch the onclick event,
  • use javascript to change the page title,
  • then execute the print command via javascript as well.

document.title = "Print page title"; window.print();

This should work in every browser.

Solution 4 - Javascript

You can add this in your stylesheet: @page{size:auto; margin:5mm;} But this discards the page number too

Solution 5 - Javascript

completing Kai Noack's answer, I would do this:

var originalTitle = document.title;
document.title = "Print page title";
window.print();
document.title = originalTitle;

this way once you print page, This will return to have its original title.

Solution 6 - Javascript

Nothing works for me except the following solution from @Md. Hasibul Huq. You can use it without styles for body:

@media print {
  @page {
    margin-top: 0;
    margin-bottom: 0;
  }
  body  {
    padding-top: 5rem;
    padding-bottom: 5rem;
  }
}

Solution 7 - Javascript

This is not a programming solution (I absolutely know that)
The person who asked this question was seeking an answer with CSS, I am aware of that, and this answer will not help him at all, but it could help others (like me) which brought me to here in the first place.


but It can save your life especially if your client calls you on Weekend to fix it :)
(like what happened to me)

quickly she (your client) can expand the "More Settings" and uncheck the "Headers and footers" from Chrome page, as shown in the image below,

Again the programming solution for this problem is as in the accepted answer, I will not repeat it, but if you do not want to deploy your application on Weekend, then this is the way to go

enter image description here

Solution 8 - Javascript

Try this;

@media print{ 
  @page {
    margin-top: 30px;
    margin-bottom: 30px;
  }
}

Solution 9 - Javascript

To add custom page title in the place of about:blank when printing via window.print(): Add the following lines:

document.write('<head>' +
   '<title>' +
   'My Title' +
   '</title>'+ ...)

Solution 10 - Javascript

There's a facility to have a separate style sheet for print, using

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

I don't know if it does what you want though.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionswisstonyView Question on Stackoverflow
Solution 1 - JavascriptgreimView Answer on Stackoverflow
Solution 2 - JavascriptVigneswaran SView Answer on Stackoverflow
Solution 3 - JavascriptAvatarView Answer on Stackoverflow
Solution 4 - JavascriptmtchuenteView Answer on Stackoverflow
Solution 5 - JavascriptLuckeView Answer on Stackoverflow
Solution 6 - JavascriptRomanView Answer on Stackoverflow
Solution 7 - JavascriptHakan FıstıkView Answer on Stackoverflow
Solution 8 - JavascriptBugay SarikayaView Answer on Stackoverflow
Solution 9 - JavascriptPatricodeView Answer on Stackoverflow
Solution 10 - Javascriptuser181548View Answer on Stackoverflow