Making Links Printable with ¡after and content

Must Read

Mobile Phone Client Applications

The physical properties of all mobile phones as a platform for building client applications apply certain limitations to the...

Twas the Night Before Black Friday

Yes, that special time of year is upon us again. Before you get up the night before to battle...

5 Benefits of Student Loan Consolidation

Are you sick of paying interest on your monthly student loans with no end in sight? Afraid of cash-flow...

Fun Board Games for College Students: The family of Carcassonne

The Carcassonne includes family games aboard the original Carcassonne, Carcassonne: The City Carcassonne: the Castle, The Kids of Carcassonne,...

Refinancing Your Home – Three Reasons to Refinance

Homeowners choose to refinance their home for a variety of reasons. If you are thinking about refinancing your home,...

Affiliate Marketing Guide – How affiliate products to your list of email promotion

In this article you will learn how to promote affiliate products to your email list. You can use this...

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

Digital Marketing for Beginners

Digital marketing for starter, Let to basic learning about connecting with your audience in the right place...

What are 7 things poor people do that the rich don’t?

1. poor people watch TV in which people read books how many hours you spend in front of the TV and when...

Top 18 best small business ideas for beginners starting

A small business can be frightening and requires plenty of careful planning there are many small business ideas which can be beneficial...

Summer that makes you happy

We saw were already here I've been thinking about some of the things. I used to do with my husband even though...

4 Point to helpful tips specifically for caregivers

What you need to take a vacation. I know it sounds impossible creative and try to make it work for you almost...

More Articles Like This