Previous Message
Next Message

Re: [css-d] Turn of bgcolor for links wich contain an image

Sent by James Bennett on 30 January 2003 02:02


Sasa Velickovic wrote:
> This turns off the bgcolor of the _img_ element. I need to turn of the
> bgcolor for the _a_ element _if_ it contains an img element :) I think this
> is not possible in CSS2 (am I wrong?)

This depends on your content; if you can set a class on those links, 
it's quite easy.  For example, I occasionally use GIFs with transparent 
backgrounds as links (logos and such), and don't want the 
background-color from a hovered link to show up there.  So I simply give 
those links a class I can select and alter with CSS.  For example:

<a class="image" href="http://example.com"><img src="picture.gif" /></a>

and in the CSS:

a.image:hover {
         background-color: transparent !important;
}

The !important is there because I've occasionally run into browser 
quirks or oddities related to the cascade which can cause other rules to 
take a higher precedence; this way the rule is always applied.

And if you don't want to take the time to change all of those link tags 
manually, you can try javascript; using something like 
"document.getElementsByTagName("A")" and then iterating over the 
returned array looking for those links which have an image as a child, 
you could dynamically add the "class" attribute to only that particular 
subset of links.  It wouldn't be too hard and would probably only take a 
few lines of scripting.

-- 
"May the forces of evil become confused on the way to your house."
      -- George Carlin
Previous Message
Next Message

Message thread:

Possibly related: