So, as we all know - CSS has its limitations. If you’ve ever tried to implement printable watermarks with CSS, then you’ve no doubt come across a few of them. From vertical centering, to cross-browser feature support, to mysterious print-mode CSS rendering - printable watermarks turn out to be a real pain to get working across the browser-space. I spent a good amount of time cracking this nut this weekend and thought I’d share.
Go straight to the CSS print watermark example page.
The Good
The first step to getting a printable watermark is to use an inline tag, rather than background-images. In most browsers, background-images won’t be printed unless the user selects an additional browser option to enable it.
From here, we need to place the watermark image so that it is repeated on each page. It looks like the W3C thought of this ahead of time in the positioning module. The position property’s fixed value should ensure that a watermark is printed on each page. Some browsers, like FireFox and Internet Explorer 7, apply this rule correctly.
The Bad
Not all browsers play nicely with position:fixed and we haven’t yet considered using z-indexing to ensure the watermark displays behind all the regular page content. For IE and FireFox 3+, simply applying z-index:-1 to the watermark will do the trick.
Though, it turns out that earlier versions of FireFox (and other Gecko-based browsers) don’t play nicely with negative z-indexing. So, my current approach uses a few Gecko-specific hacks to degrade the watermark approach for FireFox 2. For FireFox 2, the watermark will display over the content, but still display on every page. Unfortunately, this means the CSS for my approach doesn’t validate.
The Ugly
But of course, position:fixed isn’t supported at all in IE6; it appears to ignore the rule altogether and display the image inline. So, I use conditional comments to filter just a *few* IE6-specific hacks:
Absolute positioning and an additional 100% height on the watermark and printable content are the keys to getting this working for IE6:
div.watermark{
position:absolute;
overflow:hidden;
}
div.content{
height:100%;
}
Conclusion
So, this approach still needs a bit of testing. I want to ensure that backgrounds, margins, and paddings don’t break the layout - want to test a few more of these types of edge-cases. One way or the other, I hope you find it useful. if you like it or find problems with it, feel free to comment.
on Aug 24th, 2008 at 7:37 pm
You rock, dude! What an awesome solution to a nasty problem!
on Aug 25th, 2008 at 6:40 am
Hay
Coool solution for watermark printing
on Aug 25th, 2008 at 9:49 am
This is like a midair headshot on IE6. PWNED!
on Aug 27th, 2008 at 3:28 am
[...] Andy Pemberton has put together a simple solution to get the watermark technique to work nicely with print CSS. [...]
on Aug 27th, 2008 at 5:14 pm
[Safari 3.1.2]
the good (ish): horizontal positioning works, first row causes the watermark to show on the first page (only)
the bad: second row shows the watermark on the second page only
the ugly: the third row causes the document to spread over 4 pages instead of 3, with the watermark on the 4th page, clipped a bit!
just tried initial print preview and save to PDF
good luck with this as it’s a nice concept!
on Aug 27th, 2008 at 7:42 pm
Matt: thanks for the testing! Unfortunately Safari (as of 3.1.2) doesn’t support the display:fixed property appropriately when printing, so I decided to kind of give up on support for Safari until they support it. Also, I tried out the latest WebKit nightly build and it has the same issue.
on Aug 29th, 2008 at 11:40 am
[...] 29, 2008 at 11:26 pm · Filed under webdesign Link:Print Watermarks with CSS [...]
on Sep 24th, 2008 at 12:57 am
I m facing issues in IIE7, when i put image tags in CONTENT DIV, with tags, it shows watermark only once and it shows this on page1 with blank contents, the actual data starts from page2 with no watermark :-/
on Sep 24th, 2008 at 9:08 am
Muhammad: is your layout table-based? If so, you’ll need to adjust the height on the TDs before the actual watermark div. See: http://www.andypemberton.com/sandbox/watermark/styles/watermark-print-ie.css