24 ways

to impress your friends

Practical Microformats with hCard by Drew McLellan

You’ve probably heard about microformats over the last few months. You may have even read the easily digestible introduction at Digital Web Magazine, but perhaps you’ve not found time to actually implement much yet. That’s understandable, as it can sometimes be difficult to see exactly what you’re adding by applying a microformat to a page. Sure, you’re semantically enhancing the information you’re marking up, and the Semantic Web is a great idea and all, but what benefit is it right now, today?

Well, the answer to that question is simple: you’re adding lots of information that can be and is being used on the web here and now. The big ongoing battle amongst the big web companies if one of territory over information. Everyone’s grasping for as much data as possible. Some of that information many of us are cautious to give away, but a lot of is happy to be freely available. Of the data you’re giving away, it makes sense to give it as much meaning as possible, thus enabling anyone from your friends and family to the giant search company down the road to make the most of it.

Ok, enough of the waffle, let’s get working.

Introducing hCard

You may have come across hCard. It’s a microformat for describing contact information (or really address book information) from within your HTML. It’s based on the vCard format, which is the format the contacts/address book program on your computer uses. All the usual fields are available – name, address, town, website, email, you name it.

If you’re running Firefox and Greasemonkey (or if you can, just to try this out), install this user script. What it does is look for instances of the hCard microformat in a page, and then add in a link to pass any hCards it finds to a web service which will convert it to a vCard. Take a look at the About the author box at the bottom of this article. It’s a hCard, so you should be able to click the icon the user script inserts and add me to your Outlook contacts or OS X Address Book with just a click.

So microformats are useful after all. Free microformats all round!

Implementing hCard

This is the really easy bit. All the hCard microformat is, is a bunch of predefined class names that you apply to the markup you’ve probably already got around your contact information. Let’s take the example of the About the author box from this article. Here’s how the markup looks without hCard:

<div class="bio">
     <h3>About the author</h3>
     <p>Drew McLellan is a web developer, author and no-good swindler from 
     just outside London, England. At the 
     <a href="http://www.webstandards.org/">Web Standards Project</a> he works 
     on press, strategy and tools. Drew keeps a 
     <a href="http://www.allinthehead.com/">personal weblog</a> covering web 
     development issues and themes.</p>
</div>

This is a really simple example because there’s only two key bits of address book information here:- my name and my website address. Let’s push it a little and say that the Web Standards Project is the organisation I work for – that gives us Name, Company and URL.

To kick off an hCard, you need a containing object with a class of vcard. The div I already have with a class of bio is perfect for this – all it needs to do is contain the rest of the contact information.

The next thing to identify is my name. hCard uses a class of fn (meaning Full Name) to identify a name. As is this case there’s no element surrounding my name, we can just use a span. These changes give us:

<div class="bio vcard">
     <h3>About the author</h3>
     <p><span class="fn">Drew McLellan</span> is a web developer...

The two remaining items are my URL and the organisation I belong to. The class names designated for those are url and org respectively. As both of those items are links in this case, I can apply the classes to those links. So here’s the finished hCard.

<div class="bio vcard">
     <h3>About the author</h3>
     <p><span class="fn">Drew McLellan</span> is a web developer, author and 
     no-good swindler from just outside London, England. 
     At the <a class="org" href="http://www.webstandards.org/">Web Standards Project</a> 
     he works on press, strategy and tools. Drew keeps a 
     <a class="url" href="http://www.allinthehead.com/">personal weblog</a> covering web 
     development issues and themes.</p>
</div>

OK, that was easy. By just applying a few easy class names to the HTML I was already publishing, I’ve implemented an hCard that right now anyone with Greasemonkey can click to add to their address book, that Google and Yahoo! and whoever else can index and work out important things like which websites are associated with my name if they so choose (and boy, will they so choose), and in the future who knows what. In terms of effort, practically nil.

Where next?

So that was a trivial example, but to be honest it doesn’t really get much more complex even with the most pernickety permutations. Because hCard is based on vCard (a mature and well thought-out standard), it’s all tried and tested. Here’s some good next steps.

hCard is just one of an ever-increasing number of microformats. If this tickled your fancy, I suggest subscribing to the microformats site in your RSS reader to keep in touch with new developments.

What’s the take-away?

The take-away is this. They may sound like just more Web 2-point-HoHoHo hype, but microformats are a well thought-out, and easy to implement way of adding greater depth to the information you publish online. They have some nice benefits right away – certainly at geek-level – but in the longer term they become much more significant. We’ve been at this long enough to know that the web has a long, long memory and that what you publish today will likely be around for years. But putting the extra depth of meaning into your documents now you can help guard that they’ll continue to be useful in the future, and not just a bunch of flat ASCII.

