views:

156

answers:

6

Hi,

I have used css before and I came across the below css style dont have a clue what it does.

Thanks

a[href^="http:"] {
   background: url(img/keys.gif) no-repeat right top;
}
a[href^="http://mysite.com"], a[href^="http://www.mysite.com"] {
   background-image: none; padding-right:0;
}
+5  A: 

Those are attribute-starts-with selectors, they'll select <a> elements with an href attribute starting with that value, e.g. a[href^="http:"] matches any anchors with an href starting with href="http:....", for example:

<a href="http://www.google.com"&gt;Test&lt;/a&gt; //would match
<a href="#element">Test</a>              //wouldn't match
Nick Craver
so do you mean if there is a css selector say with a[href^=”http://www.google.com”] { color: red;}only the a anchor link which has href="www.google.com" will be in red?
@user443946 - nope, but if both had the `www.` then it would be red, it has to start with *exactly* the same string. Test it here: http://jsfiddle.net/nick_craver/7hnAB/ (make sure you're in a newer browser that supports the selectors).
Nick Craver
Is that using this regular expression? [^ ] (quote from wikipedia follows) Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". As above, literal characters and ranges can be mixed.
orolo
@orolo - It's not a regex, there are only specific cases that work, here's a table of them: http://www.w3.org/TR/css3-selectors/#selectors
Nick Craver
@user443946: That would only match if the tag was `<a href="google.com/somethind" ...`. The selector doesn't check for semantic, it just compares what's inside of the attribute, and if the value starts with the same text as in your selector, it matches.
poke
+4  A: 

For every link which's "href" parameter starts with "http:", set the background to a key image (without repetition, positioned in the top-right corner).

For every link which's "href" parameter starts with "http://mysite.com" or "http://www.mysite.com", set the background image to nothing (and the right-side padding to 0).

To me, this seems like a clever CSS trick that will make your users aware of when they are leaving your website through an external link by displaying a key image.

(I think I'll use it in the future. :)

Lazlo
Exactly this. And it is a *tremendously* cool idea I hadn't thought of before but will probably use everywhere from this point forward.
Stan Rogers
+1 for suggesting the purpose of the styles.
BoltClock
+4  A: 

a[href^="http:"]

Selects an <a> element whose href attribute value begins with http:.

For example:

p[title^="para"] {background: green;}

Will match the following:

<p title="paragraph"> This paragraph should have a green background. </p> 
Russell Dias
+1 for the example. I added an example to my answer too but it's long-winded as heck :P
BoltClock
A: 

The rules say, according to the W3C docs:

  • All anchors which have an href attribute that starts with http:
  • All anchors which have an href attribute that start with http://mysite.com or http://www.mysite.com
thenduks
A: 

It's an attribute selector.
The ^= part means that the href attribute of the anchor tags must begin with http: in your first example.

chigley
+4  A: 

That's known as the attribute-starts-with selector, available in CSS3. It matches links with href attributes whose values start with the given string.

To illustrate, we'll take your example CSS, and add some defaults:

a {
   background: none; padding: 0 1em;
}

a[href^="http:"] {
   background: url(img/keys.gif) no-repeat right top;
}

a[href^="http://mysite.com"], a[href^="http://www.mysite.com"] {
   background-image: none; padding-right:0;
}

And style the following HTML with it. The output styles are summarized in comments:

<ul>
    <!-- [1] No background, 1em left and right padding -->
    <li><a href="/index.php">My site's page</a></li>

    <!-- [2] Background, 1em left and right padding -->
    <li><a href="http://example.com"&gt;External link</a></li>

    <!-- [3] No background, no right padding -->
    <li><a href="http://mysite.com"&gt;My site's base URL without www</a></li>        

    <!-- [4] No background, no right padding -->
    <li><a href="http://www.mysite.com"&gt;My site's base URL with www</a></li>

    <!-- [5] No background, no right padding -->
    <li><a href="http://mysite.com/page.php"&gt;A page in my site with base URL</a></li>
</ul>

What's happening?

  1. Matched only by a
    Its href="/index.php" attribute doesn't start with http: or the other values.

    There is no background, but there is left and right padding.

  2. Matched only by a[href^="http:"]
    Its href="http://example.com" attribute starts with http: but doesn't start with http://mysite.com.

    There is both left and right padding, and a background image.

  3. Matched by a[href^="http:"] and a[href^="http://mysite.com"]
    Its href="http://mysite.com" attribute starts with http: and further starts with http://mysite.com.

    Since the second selector overrules the first selector, the background image and right padding are removed.

  4. Matched by a[href^="http:"] and a[href^="http://www.mysite.com"]
    Its href="http://www.mysite.com" attribute starts with http: and further starts with http://www.mysite.com (notice the www).

    Since the second selector overrules the first selector, the background image and right padding are removed.

  5. Matched by a[href^="http:"] and a[href^="http://mysite.com"]
    Its href="http://mysite.com/page.php" attribute starts with http: and further starts with http://mysite.com.

    Notice that, compared to the third link, the attribute in this one contains more than just the base URL; however, the ^= indicates that the attribute's value just needs to start with your site's base URL, as opposed to = which would mean "select links that only point to http://mysite.com". Therefore, this link is matched by the second selector.

    Since the second selector overrules the first selector, the background image and right padding are removed.

BoltClock
Great explanation, shame I'm the only one who +1ed.
Lazlo
@Lazlo: Thanks for the vote. Nick and Russell beat me to it with much more concise examples, and thus with less time to write their answers :)
BoltClock
+1 long winded answers (but relevant) are what make this site great =)
Russell Dias
@Russell: Awww thanks, you shouldn't have! Also why are you eating a Rubik's cube?
BoltClock
@BoltClock's a Unicorn Why do you look like a unicorn? Anyway, it was one of the first times I tried to solve it and got extremely frustrated, why I decided to eat it is beyond me. Eventually I did get around to solving it, but the time is nothing to boast about =(
Russell Dias
@Russell: Congrats on solving it eventually! As for myself, after attaining 10k rep, I turned into a unicorn to celebrate. I feel like this will wear off soon...
BoltClock
@BoltClock's a Unicorn Of course. I wonder whats in store for me when I hit the rep mark...
Russell Dias
@Russell: You're not alone: some people posted on Meta asking the same question.
BoltClock
@Russell: I am a unicorn no more.
BoltClock