Complete Entry

Creating a Basic Print Stylesheet

  • Posted at: 4:11 pm
  • Filed in: Design

Tags:

Creating a stylesheet for print is fairly simple to do for anyone who has even a little experience playing with CSS. Most smaller blogs I run across have no printing styles. A print stylesheet is an entirely separate set of styles that are called once someone decides to print your website. If you print out this website you will notice that the top and bottom navigation, as well as the Recent Post, Comments, etc. are not printed. Removing elements such as navigation, banners, large images and backgrounds saves ink. Print-friendly versions also avoid problems with printer reformatting and normally use fewer pages than directly printing a web page.

The following are some ideas of what you to can do with print stylesheets:

  • Add or remove defined page elements such as navigation aids, images, and search boxes
  • Suppress background colors and background images
  • Change font type, size, and measurement.
  • Change text line-height for better readability.
  • Write out URLs for links through generated content. (advanced browsers only)

Let’s take a look at my current print.css
The Basics:
html {
width:100%
}
body {
background: white;
font-size: 12pt;
}
img {
border: 0;
}

These are fairly straightforward styles. Printers don’t print white, so I set the page’s background to be white, made the font size 12pt, and did away with any borders on images. Many people hide all of their images, but I like having my header image print on every page.

Hiding Elements:
#nav, #botnav, #footer, .col, .google {
display: none;
}

These styles hide all of the elements listed when your site is printed. You can add all the elements you want by just separating them with commas in this one style.

Links:
a:link, a:visited {
color: #FF0000;
background: transparent;
font-weight: bold;
text-decoration: underline;
}
.primary a:link:after, #content a:visited:after {
content: ” (” attr(href) “) “;
font-size: 90%;
}
.primary a[href^="/"]:after {
content: ” (http://www.yourdomain.com” attr(href) “) “;
}

These styles underline all links and transforms a value like /yourpost/ into http://www.yourdomain.com/yourpost/.

Now let’s look at the all of our styles.

The entire stylesheet:
/* Basic Structure */
html {
width:100%
}
body {
background: white;
font-size: 12pt;
}
img {
border: 0;
}
/* Hide Various Elements when printing */
.hiddenitem, #hiddensidebar, .morehiddenstuff {
display: none;
}
/* Link Management */
a:link, a:visited {
color: #FF0000;
background: transparent;
font-weight: bold;
text-decoration: underline;
}
.primary a:link:after, #content a:visited:after {
content: ” (” attr(href) “) “;
font-size: 90%;
}
.primary a[href^="/"]:after {
content: ” (http://www.yourdomain.com” attr(href) “) “;
}

Adding the stylesheet to your website:
Once you have created your own personal print stylesheet, save it, and upload it to your server. Next, you will need to place a link to you print stylesheet into the HEAD area of your document simliar to the following link: link rel="stylesheet" type="text/css" href="http://www.yourdomain.com/print.css" media="print"

Alternatively, you could add your print styles into your main stylesheet by adding the following:
@media print{
.yourstyleA {
blah blah;
}
.yourstyleB {
blah blah;
}
}

Be sure that you surround your print styles with brackets. I recommend the first method of just linking to another separate stylesheet, but either should work just fine. Choose which option is best for you.

That is more or less it. You can create a very simple, basic, but effective stylesheet for printing in no time at all, and save your vistors a lot of headaches when printing your site.

Comments

  1. Great tutorial Christopher -

    I had no idea you could transform link values like that.

    Another use of a print style sheet that I’ve used a bunch is to use CSS to customize the page output by using the page-break-before, page-break-after, and page-break-inside properties. It’s useful in report printing when you want different sections to start on a new page.

  2. Oh yeah - here’s a link to more info ’bout that paged media stuff…

  3. [...] Exploding Boy Chris Ware has a great little tutorial on creating print stylesheets. Some of the tips I had not even given thought to yet, like setting HTML to 100%. [...]

  4. Thanks for the fast, clear info. :)

  5. [...] Creating a Basic Print Stylesheet at ExplodingBoy I haven’t done this yet, I have to see the interaction with my blog pages, but I will when I get a chance. [...]

  6. Works great but slight problem with the linking part.

    I use a master style sheet which then links to my other stylesheets, This way i dont have to add links to each page on my site i just add it in the master stylesheet file.

    The master.css looks like this

    @import url(”layout.css”); /*layout css file*/
    @import url(”font_style.css”); /*Use this file to customize your website*/

    now im trying to add the print media but it doesnt seem to work this way.

    @media print(”print.css”); /*Used to define print layout*/

    any ideas or suggestions?

Comments are closed.

Date & Time

You are reading “Creating a Basic Print Stylesheet”, an entry originally published on Monday, September 26, 2005 at 4:11 pm and filed under Design.

Commentary

There are currently 6 Comments on this entry. If you have something to say, feel free to leave a comment or post a trackback from your own site.

You can also follow the discussion through an RSS feed of this entry’s comments.

Related Entries

Recent Entries

Noteworthy Entries

Grab The Feeds