CSS for Print

TLDR =>

To get a good looking print style, just do these two things:

  1. Make sure you have “print” listed as a value within your MEDIA property on the link element in the head of your site.
  2. Add a @media print {} query and add some styles.
  3. Bonus: Don't reinvent the wheel! Start with the boilerplate listed at the end of this article.

The Story

So I had my print stylesheet all dolled up and ready to go to production. But wait.

Of course it wasn’t going to work straight away. I mean why would it. Muttering curses to myself I fired up Google. Down the familliar rabbit hole I go.

After growing frustrated with the complexity of many of the blog posts I found I eventually had the mind to go straight to the @media spec that actually drove this thing to see if there was something of the spec I was missing.

Yes the good ol’ W3C is where I would find my answer to the question: why isn’t my print media query actually doing anything?

I saw this and it caught my eye: http://www.w3.org/TR/CSS2/media.html

<!DOCTYPE html>  
<HTML>  
   <HEAD>
     <TITLE>Link to a target medium</TITLE>
       <LINK REL="stylesheet" TYPE="text/css" MEDIA="print, handheld" HREF="foo.css">
    </HEAD>
    <BODY>
      <P>The body...</P>
    </BODY>
   </HEAD>
</HTML>  

That “Media” property. I bet I don’t have “print” a value in there.

And I didn't. Head on table.

# Enter HTML5Boilerlate Print Styles

I didn't write mine from scratch. Why reinvent the wheel right? I lifted the CSS from HTML5Boilerplate.

Then I added display:none to the elements and classes unique to my site that I also wanted hidden from the print view.

@media print,
       (-webkit-min-device-pixel-ratio: 1.25),
       (min-resolution: 1.25dppx),
       (min-resolution: 120dpi) {
    /* Style adjustments for high resolution devices */
}

/* ==========================================================================
   Print styles.
   Inlined to avoid the additional HTTP request:
   http://www.phpied.com/delay-loading-your-print-css/
   ========================================================================== */

@media print {
    *,
    *:before,
    *:after,
    *:first-letter,
    *:first-line {
        background: transparent !important;
        color: #000 !important; /* Black prints faster:
                                   http://www.sanbeiji.com/archives/953 */
        box-shadow: none !important;
        text-shadow: none !important;
    }

    a,
    a:visited {
        text-decoration: underline;
    }

    a[href]:after {
        content: " (" attr(href) ")";
    }

    abbr[title]:after {
        content: " (" attr(title) ")";
    }

    /*
     * Don't show links that are fragment identifiers,
     * or use the `javascript:` pseudo protocol
     */

    a[href^="#"]:after,
    a[href^="javascript:"]:after {
        content: "";
    }

    pre,
    blockquote {
        border: 1px solid #999;
        page-break-inside: avoid;
    }

    /*
     * Printing Tables:
     * http://css-discuss.incutio.com/wiki/Printing_Tables
     */

    thead {
        display: table-header-group;
    }

    tr,
    img {
        page-break-inside: avoid;
    }

    img {
        max-width: 100% !important;
    }

    p,
    h2,
    h3 {
        orphans: 3;
        widows: 3;
    }

    h2,
    h3 {
        page-break-after: avoid;
    }
}