Print-friendly pages with CSS

Printing often seems to be the bane of web publishing doesn’t it? All this information scattered across the ‘net, and so many times, trying to get a decent hardcopy printout just results in frustration. Sites that are beautiful in a browser may fail when printed — text disappears into background colors if you’re printing black and white, ads take up tons of unnecessary space on the printed page, and if the site uses tables or a fixed-width CSS layout that’s wider than the page, you may end up with having text cut off along one side of the page. Many commercial sites create special ‘print friendly’ pages, but that’s something of a kludge in itself — it’s an extra click, sometimes the link to the ‘print-friendly’ page is difficult to see, and it means having to deal with that many extra pages (and extra bandwidth) to maintain and serve.

Thankfully, CSS allows a remarkably simple way to avoid having to deal with all these frustrations. With a little bit of coding time, you can create a special “print stylesheet” that will determine exactly how your page looks when it’s printed out, without ever having to deal with the problems outlined above. Here at TypePad, you’ll need to be a “Pro” user to take advantage of this (as you’ll need access to the code of your templates), but the same technique will also work for MovableType powered sites, or any other website where you have full access to your code.

Need a quick example? Just try printing this post out, then compare what your printer gives you to the screen version (Netscape, Mozilla, Firebird, and Safari users will get an extra bonus trick that IE doesn’t support). For all the gory details (which really aren’t all that gory), just read on…

This site without print CSS

For a quick example, I’ll hold myself up to the spotlight (hey, if I’ve going to point out the problems, might as well humiliate myself instead of someone else, right?). The image to the right shows the result of printing this site after my redesign — those nice big white borders that work so well on screen cause definite issues on the printed page, squishing all the actual content into a single really tiny column in the middle of the page. This obviously was not acceptable! So, I got to work.

The first step was to design the print stylesheet itself. The basics of that are essentially the same as designing an on-screen stylesheet, and you can even use a browser to work it all out. I started by copying my stylesheet into a blank text document on my computer and tweaking it piece by piece to adjust it for the printed page. I won’t go through all the changes I made (though you’re welcome to compare the two to see for yourself — feel free to look at the code for either my screen stylesheet or my print stylesheet), but I do want to call attention to a few of the methods I used.

First off, the borders — an easy fix there, all I had to do was adjust the margins for the #content div from 150 pixels to 5%. By using a percentage instead of a fixed value, it ensures that the content area of the printed page will always be 90 percent of the available width, no matter if the person printing the page is in the US or in Europe (as there are different standard paper sizes).

Now for a handier trick. One of the major differences between on-screen display and the printed page are that navigation elements, such as the navigation sidebar across the top of each page on my site, are essentially useless when printed. Obviously, they can’t be clicked, so all they really end up doing is taking up space on the printed page. If that’s the case, then why bother printing them at all?

In my original stylesheet, my navigation bar is contained within a div of its own, like so:

#navigation {
   text-align: center;
   margin-top: 2px;
   padding: 4px;
   background-color: #eee;
   }

So, for the print stylesheet, I simply changed all of that to this simple argument:

#navigation {
   display: none;
   }

And voila! When printed, that entire div just disappears from the page. That little display: none; argument comes in very handy for deciding just what appears and doesn’t appear on your printed page and on screen. I added a class called “screenonly” to my print stylesheet that uses the display: none; argument, and then wrapped the comment entry form on my individual entry pages in a div class="screenonly" tag — bingo, no printed comment entry form. With a little experimentation, it’s very easy to determine exactly what elements on your pages will and won’t print.

The same technique can be used on screen, of course. I added another class to my screen stylesheet called “printonly” that also uses the display: none; property, then added a copyright declaration to the bottom of every page that is wrapped in a div class="printonly". Now that won’t display onscreen, but will appear when printed. Pretty nifty, isn’t it?

There’s one last trick I want to call attention to before I close out, though — while it will only work in browsers that have good CSS support (in other words, almost every browser except Internet Explorer), it’s incredibly useful.

Links are somewhat of a quandry on the printed page — in fact, by default, they’re nearly useless. A slight color change in the text, but nothing else. However, CSS includes ways to work around this, as well. I added the following lines to my print stylesheet:

.postbody a:link:after, .postbody a:visited:after {
   content: " (" attr(href) ") ";
   }