About the author

Drew McLellan is a web developer, author and no-good swindler from just outside London, England. At the Web Standards Project he works on press, strategy and tools. Drew keeps a personal weblog covering web development issues and themes.

Your comments

  1. § cboone:

    Nice and simple explanation of what should be — but isn’t always — a nice and simple topic. Thanks Drew.

  2. § Andi:

    Hmm sounds interesting, but how are name clashes between Microformat class names and usual class names for styling avoided? How does the greasemonkey script know the vcard class was intended to be a vcard and not just a short name to style the “Vertical CAR Description” on car manufacturers site (silly example). With more and more Microformats evolving the class namespace becomes more and more cluttered…

    Well I guess this was discussed somewhere else already – I’ll go and have a look. Thanks for the quick introduction!

  3. § trovster:

    Yeh, that’s nice. I’m adding these into a few sites I’m making. But I’m also toying with adding class=”fn” and “org” to general information, without a “vcard” container.

  4. § Si:

    I’ve been aware of Microformats for quite a while now but I never thought it would actually kick off. I was quite wary as to whether they would get implemented much in the future but it seems like they have taken off in a big way now.

    It’s a concept I’m considering toying with on our Intranet – we’ve built an internal contacts directory which could adopt these Standards and make life easier considering the vast majority of employees are using Outlook.

    Cheers for the simple walkthru anyway Drew.

  5. § trovster:

    It’s good that these examples don’t use the strictest form that is shown on the examples of the vCards. The examples on the wiki are not very semantic.

    I’ve expanded on the format myself for recent projects, by using a definition list to contain the information and the address HTML element to markup the address.

  6. § Jules:

    First of all, minor typo: in the second paragraph after “Introducing hCard” you have ”(of if you can, just to try this out)” and I can’t figure out what “of” should be, “or”?

    Secondly, why didn’t you insert the user script in this page so we could try it out? If you have, it doesn’t seem to work in Opera 9 or FF1.5.

    Although the hCard microformat seems to work nicely, I don’t like all of the other microformats, especially the conversation one.

    Nice work Drew on this whole site!

  7. § Tantek:

    Excellent article Drew!

    Andi, take a look at the microformats FAQ.

    Jules, if you have suggestions for improving a microformat for conversations, I encourage you to join the microformats-discuss mailing list and give your feedback.

  8. § Jules:

    @Tantek:

    I went to Microformats.org but couldn’t find the conversation microformat and then I realized that I learned of it from your Podcast at WE05. I don’t think you gave it a “microformat name” at the time and I didn’t see anything resembling it on Microformats.org.

    Has it been “published”?

  9. § remo:

    those microformats dont look very semantic to me – definition lists or such would make more sense than divs and spans

    but its a good idea to define markup standarts

  10. § Jonny:

    There is a Firefox extension to open hCards as vCard files (in Outlook, etc) called Smartzilla

  11. § Drew McLellan:

    Remo – you miss the point. What I’ve done is taken my existing markup and added classes to turn it into an hCard. If I were marking up some contact information for presentation as purely contact information then that might be a different matter. What I had was marked up appropriately for a bio, and been able to enrich that with an hCard.

    Jonny – that sounds really useful. Thanks for posting the link!

  12. § ed:

    I’m probably sounding stupid here, but uhm I don’t understand this Microformats concept at all.

    What I don’t understand is,

    1. what do you do with it? specifically by just adding css classnames as a form of tagging?

    2.How is it used is a real world scenario, anyone has an example to enlighten my blurred view?

    Example: (using square ”[ ]” brackets for pointy ones)

    [div id=”contacts”]

      [span class=”full_name”]

        Joe Raindeer

      [/span]

      [span class=”full_name”]

        Jane Millenium

      [/span]

    [/div]

    Was that microformats??

    Thanks!

  13. § Bogdan:

    @ed: I think your code is not microformat because it does not follow any standard microformat rules, at least not the hCard microformat.

    As far as I’ve understood them, microformats are ways of describing your data using regular (X)HTML and CSS class names. The trick is using the same HTML frame and class names so that machines (programs, scripts etc) could follow them and understand what that specially marked up piece of text actually means.

    So, putting your full name in whatever html tag with the class full_name is not hCard microformat, but using class name fn, is.

    You only need to take a look at microformats.org and understand what are the guidelines for coding every special microformat, then you can use it in your web pages without breaking the layout, standards etc, but preparing your data for the Semantic Web.

  14. § frst:

    one thing missing

    demo!

  15. § Ciro Feitosa:

    Very good. I think that miss a demo. Other article in Portuguese: http://rs.rockgrafia.com/artigo/hcard/

Commenting is closed for this article.

24 ways: day 6