Even today, there are still many people that want to print out the entire internet. This can have many reasons. Maybe a team seeks to discuss an article’s content in a meeting. Or maybe somebody wants to read your article somewhere where they don’t have an internet connection. To satisfy these people, each website requires a CSS file for printing.
Even today, many people print plenty of articles to read them offline. If you don’t want to lose readers, you should provide options for these visitors. However, there are two hazards on the way to printing.
First: today, there are barely any WordPress themes that come with a print stylesheet. The theme developers don’t put an effort in that, even though, for me, developing a print CSS is common courtesy. Second: as no print stylesheet is available, the ultimate consumer that uses the theme doesn’t have access to a print button.
Thus, in this article, I’ll show you how to create a print CSS, how it should be integrated into the theme, and how to create a print button.
Pro Tip
Sign up for a free Jotform account to create powerful online forms in minutes — with no coding required.
Creating the Optimal Print Stylesheet
First, create an empty CSS file with a pure text or HTML editor. Name it print.css. Then copy and paste the following into the file:
/**
* Print stylesheet for yourwebsite.com
* @version 1.0
* @lastmodified 16.06.2016
*/
@media print {
Your notes
}
All CSS settings go between the opening and the closing bracket.
1 – Defining Side Borders and Font Sizes
First, we need to define the distances between the side edges, to receive an optimal result.
/* Setting content width, unsetting floats and margins */
/* Attention: the classes and IDs vary from theme to theme. Thus, set own classes here */
#content,#page {
width: 100%;
margin: 0;
float: none;
}
/** Setting margins */
@page { margin: 2cm }
/* Or: */
@page :left {
margin: 1cm;
}
@page :right {
margin: 1cm;
}
/* The first page of a print can be manipulated as well */
@page :first {
margin: 1cm 2cm;
}
I recommend using the above settings, and defining margins to 2 cm. After that’s done, the font size settings can be chosen. Here, you should keep in mind that the printer requires different units for the font size than the monitor. Thus, you need to convert pixels, em, and rem into points.
- Pixels => Points
- 6px => 5pt
- 7px => 5pt
- 8px => 6pt
- 9px => 7pt
- 10px => 8pt
- 11px => 8pt
- 12px => 9pt
- 13px => 10pt
- 14px => 11pt
- 15px => 11pt
- 16px => 12pt
- 17px => 13pt
- 18px => 14pt
- 19px => 14pt
- 20px => 15pt
- 21px => 16pt
- 22px => 17pt
- 23px => 17pt
- 24px => 18pt
A font size of 12pt has proven to be best. Now, you have the choice which font you would like to use for the print. On paper, fonts with serifs, like Georgia, are well readable.
/* Set font to 16px/13pt, set background to white and font to black.*/
/* This saves ink */
body {
font: 13pt Georgia, "Times New Roman", Times, serif;
line-height: 1.3;
background: #fff !important;
color: #000;
}
h1 {
font-size: 24pt;
}
h2, h3, h4 {
font-size: 14pt;
margin-top: 25px;
}
2 – Using Pagebreaks – Defining Pagebreaks
The three CSS attributes page-break-before, page-break-after, and page-break-inside allow you to decide exactly where a print page will be broken. Among other things, this will prevent images from being broken into two pieces.
- page-break-before determines if and how a pagebreak is set before this element.
- page-break-after takes care of breaks behind an element.
- page-break-inside can cause a break within an element, like images or graphics, for instance.
/* The following settings are possible: */
page-break-after : auto | always | avoid | left | right
page-break-before : auto | always | avoid | left | right
page-break-inside : auto | avoid
Auto is the print element’s standard, always places a break every time, avoid prohibits the break, and left, and right are meant for continuation pages that are formatted left or right, accordingly. If applied properly, these rules would look as follows:
/* Defining all page breaks */
a {
page-break-inside:avoid
}
blockquote {
page-break-inside: avoid;
}
h1, h2, h3, h4, h5, h6 { page-break-after:avoid;
page-break-inside:avoid }
img { page-break-inside:avoid;
page-break-after:avoid; }
table, pre { page-break-inside:avoid }
ul, ol, dl { page-break-before:avoid }
3 – The Handling of Links
Links should be highlighted so that they get noticed. As links can not be clicked on a piece of paper, we need to visualize the link targets. This is done with the following notes:
/* Displaying link color and link behaviour */
a:link, a:visited, a {
background: transparent;
color: #520;
font-weight: bold;
text-decoration: underline;
text-align: left;
}
a {
page-break-inside:avoid
}
a[href^=http]:after {
content:" < " attr(href) "> ";
}
$a:after > img {
content: "";
}
article a[href^="#"]:after {
content: "";
}
a:not(:local-link):after {
content:" < " attr(href) "> ";
}
4 – Hiding Videos and Other iframes
Displaying videos on a printed piece of paper doesn’t make sense. However, when setting the iframes on display: none, ugly gaps remain. The following code allows you to remove the gaps and hide iframes, as well as videos entirely.
/**
* Making intergated videos disappear, and removing the iframes' whitespace to zero.
*/
.entry iframe, ins {
display: none;
width: 0 !important;
height: 0 !important;
overflow: hidden !important;
line-height: 0pt !important;
white-space: nowrap;
}
.embed-youtube, .embed-responsive {
position: absolute;
height: 0;
overflow: hidden;
}
5 – Hiding Unnecessary Elements
Many areas of a website can’t be printed. For one, they don’t provide any relevant information, and for another, printing these areas is a waste of ink or toner. Thus, we will hide all areas that are not relevant.
For you, this means that you can dive into your website’s source code to find the areas that you don’t want to be printed. It makes sense to hide the following things:
The header, the navigations, the pagination, the sidebar, the tags, and the categories, the comments, the share buttons, and other elements.
/* Hiding unnecessary elements for the print */
#header-widgets, nav, aside.mashsb-container,
.sidebar, .mashshare-top, .mashshare-bottom,
.content-ads, .make-comment, .author-bio,
.heading, .related-posts, #decomments-form-add-comment,
#breadcrumbs, #footer, .post-byline, .meta-single,
.site-title img, .post-tags, .readability
{
display: none;
}
6 – Adding Messages Before and After Printing
Sometimes, it can be very useful to be able to add messages before and after the actual print content. This type of message allows you to thank your reader for printing your article. Or you can display a copyright message. Once again, it is important to find the proper CSS class of your content area.
/* Adding custom messages before and after the content */
.entry:after {
content: " All Rights Reserved. (c) 2014 - 2016 ";
color: #999 !important;
font-size: 1em;
padding-top: 30px;
}
#header:before {
content: " Thank you for printing our article. We hope that some of our other articles can catch your eye as well.";
color: #777 !important;
font-size: 1em;
padding-top: 30px;
text-align: center !important;
}
The Complete Print Stylesheet
The Right Location: Where does the print.css belong?
The following functions belong into the theme’s functions.php or into a custom site plugin.
“Into the header” would be the correct answer. The following function is the right choice when a developed theme is to be added to the official theme index:
/* The proper function for the addition of the print.css */
function drweb_print_styles(){
wp_enqueue_style(
'drweb-print-style',
get_stylesheet_directory_uri() . '/css/print.css',
array(),
'20130821',
'print' // print styles only
);
}
add_action( 'wp_enqueue_scripts', 'drweb_print_styles' );
If you don’t want to supply your theme with a print stylesheet, the method described above isn’t necessarily optimal. First, the CSS is loaded on all pages, and not on individual articles only, and second, it blocks the page display, while slowing down your website a bit. Thus:
Every CSS That Is Not Required For the Page Display Belongs Into the Footer!
On top of that, it should only be loaded when single.php is accessed with the individual articles. There should barely be anyone that wants to print your landing page.
That’s why we’ll let the stylesheet load in the website’s foot area.
/* The code's modified version only adds the print.css to the footer of single articles */
function drweb_print_styles(){
if( is_single() ) {
wp_enqueue_style(
'drweb-print-style',
get_stylesheet_directory_uri() . '/css/print.css',
array(),
'20130821',
'print' // print styles only
);
}
}
add_action( 'wp_footer', 'drweb_print_styles' );
User-Friendliness: Creating a Print Button
When offering a well-done print stylesheet to your readers, it would be advantageous to also integrate a print button into your theme. The procedure is rather simple, as CSS allows you to design the button the way you want it to be. The code on my website looks like this:
<?php
function ah_print_button() {
?>
<div class="printversion">
<header><span>Print Version</span></header>
<a class="printit" href="javascript:window.print()"> <img src="//pathtoyourgraphic" width="146" height="20" alt="Print Page"> </a>
</div>
<?php }
In the theme, this button can then be called up wherever you want it to appear with a simple <?php ah_print_button();?>
(dpe)
Send Comment:
16 Comments:
More than a year ago
I am trying to use PDF in custom fashion.
When trying to convert epub to PDF, the epub stylesheet specifies
@page {
margin: 0;
padding-top: 1em;
padding-bottom: 1em;
padding-left: 2em;
padding-right: 2em;
}
For some reason that is not being translated properly, because I am only getting padding at the left/right, but none on the top/bottom.
Why is that?
Thank you.
More than a year ago
Oops. I just found that my problem was that the old print.css was stuck in a cache.... I'm getting there. Thanks for your article. Now I have to figure out why I had three copies of print.css showing up at the same time. Perhaps there needs to be a dequeueing function ...
More than a year ago
I've got the link in my footer showing up just fine when I use the Firefox Developer Edition Inspector. My custom print.css (copied from the WordPress codex) appears with all the other css files in the left column when I open the Style Editor in Firefox Developer Edition. But the print.css appears to be completely ignored when I go to print. Just to be sure, I set the color of all h1 - h6 to red. No impact. Any thoughts about how to debug a css file that is enqueued to the wp_footer, but is not acted upon by the browser?
More than a year ago
Thank you.
More than a year ago
This was super-helpful. The additional code for the Print Page button was a nice touch. Thank you!
More than a year ago
I've yet to see @media print and @page do anything on Mac OS Safari, Chrome or Firefox. My impression is that these CSS features are no longer supported?
More than a year ago
Thank you so much for outlining all of the considerations when setting up a print css file. I have a bit of an odd use case that I can't seem to find a solution for. I have a page with two columns comparing the text of two similar long-form texts side-by-side. There are several headings throughout the text that I would like to reappear with "(continued)" after each page break. Is there any way to use the :after pseudo-selector on page breaks?
Thanks again!
More than a year ago
Thank you a lot for sharing this facts. it has helped me a lot to construct inbound links for my website. preserve going.
cheek now
More than a year ago
Thanks for the summary.
One feature does not work:
6 – Adding Messages Before and After Printing
Here is my working Google Blogger printfriendly css to remove right sidebar and scale main post body to A4 paper size.
Scaling is tricky with Blogger themes.
/* START PRINT FRIENDLY */
@media print {
.author-bio, #breadcrumbs, .blog-pager, #decomments-form-add-comment, .feed-links, #footer, .footer, .footer-outer, #header-widgets, nav, .sidebar, #sidebar, #header, .sidebar div div, .content-ads, .make-comment, .heading, .related-posts, .body #navbar, .Navbar, .meta, .main-inner .fauxcolumn-right-outer, .main-inner .column-right-outer, .navigation, .noprint, .post-byline, .meta-single, .post-tags, .readability, .site-title img
{
display: none !important;
}
.main-inner .fauxcolumn-right-outer {
padding-right: 0px;
width: 0px;
}
.main-inner .column-right-outer {
padding-right: 0px;
width: 0px;
}
.main-inner .columns {
padding-right: 0px;
width: 100%;
}
/* PRINT Avoid breaking images in the middle */
p {
page-break-inside: avoid;
}
/* PRINT Inhaltsbreite setzen */
@page {
size:210mm 297mm; /* portrait */
margin: 1.5cm;
}
/* PRINT SAVE INK: Set fontsize, background white and font black.*/
body, .content, .post-outer, html .main-inner .date-outer, .body-fauxcolumns, .content-fauxcolumns {
font: 18pt Georgia, "Times New Roman", Times, serif;
line-height: 1.3;
background: #fff !important;
color: #000 !important;;
}
h1 {
font-size: 24pt;
}
h2, h3, h4 {
font-size: 16pt;
margin-top: 25px;
}
}
/* END PRINT FRIENDLY */
More than a year ago
Too much amazing Content. I also design Custom Printed header Card and it helps me a lot. I remember your guideline and read your blog on a regular basis.
More than a year ago
Excellent, thanks for the fashion sheet and explanation! I even have used a number of themes that embrace this, however my current one doesn't and since i am close to be blogging rather more it'd add up to incorporate this.Graphics & Design
More than a year ago
Thanks!!! :)
More than a year ago
Great information and a timely read for myself as I am about to go into a site redesign and a much needed update. A clean and well laid out print stylesheet will certainly be part of this update. Glad I have this article as a reference. Thank you!
More than a year ago
Excellent, thanks for the stylesheet and explanation! I have used a few themes that include this, but my current one does not and since I'm about to be blogging much more it might make sense to include this.
More than a year ago
A little disappointing to see the image of someones very well laid out CV used as the example for printer style sheets, when in fact when you look at it the example on your blog it's quite far distance away.
More than a year ago
Nice article! This will make my future print stylesheets easier to deal.
But one thing:
What is that $a:after > img selector.
I have never seen that before.
It’s another old IE hack that I don’t know?