Quick Tip: Adding a Simple Print Stylesheet to WordPress

Quick Tip: Adding a Simple Print Stylesheet to WordPress

Tutorial Details
  • WordPress Version: 3.1.3
  • Difficulty: Beginner
  • Time Required: 30 Minutes

Adding a print stylesheet to your WordPress site is a wise move. The environment will thank you for applying this article to your website. It’s such a simple feature to implement, and most people just overlook it. I’ll show you how easy it is to add a simple print stylesheet to your WordPress site. I’m going to give you a great foundation of one you can use in your site immediately, and show you a few tips so you can take it to the next level.


Step 1. Implementing a Print Stylesheet

You’ll need to update your theme’s header.php file to let it know that the stylesheet we’ll be updating will be used for printing as well. Open up header.php, look for:

<link rel="stylesheet" type="text/css" media="screen" href="<?php bloginfo( 'stylesheet_url' ); ?>" />

We need to update the media attribute to include print. Screen is telling the browser “This is the stylesheet you should use to know how to display the elements within the browser”. We need to tell it “You can also use this stylesheet when the user is printing”. So now it looks like:

<link rel="stylesheet" type="text/css" media="screen, print" href="<?php bloginfo( 'stylesheet_url' ); ?>" />

Next, open up your stylesheet, style.css. At the end of the document, add:

@import "print.css";

It’s key that the code is at the bottom of the stylesheet to be sure that your screen styles don’t inherit styles we’re using for printing and so that the printing styles won’t be overridden with the styles of your site. After that’s done, create a file in the same directory with the name print.css. This is where we’ll add all our style for printing.

Adding a print stylesheet to WordPress

Step 2. Adding the Print Styling

Below is a modified version of the print style for WordPress’ Default theme “TwentyTen”. A lot of websites share these same elements, so there’s no need to reinvent the wheel with developing one. What I’m going to do is break down the key elements, so you know why they were included and what styles you should be on the look out for to add it to your own custom elements.

@media print {
	body {
		background: #fff none !important;
		color: #000;
	}
	#wrapper {
		clear: both !important;
		display: block !important;
		float: none !important;
		position: relative !important;
	}
	#header {
		border-bottom: 2pt solid #000;
		padding-bottom: 18pt;
	}
	#colophon {
		border-top: 2pt solid #000;
	}
	#site-title,
	#site-description {
		float: none;
		line-height: 1.4em;
		margin: 0;
		padding: 0;
	}
	#site-title {
		font-size: 13pt;
	}
	.entry-content {
		font-size: 14pt;
		line-height: 1.6em;
	}
	.entry-title {
		font-size: 21pt;
	}
	#access,
	#branding img,
	#respond,
	.comment-edit-link,
	.edit-link,
	.navigation,
	.page-link,
	.widget-area {
		display: none !important;
	}
	#container,
	#header,
	#footer {
		margin: 0;
		width: 100%;
	}
	#content,
	.one-column #content {
		margin: 24pt 0 0;
		width: 100%;
	}
	.wp-caption p {
		font-size: 11pt;
	}
	#site-info,
	#site-generator {
		float: none;
		width: auto;
	}
	#colophon {
		width: auto;
	}
	img#wpstats {
		display: none;
	}
	#site-generator a {
		margin: 0;
		padding: 0;
	}
	#entry-author-info {
		border: 1px solid #e7e7e7;
	}
	#main {
		display: inline;
	}
	.home .sticky {
		border: none;
	}
}

Step 3. Understanding the Print Styling

Wrap Your Tags for Print

First, you should notice how all your print styles are wrapped inside:

@media print {
}

All objects that correspond to the print functionality are styled between these brackets. Should you add any style, be sure it’s within these brackets. If it’s outside, CSS won’t know any better and will assume it’s part of the screen media.

Make Everything Black and White

	body {
		background: #fff none !important;
		color: #000;
	}

This is one of the key elements to creating a print style. Typically, anything print friendly should not have a background to it, and all coloring should be black and white.

Block Your Elements and Make Sure They Don’t Overlap

	#wrapper {
		clear: both !important;
		display: block !important;
		float: none !important;
		position: relative !important;
	}

These are basic styles that are making sure everything shows up one on top of the other. Clearing it will make sure everything in #wrapper will fall under the previous element, and anything after it will fall after the wrapper so that things don’t get shoved beside one another. It shouldn’t have a float and the position should remain relative. What this will do is make sure the content reads left to right, and that no objects will overlap one another; it will be one flowing document.

Use Borders and Spacing for Separation

	#header {
		border-bottom: 2pt solid #000;
		padding-bottom: 18pt;
	}

You’ll notice a few properties that actually style the element throughout the print stylesheet. These are absolutely acceptable. A lot of times, color will separate elements in a design. Since everything is in black and white, it is hard to do that. So things like adding a black border to the bottom of an element, and adding some space underneath it will help clearly separate them.

Keep Font and Line Size in Mind

	.entry-content {
		font-size: 14pt;
		line-height: 1.6em;
	}
	.entry-title {
		font-size: 21pt;
	}

