DEV Community

Cover image for How to style for print in Tailwind
Andreas Bergström
Andreas Bergström

Posted on

How to style for print in Tailwind

Ever been merrily coding away when suddenly the thought strikes, "What if someone... prints this page?" And the more ominous: "What if it looks like a Picasso painting after a rough night when they do?"

For a web app that's more of an interface than a classic document, you probably don't care that much about its print appearance as it's unlikely someone would print something there. But for a typical document-oriented website with lot's of text and image content, you might wanna have a look at its print preview. Looks terrible, right? How do we fix this without resorting to the hell of custom CSS classes and files? Like this?

@media print {
  a {
    color: inherit;
    text-decoration: none;
  }
}
Enter fullscreen mode Exit fullscreen mode

Well, no! With Tailwind's ever-so-handy print and screen modifiers, your website can look fabulous on screens AND on paper. No more nightmares about confetti-like printouts!

Setting up print and screen prefixes in Tailwind

Tailwind is the awsome utility-first CSS framework we wish we could use in every client or work project. It's like the Swiss Army knife of styling - just without the tiny, almost useless, scissors. Most utilities are activated right from the get-go. But some? They're like hidden Easter eggs waiting for you to find. Enter: the print and screen modifiers.

From the get-go it's not available, but luckily you don't need to install any plugin to get started. Simply open up the tailwind.config.js file and extend the available screens:

module.exports = {
  // ... rest of tailwind config
  extend: {
    screens: {
      print: { raw: 'print' },
      screen: { raw: 'screen' },
    },
  },
}
Enter fullscreen mode Exit fullscreen mode

This way, Tailwind will now auto-generate media queries prefixes that you can use in your classes. You can use them to entirely show or hide elements depending on print or not, or tweak any CSS you want accordingly:

<div class="print:text-xl screen:text-sm">I’ll look big on paper but compact on screen!</div>
Enter fullscreen mode Exit fullscreen mode

Let's go through a few other examples of where you would want to tweak the print styling on your page.

Hide navigation and footer elements

When a user prints a webpage, they typically don't need the website's navigation bar, footer, or any site-wide banner elements. These are essentially useful only in a digital context. Therefore, it's a common practice to hide these elements in the print version.

<nav class="print:hidden"> ... </nav>
Enter fullscreen mode Exit fullscreen mode

Hide interactive elements

Interactive elements, such as buttons, dropdowns, and forms, serve no purpose on a printed page. It's a good idea to hide them to avoid confusing the reader.

<button class="print:hidden">Click Me!</button>
Enter fullscreen mode Exit fullscreen mode

Display URLs for links

Hyperlinks are interactive elements that work wonders on screens but are dormant on paper. To provide context, it can be helpful to display the URL next to the link text in the print version so readers can manually access them if needed.

For this one we do need to add a custom CSS class to our global CSS, but only one!

@media print {
  a[href]:after {
    content: " (" attr(href) ")";
  }
}
Enter fullscreen mode Exit fullscreen mode

Optimize images and graphics

High-color images might not always print well, especially in grayscale. You might want to enhance the contrast, remove background images, or even swap colored images with their grayscale counterparts to ensure clarity.

<img src="colored-image.jpg" class="screen:block print:hidden">
<img src="grayscale-image.jpg" class="screen:hidden print:block">
Enter fullscreen mode Exit fullscreen mode

Considering the distinct nature of screens and printed pages, these tweaks can significantly improve the clarity, relevance, and aesthetics of printed content.

Top comments (0)