This looks a little complex, but it’s really not all that scary. The first line says simply that within any element that has the class postbody (I’ve wrapped the text of my posts, trackback excerpts, and comments in divs with that class), any a element that is either a link or a link that has been visited is going to have some information added after that element.

The second line is the content that we’re adding: first we add a space and an opening parenthesis, then we add the href attribute (in other words, the URL that the link points to), then we add a closing parenthesis and another space. That’s it — the last line is simply closing the CSS argument.

So what does all that mean? Simply this — when the page is printed, every link will be followed with the target URL of that link in parenthesis. Nifty!

Okay, so those are some of the tricks I’ve used. Now, supposing you’ve done some poking, prodding, and experimentation on your own and you’ve got a print stylesheet all set up and ready to go — how do you get it working? Easy enough! First off, create a new Index Template for your site, name it something like “Stylesheet-print”, and have it output to “print.css”. Now, you’ll need to do a slight edit to each of your templates.

The default line in TypePad templates that calls the stylesheet is in the header information at the top, and looks like this:

<link rel="stylesheet" href="<$MTBlogURL$>styles.css" type="text/css" />

We’ll be adding one argument to that line, and then adding a second, very similar line –here’s what you should end up with:

<link rel="stylesheet" media="screen" href="<$MTBlogURL$>styles.css type="text/css" />
<link rel="stylesheet" media="print" href="<$MTBlogURL$>print.css" type="text/css" />

What we’ve just done is tell the browser that the “styles.css” file should only apply to on-screen display, while the “print.css” file should apply to content that is sent to the printer. That’s it — rebuild, and you’re done!

My site with the print stylesheet

And here we have the final result — the image to the left is how my site prints out with the print stylesheet added. The useless navigation bar has disappeared, borders have been set to values that are far more manageable and readable for the printed page, I’ve brought the font size down a touch, and all links now display their target after the link itself. All in all, a far more readable printed page than what I started with, and all accomplished with a little bit of playing with CSS.

So that’s it! Hopefully this all made sense and is useful for you. As always, feel free to leave any comments or questions below, and I’ll do my best to clarify any places where I may have been unclear. Enjoy!

(Post-link URL display code was found at A List Apart.)

www.michaelhanscom.com

One of the features of the TypePadPro” level that I’ve been looking forward to is domain mapping — the ability to assign a domain name I own with my TypePad weblog. Last night I noticed that domain mapping beta testing was in progress, so I sent a note to let them know that I was interested. Lo and behold, I got my response this morning, made a few clicks to my domain configurations…

…and it worked! Eclecticism is now residing at www.michaelhanscom.com! Even better, the old address of djwudi.typepad.com will still work as a backup, so any links out there pointing my way will still work, without any issues whatsoever.

So, feel free to update your bookmarks to point to www.michaelhanscom.com (or don’t, whatever suits your fancy). I know I’ve been bouncing everyone around in the past few months — from djwudi.com’s ‘The Long Letter’ to djwudi.typepad.com and now to michaelhanscom.com — but this should be the last move for the foreseeable future.

What of djwudi.com, then? Well, I’ll likely leave my DJ Wüdi propaganda over there, as well as having it available for whatever other little projects I want to play with when I’m in the mood to geek out, so it won’t be disappearing. Just in case you were worried. ;)

20 Tips for new G5 owners

