Grow your CSS skills. Land your dream job.

Better Email Links: Featuring CSS Attribute Selectors

Published by Chris Coyier

betteremaillinks.jpg

What is an CSS Attribute Selector?

CSS provides a way to style elements based specifically on attributes of the link, rather than the type of element alone. For example, you already know that you can style a header element:

h1 { color: blue; }

But you can actually get much more specific and style only header elements that have a title attribute:

h1[title] { color: blue; }

You can take it even further, and only style header elements that have specific values of attributes:

h1[title="Go Home"] { color: blue; } /* Has the exact value "Go Home" in the title attribute */
h1[title~="Go Home"] { color: red; }  /* Has the value "Go Home" somewhere in the title attribute */
h1[title^="Go Home"] {color: green; } /* Value of the title attribute starts with "Go Home" */

 

What is unique about Email Links?

An email link differs from all other links because it always begins with "mailto:", which is the command the browser uses to notify the operating system that the user wants to send an email to a specific email address with the default email program. We can use this similarity between links to our advantage, so that we can apply CSS styling to only email links.

Here is the rub:

a[href^="mailto"] { color: blue; }

That will target all links with an href that begins with "mailto".

 

Finishing the styling

Now that we have those links targeted, we can do more than just make them blue. We can also make use of another powerful CSS technique, the "content" property. The content property literally allows you to add content to page elements, even content that you have pulled right from it's own attributes! Using a psuedo-selector, you specify where you wanted the content added, before or after.

Let's get fancy and pull the title attribute from our links and append them after the link itself, on the links hover state:

a[href^="mailto"]:hover:after { content: attr(title); }

That will work, but it's going to append that text right after the link, so let's make sure the formatting looks good and add a little spacing and a > character to seperate the link from text that will be added on rollover:

a[href^="mailto"]:hover:after { content: " > " attr(title); }

Beautiful!
[VIEW EXAMPLE]

 

Why is this better?

  • Just listing the email address itself as the link doesn't imply "action". I think it's abrupt when you click a link and it launches your mail client without any warning.
  • By naming your link something like "Send Email" - you are implying that action
  • Best of both worlds, now you can name your link "Send Email", but also show the email address.
  • Forward-Enhancing (and backwords-compatible). This technique will only work in good modern browsers, but it degrades well. Older browsers will just see the link and it will work fine.

