Click here to Skip to main content
13,096,997 members (43,011 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 9 Oct 2014

CSS: Under-utilized Content Property

, 9 Oct 2014
Rate this:
Please Sign up or sign in to vote.
Create printable links using CSS

#Batman {
background-color: black;
clear: both;
visibility: hidden;
content: “I’m Batman”; }  /* @CSSHumor */



In this post, we’ll discuss about:

  • (The Theory) use and importance of CSS property – content
  • (The Magic) creating printable links using CSS content and pseudo-elements

If you are not interested in ‘The Theory’ which is available in almost all CSS books and web tutorials like, you can start with ‘The Magic’, the feature which motivated me to write this blog post.

CSS ‘content’ Property

The primary use of CSS is to describe the presentation semantics of an HTML document but, we can also use CSS for content generation. With content property, we can inject a piece of information to an existing document. We have to use content property along with pseudo-elements like :before, :after, etc.


I feel the best use of content property is to inject the URL of a hyperlink next to its text content.

   content: "(" attr(href) ")";

This will inject the URL mentioned within the href tag next to each link and will be decorated with a pair of parenthesis. For detailed list of content property values, you may refer to

Create Printable Links

Before we start, let’s have a look at the below paragraph of an HTML page:

“If you want to start learning web technologies, then there is no better place than W3School to start with. For Microsoft technologies, you can get comprehensive help from MSDN. My suggestion is to follow blogs like Technology Talks, Daily Dot Net Tips, Dot Net Tricks, Kunal Chowdhury, SQL Authority etc. ”

The HTML code for this is:


Note that this HTML snippet has certain links to some other places. Now let someone want to print the page containing the above paragraph. He/she will get the print copy as the above paragraph but:

  • What about the links?
  • How can the user get the links for Technology Talks, Daily Dot Net Tips, Dot Net Tricks, Kunal Chowdhary, SQL Authority, etc. from the print copies?
  • Is there any way that we can show links on the print copy but not in the web page?


The easiest solution is to use content property of CSS along with pseudo-element :after. And as we want the URL to be injected next to its corresponding link only in the print page and not in the page that is rendered to the browser, we need to mention media type as ‘print’. So code view of the final document may look like:

<style media="print">
    a:after {
      content: "(" attr(href) ")";
<div>If you want to start learning web technologies then there is no better place than
 <a href="">W3School</a> to start with.For Microsoft technologies
 you can get comprehensive help from 
 <a href="">MSDN</a>.
 My suggestion is to follow blogs like 
 <a href="">
 Technology Talks</a>, 
 <a href="">Daily Dot Net Tips</a>,
 <a href="">Dot Net Tricks</a>, 
 <a href="">
 Kunal Chowdhury</a>, 
 <a href="">SQL Authority</a> etc.</div>

Live Demo

You can view the live demo here. To spot the differences, just view the print preview in Google Chrome or, you can take a printout of it.


Now, we are able to do the magic with which we can create better web pages with links for both kind of users, i.e., online users and offline users (with printed material).

Hopefully, you have enjoyed reading this post.
I’ll love to read feedback from you.
Thanks !


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Suvendu Shekhar Giri
Software Developer
India India
Interested in Microsoft Technologies like C#, ASP.Net, SQL Server and various web technologies like HTML, CSS, Javascript, JQuery etc.

I have a Master’s Degree in Computer Science and also a Microsoft Certified Professional in “Programming in HTML5 with JavaScript and CSS3”.

I always encourage learning and sharing of ideas because I believe “An idea is of no worth if it dies inside you”. Active member in various community sites and activities.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 10 Oct 2014
Article Copyright 2014 by Suvendu Shekhar Giri
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid