{"id":469,"date":"2008-01-22T10:09:12","date_gmt":"2008-01-22T17:09:12","guid":{"rendered":"http:\/\/css-tricks.com\/better-email-links-featuring-css-attribute-selectors\/"},"modified":"2008-01-22T10:09:12","modified_gmt":"2008-01-22T17:09:12","slug":"better-email-links-featuring-css-attribute-selectors","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/better-email-links-featuring-css-attribute-selectors\/","title":{"rendered":"Better Email Links: Featuring CSS Attribute Selectors"},"content":{"rendered":"

betteremaillinks.jpg<\/p>\n

What is an CSS Attribute Selector?<\/h3>\n

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:<\/p>\n

h1 { color: blue; }<\/code><\/pre>\n

But you can actually get much more specific and style only header elements that have a title attribute:<\/p>\n

h1[title] { color: blue; }<\/code><\/pre>\n

You can take it even further, and only style header elements that have specific values of attributes:<\/p>\n

h1[title=\"Go Home\"] { color: blue; } \/* Has the exact value \"Go Home\" in the title attribute *\/\r\nh1[title~=\"Go Home\"] { color: red; }  \/* Has the value \"Go Home\" somewhere in the title attribute *\/\r\nh1[title^=\"Go Home\"] {color: green; } \/* Value of the title attribute starts with \"Go Home\" *\/<\/code><\/pre>\n

 <\/p>\n

What is unique about Email Links?<\/h3>\n

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.<\/p>\n

Here is the rub:<\/p>\n

a[href^=\"mailto\"] { color: blue; }<\/code><\/pre>\n

That will target all links with an href<\/strong> that begins with<\/strong> “mailto”.<\/p>\n

 <\/p>\n

Finishing the styling<\/h3>\n

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. <\/p>\n

Let’s get fancy and pull the title attribute from our links and append them after the link itself, on the links hover state:<\/p>\n

a[href^=\"mailto\"]:hover:after { content: attr(title); }<\/code><\/pre>\n

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:<\/p>\n

a[href^=\"mailto\"]:hover:after { content: \" > \" attr(title); }<\/code><\/pre>\n

Beautiful!
\n[VIEW EXAMPLE]<\/a><\/p>\n

 <\/p>\n

Why is this better?<\/h3>\n
    \n
  • 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.<\/li>\n
  • By naming your link something like “Send Email” – you are implying that action<\/li>\n
  • Best of both worlds, now you can name your link “Send Email”, but also show the email address.<\/li>\n
  • 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.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"

    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 […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":5591,"url":"https:\/\/css-tricks.com\/attribute-selectors\/","url_meta":{"origin":469,"position":0},"title":"The Skinny on CSS Attribute\u00a0Selectors","date":"February 13, 2010","format":false,"excerpt":"CSS has the ability to target HTML elements based on any one of their attributes. You probably already know about classes and IDs. Check out this bit of HTML:

    David Walsh<\/h2> This single element has three attributes: ID, class, and rel. To select the element in CSS,\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":336933,"url":"https:\/\/css-tricks.com\/want-to-write-a-hover-effect-with-inline-css-use-css-variables\/","url_meta":{"origin":469,"position":1},"title":"Want to Write a Hover Effect With Inline CSS? Use CSS Variables.","date":"March 26, 2021","format":false,"excerpt":"The other day I was working on a blog where each post has a custom color attached to it for a little dose of personality. The author gets to pick that color in the CMS when they\u2019re writing the post. Just a super-light layer of art direction. To make that\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/05\/button-hover.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":377635,"url":"https:\/\/css-tricks.com\/the-power-of-has-in-css\/","url_meta":{"origin":469,"position":2},"title":"The Power of :has() in CSS","date":"March 29, 2024","format":false,"excerpt":"Hey all you wonderful developers out there! In this post we are going to explore the use of :has() in your next web project. :has() is relatively newish but has gained popularity in the front end community by delivering control over various elements in your UI. Let\u2019s take a look\u2026","rel":"","context":"In "Article"","img":{"alt_text":":has CSS selector","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2024\/03\/has-selector.png?fit=1160%2C856&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":362616,"url":"https:\/\/css-tricks.com\/css-scroll-snap-slide-deck\/","url_meta":{"origin":469,"position":3},"title":"CSS Scroll Snap Slide Deck That Supports Live Coding","date":"February 7, 2022","format":false,"excerpt":"Virtual conferences have changed the game in terms of how a presenter is able to deliver content to an audience. At a live event it\u2019s likely you just have your laptop, but at home, you may have multiple monitors so that you can move around windows and make off-screen changes\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/01\/s_7D85D8A43A72F77C800DD73331D3BC4CD2768EE61D1955657BAA888C80FC51B0_1642040704100_slide-topic.png?fit=1200%2C631&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":157146,"url":"https:\/\/css-tricks.com\/html5-meter-element\/","url_meta":{"origin":469,"position":4},"title":"The HTML5 meter Element","date":"November 26, 2013","format":false,"excerpt":"The following is a guest post by Pankaj Parashar. Pankaj has written here before, last time about the progress element. Fitting indeed then to write again about the very related meter element. They are different both functionally and semantically, so read on! As defined by W3C, The meter element represents\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":363532,"url":"https:\/\/css-tricks.com\/5-accessibility-quick-wins-you-can-implement-today\/","url_meta":{"origin":469,"position":5},"title":"5 Accessibility Quick Wins You Can Implement Today","date":"February 23, 2022","format":false,"excerpt":"Let\u2019s face it: building an AA or AAA-accessible product can be quite daunting. Luckily, having an accessible product isn\u2019t all-or-nothing. Even seemingly small improvements can have nice quality of life benefits for many people. In that spirit, here are five accessibility quick wins you can implement today. Quick Win 1:\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/10\/a11y-icon-pattern.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/469"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=469"}],"version-history":[{"count":0,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/469\/revisions"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}