How to style for print in Tailwind
Andreas Bergström
Posted on August 13, 2023
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;
}
}
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' },
},
},
}
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>
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>
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>
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) ")";
}
}
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">
Considering the distinct nature of screens and printed pages, these tweaks can significantly improve the clarity, relevance, and aesthetics of printed content.
Posted on August 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 5, 2024