Comments

  1. H@med
    Permalink to comment#

    this code have error in IE.

  2. Permalink to comment#

    Such a great tips thanks a lot! I am definitely going to use this technique to warn users when a link goes to an external page or open a new window.

    A shame for ie users but well… if you have a good browser you get the good features isn’t it? ;)

  3. chet
    Permalink to comment#

    Definitely a fail in IE 6 (Win 2K). I see what you mean when I view the example in FF.

  4. atom
    Permalink to comment#

    IE does not currently support attribute selectors.

  5. James
    Permalink to comment#

    This code also fails in IE 7

  6. Thanks guys, I did mention at the end of the article that this is a forward-enhancement technique that only works in good browsers =)

    The cool part is that it doesn’t break in IE, you just don’t get the fun.

  7. The fact that the email still functions correctly is good. I have it in practice now on my current site as most of my visitors thankfully use FF.

    I know this will be frowned upon by most any semantic/standards minded programmers but..

    to hopefully help with spam I opted to use this without supplying my mailto link with a title and instead completely added the email address in the add content css command.

    if youre worried about it not rendering in IE right you could always reverse this and use the link text to say the email address and then add content through css BEFORE the text to say “send email” then it proveds the pleasant transition before clicking, gives a call to action, and kinda has the best of both worlds.

  8. Anon
    Permalink to comment#

    I don’t like using the content attribute. CSS is for style, not content.

  9. I have never seen this used before!
    I like so thanks Chris :)

  10. Rob
    Permalink to comment#

    If you want to make it work in IE you may want to use this JS library:

    http://dean.edwards.name/IE7/

  11. Hey thanks for that link Rob, I REALLY gotta try that, seems pretty brilliant.

  12. Permalink to comment#

    Nice one, thanks !

  13. jtyrrell
    Permalink to comment#

    #8, Style is a broad term all to often believed to be attributed only to static content. The author presents an interesting method to increasing visibility and meaning via the ‘content’ attribute. While the use of ‘content’ typically implies an alteration of data, this approach merely reveals previously defined data (the anchor’s title).

    I won’t argue whether its semantic, but the utility of this approach is what’s most interesting. I would like to see what other creative ways designers implement this.

  14. Attribute selectors are very cool. If IE8 could implement these correctly that would be dandy!

  15. Chad
    Permalink to comment#

    For some reason it does not work in my Firefox 2.

  16. Chad
    Permalink to comment#

    Ok, the extention Better Gmail 2 for Firefox has conflict with this effect. :(

  17. Marc
    Permalink to comment#

    Nice idea, but doesn’t work in my IE

  18. Permalink to comment#

    thanks good resource

  19. These selectors are indeed awesome but definitely more useful in other cases, simply because email links are more recognizable when they look like email links (so “jd@example.com” is a better link name than “send mail”). This hasn’t really changed since 2002 (and earlier days). Showing the email address behind some text (like in your example) looks like an alternative, but here it isn’t really accessible for keyboard users, and trying to copy the email address will reveal yet a specific usability problem.

  20. Nice article. In the TITLE attribute of the link I use “launches your email program” as a hint to the user. I have also used little mail icons as background images placed at the end of the link as another visual clue.

    Personally I HATE it when some link does something unexpected… like launching a PDF.

    Thank you,
    Jim Summer
    Jacksonville Web Design

    http://tentonweb.com/

  21. Permalink to comment#

    Thank you for that link Rob, I will try it in my next project !!

    Holger

  22. Tinker
    Permalink to comment#

    This can be made to work — better — in IE 7 than in Firefox.

    No offense to you FF fans, but I spend MUCH more time trying to get CSS to work predictably in FF than I must spend for any other browser – and again, no offense intended but “Them’s that rule the marketplace gets to set the standards…”

    But it’s still a good trick.

  23. Volkan Görgülü
    Permalink to comment#

    Great Tip/Trick, thanks for posting.

  24. Tephlon
    Permalink to comment#

    @Tinker:

    If you write your CSS to the standards specs, you’ll see that Firefox does exactly what it should do. You are using non-standard CSS that works for IE. It is hard to make cross-browser compatibility work, but I’d rather code for FF than IE.

  25. “I don’t like using the content attribute. CSS is for style, not content.”

    But what’s being revealed in this example is still part of the content – it’s in the HTML as part of the title attribute. The CSS is merely controlling the presentation of what’s already in the HTML. I take your point about CSS being for presentation and not content, but the content property is part of the CSS spec, so why not make use of it when you can?

  26. Permalink to comment#

    I agree with comment #25.

    This sample does not actually look at the content (as is the text between HTML tags), but rather the attribute of the A tag. I believe this to be valid behavior from a CSS point-of-view, just as using the style attribute when applying different CSS code to HTML tags of the same type.

    Indeed, the functionality is there, and since it does prove itself valuable, why not make use of it? I, for one, will start using now that I know that it exist. The only problem is when IE will start supporting it…

  27. Here is another use – for printing. Instead of seeing just the underlined text that is a hyperlink, the print would also display the actual URL of the link, just after the printed hyperlink… works in Firefox, no effect in IE6… have not tested in IE7.

    This would print URLs to hyperlinks within the main content div only, surrounded by « and ».

    #content a:after {
    content:” «” attr(href) “»”;
    }

    Thank you,
    Jim Summer
    Jacksonville Web Design http://tentonweb.com/

  28. Very nice tip there Jim. I REALLY gotta get on a print stylesheet here at CSS-Tricks, I’m gonna try and work on that today (hopefully).

    There is also a comment over on CSS Zone about using JQuery for this too if anyone is interested.

  29. Great tip..Thx :D

  30. chris
    Permalink to comment#

    as already noted IE doesn’t support attribute selectors. the simple workaround is to mark up mailto links as being of a specific class. extra (useful) eye candy by applying a background image of an envelope etc.
    anyway, something much more useful nowadays (most likely not in CSS domain) would be the ability to launch your webmail service of choice to compose the email, instead of starting default system email application. this is so annoying for people with windows, and a lot of these users use webmail. The default on windows is to start outlook express unless you own MSOffice…and outlook express is not even supported by MS…so go figure why it starts by default.

This comment thread is closed. If you have important information to share, you can always contact me.

*May or may not contain any actual "CSS" or "Tricks".