Everything In Between

If your project so much as pretends to have a profit motive, I will tell you to go fuck yourself and your project.

Targetting Relevance in Long Web Pages with the CSS :target Pseudo-Class

5 comments

The Importance of Context and Relevance

Context and relevance are two of the most important aspects of effective communication. If the context of a message is not understood then the commuication will likely be disregarded, or worse—misinterpreted. If the message isn’t relevant to the topic at hand then it will be useless to everybody involved.

In order to ensure that a message is successfully received and has the desired effect, one must first ensure that the context for that message is properly set up and that the message itself is relevant to the subject matter.

An excellent example of a scenario where a loss of context and/or relevance can easily cause communication problems is receiving email messages. Surely everyone has recieved an email that they simply don’t know what to do with or why they’ve gotten it. (By the way, for some unbelievably insightful tips on composing effective email messages, read this article [via 43 Folders].)

The same thing can—and does—happen on the web. For example, let’s say you’re reading a page which has a link in its text to another page. You decide to follow the link but once you’ve arrived at the new page you find yourself staring at an enormous amount of other text that doesn’t have anything to do with why you followed the link in the first place. Suddenly you’re confused, you feel lost, and you quickly push the “Back” button of your browser.

What went wrong here? Context was lost; the destination page had no relevance to the source page. If it did, you couldn’t find it because it wasn’t clearly identified. This is a confusing and frustrating situation, and one that I’m willing to bet happens way too often on the web.

On my own blog, I frequently link to entries I’ve previously written. Sometimes these entries can be quite long, and I’m not always referencing the entire entry in my link. But by pointing the link to a specific “intra-page” anchor by using a fragment identifier and clearly marking the relevance of this information in both the link itself (with a descriptive title) and in the landing page (with what I’ve come to call a relevancy message), context is preserved and confusion is eliminated.

To see what I’m talking about in practice, check out my experiment pages. It’s a demo of the problem and relevancy messages in action.

CSS2 The Rescue

There’s only one problem left. How does one insert text into the landing page which will be viewable when someone follows a specially-crafted link to it, but not when they arrive at the page via any other link? Though it is possible to program this into your pages using PHP or another web scripting language, CSS level 2 offers a much simpler and maintainable solution: the :target and :before pseudo-classes.

When used in combination, the :target and :before pseudo-classes can be used to generate text on a page at a specific point in the document when your page is linked to in a certain way. By using an additional CSS2 function, attr(), we can also generate customized text for each landing spot within the document. Let’s take a look at each component separately.

The HTML You’ll Need

Let’s say you’re writing about apples. In your text, you link to a previous article you wrote about oranges. Your link in the article on apples might look like this:

<a href="oranges.html" title="Oranges have a thick, white pith beneath their skin." >oranges</a>

And your text in the article on oranges might look like this:

<p>[…] The skin of an orange is thick, porous, and varies in color from deep to light orange. There is a bitter-tasting white pith underneath the outter-most layer of the fruit's skin. […]</p>

In order to link directly to the paragraph mentioning the orange’s pith you need to insert a named anchor just after the opening paragraph tag where that content was written. You also need to append this name to the URI of the link, as a fragment identifier. The resulting HTML for the link might look like this:

<a href="oranges.html#skin" title="Oranges have a thick, white pith beneath their skin." >oranges</a>

The resulting HTML for the paragraph about oranges might look like this:

<p><a id="skin"></a>The skin of an orange is thick, porous, and varies in color from deep to light orange. There is a bitter-tasting pith underneath the outter-most layer of the fruit's skin.</p>

The CSS You’ll Need

Once the HTML is in place, you need to create a style rule to generate the text you’ll want to display before the paragraph. Your style sheet might look like this:

a:target::before { content: "Here's what I referenced with my link on the last page: "; font-size: larger; font-weight: bold; }

You can even take it one step further. By adding a title attribute with some helpful text in your named anchor, you can actually display that text (specific to the reference you’re making) instead of the generic “Here’s what I referenced…” text. The CSS for that would look like this, and it would display whatever you write as the title for each named anchor you use:

a:target::before { content: attr(title); }

Limitations

