How to change background-color on text links on hover but not image links

前端 未结 9 590
渐次进展
渐次进展 2020-12-17 00:35

I have a CSS rule like this:

a:hover { background-color: #fff; }

But this results in a bad-looking gap at the bottom on image links, and wh

相关标签:
9条回答
  • 2020-12-17 01:02

    I'm confused at what you are terming "image links"... is that an 'img' tag inside of an anchor? Or are you setting the image in CSS?

    If you're setting the image in CSS, then there is no problem here (since you're already able to target it)... so I must assume you mean:

    <a ...><img src="..." /></a>
    

    To which, I would suggest that you specify a background color on the image... So, assuming the container it's in should be white...

    a:hover { background: SomeColor }
    a:hover img { background-color: #fff; }
    
    0 讨论(0)
  • 2020-12-17 01:03

    The following should work (untested):

    First you

     a:hover { background-color: #fff; }
    

    Then you

    a:imagelink:hover { background-color: inherit; }
    

    The second rule will override the first for <a class="imagelink" etc.> and preserve the background color of the parent.

    I tried to do this without the class="", but I can't find a CSS selector that is the opposite of foo > bar, which styles a bar when it is the child of a foo. You would want to style the foo when it has a child of class bar. You can do that and even fancier things with jQuery, but that may not be desirable as a general technique.

    0 讨论(0)
  • 2020-12-17 01:05

    Untested idea:

    a:hover {background-color: #fff;}
    img:hover { background-color: transparent;}
    
    0 讨论(0)
提交回复
热议问题