This one’s mostly just for me — originally from Ken Tidwell and posted on MacInTouch, but as their archives can be difficult to search, I wanted to keep this around. If all goes well, it might come in handy in a couple weeks. ;)

  1. RAM must be added in pairs only…all models. Opinions vary, but 1gb \~ 2gb RAM seems to be a general minimum before these units really shine. […]

  2. Clean the cast aluminum exterior with iKlear from Apple, or a mild dishwashing detergent.

  3. PCI-X (1.8GHz and 2.0GHz G5s only) supports both PCI-X and PCI… but 3.3 volts only… not 5 V.

  4. The case latch can be used as a locking point for a security cable.

  5. Carbon Copy Cloner is a great tool for moving data from another Mac.

  6. Virtual PC (current version…6?) will not yet run on a G5.

  7. The OS shipping with the G5s at this time is an interim OS, and does not yet support the 64-bit capability of this machine. Give it another 2 to 4 months.

  8. FireWire 800 uses a different connector from FireWire 400 (and Sony’s 4-pin connector).

  9. The Sony DVD writer is not capable of handling DVD+R.

  10. The aluminum case inhibits Bluetooth and Airport signals…don’t forget to mount the external antenna(s).

  11. The G5 will automatically reduce processor speed in response to having the side cover(s) removed. There is also a system preference for selective control. From the G5 Developer Note:

    Fan speed control: The speeds of the fans are thermally controlled and are automatically set as low as possible to minimize noise. System performance can be specified by a “high/reduced/automatic” pulldown option provided in the Energy Saver Control pane in System Preferences.

  12. The rear video connector labeled DVI supports both DVI-I and DVI-D.

  13. The rear digital audio connection is a TOSLINK connector…be sure to buy optical cables that use the square (indexed) connector (not round) on at least one end.

  14. The four screws on the interior bulkhead near the drives are spares that are used when installing a second hard disk. [photo]

  15. The analog audio input on the rear can be used for directly connecting a microphone, which must be self-powered. [Apple’s G5 Developer Note, however, says:

    “The audio inputs are designed to accept high-level audio signals: 2.2 Vrms or +8 dbu, which is the standard output level from CD and DVD players. The output level of some consumer audio devices is lower, often 0.316 Vrms or -10 dbV. Sound recordings made on the Power Mac G5 with such low-level devices have more noise than those made with high-level devices.”

    -MacInTouch]

  16. The power supply is auto-ranging, meaning it will switch between two choices of voltages: 100-125V AC or 200-240V AC.

  17. The USB ports on the computer itself are USB 2.0, while the USB ports on the keyboard are USB 1.1.

  18. G5 owners interested or working with ‘High Throughput Computing’ are advised to check out Condor.

  19. G5’ers curious about 64-bit computing can start here: [Ars Technica 64-bit introduction]

  20. A good demo for friends and family is always a movie…stop by the Apple Quicktime site for latest trailers.

Apples for Alaskans

Looks like high schoolers in Alaska’s Denali Borough School District will be getting Apple PowerBook G4 computers for their school year!

When school begins Sept. 15, the Denali Borough School District will become the first in Alaska to provide a laptop computer for every student in grades 9 through 12. A total of 129 laptop computers are being prepared to hand over to high schoolers at Tri-Valley, Anderson and Cantwell schools.

This “one-on-one initiative” is the result of a partnership between the Denali Borough School District and Apple Computer, and is based on programs in other states, particularly in Virginia and in Maine.

(via MacBytes)

Updated iPods and iMacs

Oh, and just in case you’ve been considering upgrading or switching, Apple announced bigger iPods and faster iMacs this morning. The iMac base models are now \$1299 for a 1Ghz G4, and \$1799 for a 1.25Ghz G4. Not bad at all.

Meanwhile, I’m still waiting for my new G5 to show up — which is okay, since I’m also still scraping together the last little bit of cash I need to finish paying for the fool thing!

Working out the bugs

I’ve been getting some great feedback on the new design, and it’s very appreciated. The kind words on the new look are always flattering, and pointing out areas that are confusing is wonderfully helpful. Things that make sense to me as I’m putting it all together don’t always fly in the real world, and I’m never upset by constructive criticism!

A ‘home’ link has now been added to the navigation bar for all the sub-pages. While I’d had the ‘eclecticism’ title linked back to the home page, it wasn’t terribly obvious, so this should clear up any confusion there. Besides, a little redundancy never hurt.

I’m going to need to do a little tinkering to the display of the comments. I decided to break with convention a bit and put the byline of each comment above the post, rather than below, which seems to be a tad disorienting. Breaking conventions is all well and good — doing so at the cost of usability isn’t. Fixing that is high on the priority list.

Next on the priority list will be adding a bit more space between individual posts on the main page and comments in the comment threads. I’ll need to figure out the best way to do that — because I’m using a display: inline; declaration for the h3 tags to set the border just around the text rather than across the width of the div, simply adding a margin-top: 10px; argument won’t work. I could simply add one or two p or br tags to add some lines of whitespace, but that introduces some unnecessary (purely presentational) code, which I’m trying to avoid, so I’d like to come up with a better solution than that. We’ll see how that goes.

