Code Snippets
/

Print Stylesheet Recipe

Print Stylesheet Recipe

When users print a page, they want a clean, readable document, not screenshots of your nav bar. A small `@media print` block can hide chrome, expand link URLs inline, force black-on-white text, and control page sizing and breaks. Drop these recipes into any project that publishes long-form content (articles, receipts, invoices, recipes).

CSS
Easy
2 snippets
css-media-queries
css-fonts-typography
css-display

712 views

10

/* Everything inside @media print only applies when the user prints (or saves
   as PDF). Browsers also use this when generating reader-mode print views. */
@media print {
    /* Hide site chrome that does not belong in a printed document. */
    nav,
    header.site,
    footer.site,
    aside,
    .ad,
    .cookie-banner,
    .skip-link,
    .pagination,
    .share-buttons {
        display: none !important;
    }

    /* Force readable contrast. Many users still print on monochrome printers,
       and even on color printers, dark backgrounds waste ink. */
    html,
    body {
        background: white !important;
        color: black !important;
    }

    /* Use a body font that prints well. Serif faces tend to render crisper at
       300 DPI than sans-serif because they were designed for print. */
    body {
        font-family: Georgia, 'Times New Roman', serif;
        font-size: 12pt;
        line-height: 1.4;
    }

    /* Expand link URLs inline so a printed page is not full of "click here". */
    a[href]:not([href^="#"]):not([href^="javascript:"])::after {
        content: " (" attr(href) ")";
        font-size: 0.85em;
        color: #444;
    }

    /* Article images should not break across pages. */
    img,
    figure,
    table {
        page-break-inside: avoid;
        break-inside: avoid;
    }
}

The first thing every print stylesheet does is hide the parts of the page that only make sense on screen: navigation, ads, banners, share buttons. Use display: none !important because authors of those components often have specificity high enough to win without the bang. The second job is contrast: many printers are monochrome, and dark mode pages look terrible printed, so force white background and black text. The link-expansion trick (a[href]::after { content: " (" attr(href) ")"; }) prints the URL right after the link text, so the printed copy is still useful as a reference; exclude in-page anchors and javascript: links so you do not get noise. page-break-inside: avoid keeps figures from being cut in half across pages.