Making Links Printable with ¡after and content

Must Read

Dental Malpractice

Malpractice committed by the dental health care providers is called dental malpractice. It is expected of Dentists and Oral...

Student Loan Consolidation – A Way to Save Money

So you have a great need to use every penny as smart as possible, i.e. to get the biggest...

Government Assisted Consolidation Loans – The Free Federal Program to Consolidate Your Debt and Save

Do you have some federal student loans that are choking your monthly budget? Then the U.S. government may have...

CSS can style existing elements, but it can also inject content into a document. There are a few cases where content generation with CSS makes sense, and the most obvious one is appending the URL of a hyperlink next to the link’s text when a user prints the page. When you’re looking at a document on the screen, you can just hover over a link and see where it goes by looking at the status bar. However, when you look at a printout of a page, you have absolutely no idea where those links go.

AwesomeCo is working up a new page for its forms and policies, and one of the members of the redesign committee insists on printing out a copy of the site each time. He wants to be able to know exactly where all of the links go on the page so that he can determine whether they need to be moved. With just a little bit of CSS, we can add that functionality, and it will work in IE 8, Firefox, Safari, and Chrome. We can use some proprietary JavaScript to make it work in IE 6 and 7.

The page itself has nothing more than a list of links on it right now. Eventually it’ll get put into a template.

<a href=”travel/index.html”>Travel Authorization Form</a>
<a href=”travel/expenses.html”>Travel Reimbursement Form</a>
<a href=”travel/guidelines.html”>Travel Guidelines</a>

If you were to look at that page on a printout, you’d have no idea where those links go. Let’s fix that.


When we add a style sheet to a page, we can specify the media type that the styles apply to. Most of the time, we use the screen type. However, we can use the print type to define a style sheet that loads only when the page is printed (or when the user uses the print preview  function).

<link rel=”stylesheet” href=”print.css” type=”text/css” media=”print”>
We can then create a print.css style sheet file with this simple rule:
Down! oad css3_prmt_lmks/pnnt.css
a:after {
content: ” (” attr(href) “) “;

This takes every link on the page and adds the value of the href value inside parentheses after the link’s text. When you print it from a modern browser, it looks just like this:

If you want to see it in action without actually using up paper, you can use your browser’s Print Preview feature, which also triggers this style sheet.

That handles everything except for Internet Explorer 6 and 7. Let’s fix that, shall we?

Falling Back

Internet Explorer has a couple of JavaScript events that I wish every browser would adopt: onbeforeprint and onafterprint. Using those events, we can modify the hyperlink text when the printing is triggered and then revert the links when printing is finished. Our users will never notice the difference.

We just need to create a file called print.js and add this code:

Une l $ (function () {
if (window.onbeforeprint !== undefined) {
window.onbeforeprint = ShowLinks;
window, onafterprint = HideLinks;
5 }
} ) ;
function ShowLinks() {
$(“a”) .each(function() {
io $(this) .data(“7in/c7~ext” , $(this) .textO) ;
$(this) .append(” (” + $(this) .attr(‘7iref”) + “)”);
15 function HideLinksO {
$(“a”) .each(function() {
$(this) .text($(this) .data(“7in/cText”)) ;

Then we just need to attach it to our page. We only need this fallback for IE 6 and 7, so we’ll use a conditional comment for that. This code relies on jQuery, so we have to make sure that we link in the jQuery library as well.

<script charset= “utf-8″ src=’http://ajax.googleapis.eom/ajax/libs/jquery/l.4.2/jquery.min.js’
type=’text/j avascri pt’>
<! — [ i f lte IE 7]>
<script type=”text/javascript” src=”print.js”x/script>
<hl>Forms and Policies</hl>
<a href=”travel/index.html”>Travel Authorization Form</a>
<a href=”travel/expenses.html”>Travel Reimbursement Form</a>

<a href=”travel/guide”Hnes.html”>Travel Guidelines</a>
</l i>

With the JavaScript linked, the link URLs will print on all of our target browsers. You can use this print style sheet as the basis for a more comprehensive one, and you may choose to apply this behavior to only some links on your site, not to every link like we did here.

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This