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

前端 未结 9 593
渐次进展
渐次进展 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 00:41

    I had this problem today, and used another solution than display: block thanks to the link by asker. This means I am able to retain the link ONLY on the image and not expand it to its container.

    Images are inline, so they have space below them for lower part of letters like "y, j, g". This positions the images at baseline, but you can alter it if you have no TEXT HERE like with a logo. However you still need to mask the text line space and its easy if you use a plain color as background (eg in body or div#wrapper).

    body {
      background-color: #112233; 
    }
    a:hover {
      background-color: red;
    }
    a img {
      border-style: none; /* not need for this solution, but removes borders around images which have a link */
      vertical-align: bottom; /* here */
    }
    a:hover img {
      background-color: #112233; /* MUST match the container background, or you arent masking the hover effect */
    }
    

提交回复
热议问题