tags:

views:

53

answers:

2

I want all .jpg links to have a certain background image. so a link on a page like 'http://mysite/myimage.jpg' would automatically be prefixed with a small icon. Actually all links to images, ie .gif .png as well with the same icon. If the link is to a website, ie .htm/.php/.html/.asp it should have a different image. I want it to be through classes in CSS. Any help appreciated. TIA

A: 

You can accomplish this with CSS selectors + background URL properties. For example, to include an icon with all IMG tags within an Anchor tag:

A IMG { background: url("/icon.png") no-repeat scroll; }
Bosh
I think he means all links that link *to* an image, rather than all links that *contain* an image (but I have been wrong before, so you might be right).
David Thomas
+1  A: 

I think this should work, it's using the CSS3 attribute selectors though, so browser implementation varies wildly:

a[href$='png'],
a[href$='gif'] {/* styles */}

It's basically selecting all links whose href attribute ends with (the $= part) the file-type extension 'png' or 'gif' (obviously other file-types are similarly possible).

Reference, and further details at: http://www.css3.info/preview/attribute-selectors/


Edited:

So, if I wanted to make a special BG image for just youtube links, would I use a[href$='youtube'] {/* styles */}

No, if you wanted it for just YouTube links you could use:

a[href*=youtube] { /* css */ }

The *= is the equivalent of 'contains', though you could use:

a[href^=http://www.youtube.com] { /* css */ }
David Thomas
Thanks for your replies, both. Yes, in fact I want all links that link to an image and this code a[href$='gif'] {/* styles */}worked perfectly. I'd seen it before, but the syntax was wrong and didn't work. So, if I wanted to make a special BG image for just youtube links, would I use<code>a[href$='youtube'] {/* styles */}</code>
Richard
Thanks ricebowl, I followed your references and dug around a bit more a[href^=http://example.com] { color: red; } found on http://css-tricks.com/attribute-selectors/ same as your example above. Thanks so much for your help guys, I just needed to know what to look for. You taught me how to fish! that's worth so much
Richard