打印网页时删除页面标题和日期(使用 CSS?)

默认情况下,打印网页时,页面 书名网址打印在页面的顶部,同样,约会时间打印在页面的底部。

当您通过“ PAGE 设置”菜单(在 InternetExp 中的“文件”下)打印时,可以删除此附加内容

有人知道一种通过 CSS 或者 javascript 实现的方法吗?

128735 次浏览

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.

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.

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.

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

Its simple. Just use css.

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

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.

Try this;

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

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;
}
}

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

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>'+ ...)