It’s a great idea to consider the text size of the content you’re printing. The size you see on the screen compared to what eyes are able to read are very different. Consider a smaller font size, 14 pt is plenty I feel. I may consider even going smaller than that. Having a line height that helps the content flow is also very helpful. Be sure to give some space between your lines so that the printed text isn’t illegible. Also, giving titles a larger font size will help to distinguish them from the content.

Hide Unnecessary Elements

	#access,
	#branding img,
	#respond,
	.comment-edit-link,
	.edit-link,
	.navigation,
	.page-link,
	.widget-area {
		display: none !important;
	}

This will be one of your most used element attributes. ‘display:none’ is going to hide the element entirely. The printed page will treat it like it wasn’t even there to begin with, so if you hide all your images it won’t show a blank spot where they would be normally; the element will be taken completely away from the layout and the next available content will render. I use this a lot for unnecessary elements to include in my print like images, navigation, and sidebars.

Take the Full Page Width

	#container,
	#header,
	#footer {
		margin: 0;
		width: 100%;
	}

This element is making sure that all your elements that wrap the content of the page have a width of 100% so that they’ll take up the entire available margin on the printed document. Sometimes designers will set a width of 900px or the likes there of, which would only allow print to happen in a 900px area.

Elements are Allowed Different Emphasis in Print

	#content,
	.one-column #content {
		margin: 24pt 0 0;
		width: 100%;
	}

In the screen stylesheet, this would be the left chunk of content. It would have a sidebar to its right. You’ll notice now that it no longer has a width limited to that space, it will take up the full available margin on the printed document. It has a width of 100% and the sidebar will no longer be able to fit beside it. In fact, the sidebar should have a display:none somewhere in the print stylesheet. There’s also a margin attribute that will separate it from the content around it so that it will stand out.

Find Out Which Images You Can Remove

	img#wpstats {
		display: none;
	}

This is a great example of hiding images that have the ID of “wpstats”. You can create your own class called hideInPrint to add to images that you want to remove when printing. The code for that would look like:

	img.hideInPrint {
		display: none;
	}

Conclusion

This article is just a foundation – you can obviously add your own unique brand styles to this to create a customized print style. Here, we’ve shown you the elements you should keep in mind when implementing a print stylesheet into your own project; The rest of the styling will be up to you in your own project! Knowing the thought behind “why” certain attributes were used, will help your insight as to what your project will need.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Mark

    Cool, I’ve always wanted to see how this was done.

    thanks,

    Mark

  • http://www.rv-designs.nl Rv-Designs

    Thanks for this well explaned tutorial.

  • Connor Crosby

    When I make a print.css for my sites, I include a code that shows URLs to any links in content. I forget where I saw that, but I think it’s really cool.

  • http://www.derby-webdesign.co.uk Kevin

    I’ve never really bothered with print CSS files before but I may have to make a start using them. Thanks for the information.

  • http://blog.kamarkecil.com Arif Riyanto

    Wow, it’s cool, I’ll try this one. Thanks

  • Friend

    Is there a way to preview your webpage in a browser with print.css? This would be useful while editing print.css.

    • http://rockingthepjs.com/ Trisha Cupra

      Yes, you can, if you use Firefox with the Firebug and Web Developer add-ons.

      Once you have Web Developer installed, right-click on the web page you’re working on.

      Web Developer > CSS > Display CSS by Media Type > Print

      The web page appears the way it will look when printed out.

      Then, right-click again and do this:

      Web Developer > Options > Persist Features

      This will allow you to refresh the page without losing the effect of the Print CSS.

      Now, with Firebug installed, you can right-click and select ‘Inspect Element’

      This will show you the CSS that is being applied to the page, and allow you to modify it and preview how it would look with different styles. Then, copy and paste your modifications to your stylesheet (Firebug even tells you the line numbers of the elements in your CSS file/s.)

    • Mark

      Go print then preview!

  • http://wandpapier.de ralf_donald

    Thanks for the nice tutorial. :)

  • Sly

    For us complete newbies out there: What the hell is a Print Style Sheet and what is it supposed to do?

    • http://www.sigaspot.com/ SiGa

      There´s the CSS-file which will define how your site looks on the screen. If you don´t want your ads, header, sidebar or big pics to be added when someone tries to print a page of your site, you can provide a print-stylesheet as described here. And maybe people will love you for doing so… ;-)

  • Pingback: holyswisscheese / Snapshot: Useful posts on Wordpress, July

  • http://www.wjzhb.com wjzhb

    use this can do more work beyond this.

  • Pingback: 40+ Best WordPress Tutorials of July 2011 - Wordpress Tutorials

  • Pingback: WordPress: Useful links, July / Holy Swiss Cheese

  • Pingback: Best WordPress Tips and Tutorials | Designs Showcase

  • http://8mediaeg.com 8MEDIA

    amazing post ,, thanks for sharing information

  • Jacqui

    This is not so easy with a very complex theme. Isn’t there a better way to do this like including just the header, post image and the content?

    Looking through over 6,000 lines of CSS to see what needs to be ex-cluded with display:none could be incredibly complex and time consuming.

  • http://purpledirectory.co.za rene

    This is a directory site – how can I print all my suppliers (posts) into one document (pdf/word/xml) for offline distribution? I know hardly anything about coding so basic instruction is required.
    Thanks

  • Pingback: Best Wordpress Tutorials Which Will Make Your Life Easy - Webkia