How this page looks in Safari

Right now, the lowest priority is fighting with the skyline image at the top. If those of you that are seeing problems with the display of the image could let me know what browser/version/OS/resolution you’re using, as well as telling me that it’s ‘off-center’, it’d help greatly. I’m using Safari 1.0 on Mac OS X, at 1024×768, and the header looks fine to me. I also checked it in Camino (which should match with Mozilla or Netscape, as they use the same rendering engine), and it was good there. It was only in IE/Mac OS X that I saw any issues (and I haven’t looked into that yet). Unfortunately, my PC is dead at the moment, so I can’t test the site on PC browsers from home, but I’ll certainly be looking into it from work.

Anyway, I’m quite gratified that the design seems to be fairly well received, and that any bugs that have been mentioned so far are actually fairly minor. It’s about time I started exploring different ideas, and you all are helping me iron things out a lot. I’ll buy you a drink next time you’re in town. :)

Feedster IM Searching

Feedster is currently my favorite search engine for finding topical, up-to-the-minute information, and they just released a really nifty new feature today — the Feedbot.

Add ‘feedbot’ to whatever IM client you use, send it a message like ‘find apple imac’, and get the search results IM’d back to you. Very handy!

Validation favelets

Jeffrey Zeldman has posted two ‘favelets’ for easy one-click site validation. Drag them onto your browser bookmark bar, and with one click, you can run any page you’re at through the W3C’s beta page validation service. Very handy.

On a related note, every page of this redesign validated from the get-go. The single issue I ran into was with a link in a story earlier where I’d forgotten to escape the ampersands in the link. The ampersand character — & — should be coded in valid HTML as &amp;, and failure to do so will result in broken validation. Unfortunately, because many database-driven sites use a URL format of http://www.server.com/option1=“sample”&option2=“sample” or some such, you occasionally need to remember to fix the link so that it looks like http://www.server.com/option1=“sample”&amp;option2=“sample”. A minor annoyance, but not insurmountable. Once I got that link fixed, I validated without any further changes needed. Go me!

Well, would'ja look at that?

And here we go, folks — step one of the new design. It’s not completely finished yet, as all I’ve got active at the moment is the primary content, but the rest will follow soon enough.

Update: Okay, I should have thought to check this first, but this redesign has just proven — again — that Internet Explorer sucks. This may or may not get fixed in the future — I’m tempted to just leave it as-is. I’m doing things correctly, dammit, and it’s not my fault that that program doesn’t do what it’s supposed to. Grrr.

Update: That’s it — we’re live. The only page I have yet to dink with is the ‘About’ page, but considering it’s 4:22am, I need to get to bed. All pages linked in the navbar now work, and there are even more choices for RSS feeds available (Full posts with comments, full posts without comments, and excerpts only). I still haven’t looked into the IE wierdnesses, but that will come. Maybe.

Another idea

Another possible redesign

Work continues on reworking the design of this place. I think I’ve finally come up with something that I like, too — miracles never cease!

This time, I took some time to bounce around some of the sites that have caught my eye the most. Generally, they tend to be the exact opposite of what I’ve had here for a long time. Where I’ve had some sort of compulsion to present everything possible at once, all on the front page, I’m far more drawn to very sparse, open, clean designs. Yet, for absolutely no reason that I can come up with, I’d never made an attempt at that style.

So, this is where I think I’m heading at the moment. The navigation bar at the top will take care of all the extraneous crap. ‘About’ will lead to my about page, which will absorb things like my music and reading lists. ‘Past’ takes care of my archives. ‘Comments’ will catch the last n comments made (I’m waffling on this one, actually — it seems a little silly to have a page just for that, but it’s also really nice to have a quick reference of when someone’s said hello). ‘Elsewhere’ will handle both the blogroll and the Destinations feed (just how, I’m not sure yet, but that will come). And lastly, ‘Feeds’ will hold my RSS feeds (which I plan on expanding to include an excerpts feed, a full post feed, and a comment feed).

At least, that’s the basic concept to start with. As always, questions, comments and words of wisdom are greatly appreciated!

Update: A quick list of inspirations, code examples, and other ideas as I work on this. Links may be added here as I go.