Style Links Depending on Destination

a[href^="http://"] {
        /* fully valid URL, likely external link */

a[href=""] {
        /* link to specific website */

a[href^="/"], a[href^=".."] {
        /* internal relative link */

a[href^="mailto:"] {
        /* email link */

a[href$=".pdf"] {
        /* PDF file */

a[href$=".doc"] {
        /* Microsoft Word document */

a[href$=".mp3"] {
        /* Music file */

a[href$=".zip"] {
        /* Archive file */


  1. Richard
    Permalink to comment#

    Wow! This is really neat! I’m sure I’ve seen it before but you’ve displayed it in an easy-to-understand way!

    I just have a couple of questions:

    1) I imagine that ^ means “beings with”; does $ mean “ends with” or “includes this text somewhere”?

    2) Full cross-browser support? IE?


  2. e11world
    Permalink to comment#

    This is pretty handy. I’ve been using this method for nearly two years now. I saw a couple of new things here though. Thanks for sharing!

  3. Thao
    Permalink to comment#

    For the “link to specific website”, would declaring cover all subdirectories of (ie:, etc?). Or would they need to be specifically declared?

  4. Kyle
    Permalink to comment#

    This is my first time encountering this CSS trick and I love it. I was at first a little stymied by it as I needed to style links on my site that had certain text in the URL. Just a bunch of links to products that I wanted to be styled similar to the product’s brand. For this I needed to style links going to any products that had the brand’s name in the URL.

    I used a[href*=”/brand-name”] and other variations of that to get it done and I’m loving it.

    Out of curiosity though, what is this called? This cool trick in the brackets?

    Thanks for this awesome post!

  5. Rumman Amin

    How would I use this for relative links that contain a unique user ID (Drupal) ie: /user/1/bookmarks

  6. Sg
    Permalink to comment#


    Thanks for these options. Is there a way to say

    Apply this style if the href does not contain the word “archives”

    Thanks in advance

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.