A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > CSS > Style Links Depending on Destination Submit one!

Style Links Depending on Destination

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

a[href="http://google.com"] {
        /* 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 */
}

3 Responses

  1. Richard says:

    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?

    Thanks!

  2. e11world says:

    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!

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.