How to create print-friendly webpages with CSS.
Author: Stefan Vervoort | Please Comment!This time, I’ll be explaining the printing possibilities of CSS.
Most people visiting this blog are familiar with what CSS can do. Structuring and modeling webpages. But CSS has more possibilities. You can for example, define which stylesheet works on which device. Those can be referred to by the ‘media’ attribute.
When you use an external CSS stylesheet in your webpages, you use the <style> tag. This tag has more options that only refer to the stylesheet you use on that page. You might not have used the <media> attribute yet, because it is set standard to ’screen’.
So, standard for media is ’screen’, when you visit a webpage you’ll see the screen.css stylesheet. When you click the file>print button, the webpage will be printed in the print.css style.
You can refer to this different stylesheets by the following codes in your tags.
<link rel="stylesheet" type="text/css" href="path/screen.css" media="screen" /> <link rel="stylesheet" type="text/css" href="path/print.css" media="print" />
Is there a problem with this technique?
There isn’t a problem yet, but you should know what to do. As you could have thought of, the only thing that will change is the CSS, not the HTML. The structure of the webpage will stay the same, so are the id’s and classes. You have to define all those id’s and classes in the different CSS files in the same way.
Example:
In your screen.css stylesheet..
#content{ width:300px; height:400px; background-color:#333333; margin:4px; padding:5px; font-size:13px; font-color:#ffffff; }
Your print.css stylesheet..
#content{ width:300px; height:400px; font-size:13px; font-color: #000000; }
As you can see, the general structure remains the same. All the backgrounds are removed in the print.css stylesheet, and text colors have to be readable in a printed version.
You could have, for example, advertisements or links on your webpage. You might not want them to display on the printed page, but you can’t change the HTML of the page. There’s a simple trick for not displaying HTML.
When a div has id ‘ads’, you can make sure nothing is displayed from inside that div, by adding this code in your print.css file.
#ads{ display:none; }
This line will make sure nothing is shown.
One simple tip, if you use links that doesn’t have an underline, only when hovered, make sure you change this in prints. It’s handy for people to see where the link suppose to be.
That’s it! Thanks for reading.










Loading...
3:32 pm on August 18th, 2007
Nice article….thanks a lot…!!!!
6:40 am on August 27th, 2007
Why does everyone hide the name Wordpress on their blogs?
Anyone worth a dime knows you are using Wordpress, so give them some credit will ya!
hint: “credit” // “Powered by WP” in footer
7:26 am on August 27th, 2007
Good post m8. I’d also suggest not confining the width of your div containers and just kind of letting them run free. width:100%;
8:34 am on August 27th, 2007
@Craig W -> you are right, I used too have the standard Wordpress blog description, but I changed that. I’ll add the copyright.
@Patrick -> Thanks, just a detail.
12:25 am on September 12th, 2007
I couldn’t understand some parts of this article How to create print-friendly webpages with CSS., but I guess I just need to check some more resources regarding this, because it sounds interesting.
4:12 pm on May 21st, 2008
Great post helped me out a lot! Thanks!
3:09 am on September 27th, 2008
Pretty handy post. I am just starting to do print stylesheets
5:30 pm on November 4th, 2008
I am interested, but not clear on the function of the ‘media’ attribute when calling a style sheet or style rules.
11:41 pm on November 20th, 2008
This is brilliant I was just searching google for this, thankyou very much!!!
5:52 pm on December 27th, 2008
Great post. Thanks for sharing!I also have an online printing website to recommend. Check out at my website.139