Using print stylesheets with ASP.NET themes

I found a great tip today on a blog called graphical wonder for creating print stylesheets if you’re using Themes in ASP.NET 2.0.

The problem is that all CSS files in the themes directory are automatically included in the of the document as tags. If one of these files is a print stylesheet you don’t get the opportunity to tell the ASP.NET that the media attribute for the file should be print:

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

It turns out there’s a way to actually embed this information into the document itself. You just need to surround the declarations in your print stylesheet with an @media print declaration like this:

  1. @media print {
  2.   h1, h2 {
  3.   color: Black;
  4.   }
  5.  …
  6. }

Posted on 16 Oct 06 by Helen Emerson (last updated on 16 Oct 06).
Filed under ASP.NET

Comments

Tom 03 Oct 2007

This is exactly what I was looking for. One example had codebehind and user controls and all kinds of overhead to allow the print functionality. This is simple and standard, I love it.

Thanks!

Craig 29 Nov 2007

Excellent solution. Thank you!

Jojo 17 Jan 2008

Thank you for showing us this trick

Torst 29 Sep 2008

Thanks. xcly what i was looking for!
…does this work in all modern browsers btw?

Helen 01 Oct 2008

Yeah. @media has been supported by the modern browsers for a while now. Even works in IE6. :)