The major drawback to this technique is that you need to change the code of the destination document for it to work. Specifically, you need to insert named anchors (though you can also target any element by giving it an unique id), and you need to insert a style rule. (However, see below for another experiment that will let site owners allow people to get around this limitation.)

Another major drawback is that it requires a fully CSS2-compliant browser. That means Internet Explorer 5 and 6 (both Macintosh and Windows versions) will not show the special content on the destination page. They will, however, move focus to the point in the document at which the anchor resides.

Looking to the Future

I have been playing with this technique recently on my blog (can you find the links I’ve used this on? ;). The obvious benefit is creating a far more seamless transition from one page to another and maintaining the same context across web pages. This is invaluable for me and my readers in particular, since I tend to enjoy writing and end up writing far longer entries than I probably should. (Even though I use headings to divide my entries into logical chunks, it’s still good to have another trick up my sleeve to help usability!)

However, this technique is ripe for expansion. For instance, I have created another experiment to dynamically generate the content of the CSS rule based upon a variable in a GET querystring. (It uses three lines of PHP, and that link itself is a proof-of-concept.) This will allow people to write links that point to my documents and insert their own special relevance-describing blurbs in front of the fragment they’ve linked to. (Yes, I realize this may pose XSS security problems, I’m still just playing with the concept.)

Exploring all of this is a lot of fun. The crux of this technique relies upon accessible and clever hypertext copywriting to enhance usability when linking to web pages and referencing information across pages or, one day, sites. As is the case with most things Web, simple is better.

I’m very eager to hear about comments on this technique. Are there dangers I haven’t seen yet? Can you think of another way to use it that I haven’t mentioned here? Are you already using it on your own site? All feedback welcome. :)

Written by Meitar

March 11th, 2005 at 3:24 pm

5 Responses to 'Targetting Relevance in Long Web Pages with the CSS :target Pseudo-Class'

Subscribe to comments with RSS or TrackBack to 'Targetting Relevance in Long Web Pages with the CSS :target Pseudo-Class'.

  1. This is very good. Overcoming the linearity of reading so that it better matched the free associatiopn of thoughts is a huge undertaking. While hypertext techniques have been around for decades, none is really mimicking free association while maintaining a main thread satisfactorily. It takes EXACTLY the kind of considerations you mentioned. Please develop this a deep as you can go. There are for sure vast unexplored territories in the direction you are going. Not only with techniques, but more so conceptually, philosophically and logically. This is fascinating.

    NIck

    11 Mar 05 at 8:47 PM

  2. So, on those browser which are not fully CSS2 complient your links do not show up at all -

    Nick Moscovitz

    11 Mar 05 at 11:16 PM

  3. The links still work, and the focus still changes to the proper point in the document. In other words, the technique degrades very gracefully.

    However, the embedded or dynamically generated relevancy messages do not show up because of said browsers’ poor support for CSS. There is only way to work around that issue, which is total scripting, but this is a somewhat kludgy solution for what should be so easily maintainable via stylesheets. Possible (for the dynamic code), but not really necessary.

    Meitar

    12 Mar 05 at 5:35 AM

  4. This is near perfect implementation for a site I’m redesigning. Does the Abstraction of URL mean you have to maintain the .htaccess file – I was thinking of simply using folders and subfolders – easier for staff to maintain. However how can I trip the ‘.php’ off the end of the bread crumb trail?

    Ross Geoghegan

    3 Jul 05 at 12:33 PM

  5. Hi Ross. In this entry I was discussing CSS…are you sure this was what you wanted to comment on, rather than my post on breadcrumb navigation with PHP?

    In either event; yes, you do need to maintain the .htaccess file, but if you use any sort of simple CMS mechanism, the mechanism can easily automate this process. WordPress is an example of such a tool that can auotmate the writing of .htaccess files.

    However, if you want to use folders and subfolders, you can do that too. Use a rule such as this in your .htaccess.php
    RewriteEngine On
    RewriteBase /mydirectory/
    RewriteRule ^(.*)\.php$ \1

    Good luck with your redesign project!

    Meitar

    25 Dec 05 at 10:31 AM

Leave a Reply