最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add Print headers and footers in HTML, CSS and JavaScript? - Stack Overflow

programmeradmin6浏览0评论

I have a document in HTML and CSS. I would like to have headers and footers in every printed page so that when you press Print, each page has the same footer and header. Is there a way to do this using web technologies? I can't find much information on printing besides page-break-after and so on.

I have a document in HTML and CSS. I would like to have headers and footers in every printed page so that when you press Print, each page has the same footer and header. Is there a way to do this using web technologies? I can't find much information on printing besides page-break-after and so on.

Share asked Jul 12, 2011 at 13:16 TowerTower 103k131 gold badges364 silver badges521 bronze badges 2
  • 4 There's not support for this per se in web technologies. What you can try doing is this: (1) have <div> for header and another for footer on your pages, (2) define different stylesheets for display and print; (3) in display stylesheet, set display:none for those divs; (4) in print stylesheet, set display:block. – Aleks G Commented Jul 12, 2011 at 13:20
  • possible duplicate of HTML Print Header & Footer – Shef Commented Jul 12, 2011 at 13:21
Add a ment  | 

2 Answers 2

Reset to default 6

This is what I found out:

You can use position: fixed and top: 0 or bottom: 0 to position an element in top/bottom of each page. However, since fixed positioning does not work on IE6, the only choice for that is to use a table with 100% height and thead and tfoot. It is some sort of magic that works the way you want it to (to have the contents of thead and tfoot to appear on each page). If you don't care about IE6 just use fixed positioning.

However, fixed positioning also means that any content you have there may go behind the fixed positioned element. So, in effect, the header and the footer does not occupy any space.

You can't control the actual header/footer (e.g. the URL that is printed). The user has to disable those himself/herself.

The best alternative to painful HTML/CSS printing is PDF. You can control how each page look like, what's the size of them and header/footer. The drawbacks are slower prints (generating PDF is not as fast as loading a HTML page) and the pain of working your way with some PDF library.

You CAN'T set the header and footer, it's the browser which handles that part.

发布评论

评论列表(0)

  1. 暂无评论