0

I have an <img> in an <a> and there is a random additional space at the bottom:

a { 
    text-decoration: none;
    color: inherit;
    display: inline-block;
    position: relative;
    margin: 0;
    padding: 0;
    border: 1px solid red; 
}

img {  
    margin: 0;
    padding: 0;
}
<span><a target="_blank" href="#test"><img border="0" referrerpolicy="origin" src="http://via.placeholder.com/300/E8117F/ffffff" height="300" width="300"></a></span>

The link should exactly wrap the image, but there is a white space at the bottom that shouldn't be there:

Get rid of this...

How do I get rid of this space? I don't want to set overflow: hidden or otherwise clip the content - the height and width of the image can change and I want the <a> to stretch.

Keith
  • 150,284
  • 78
  • 298
  • 434

3 Answers3

2

Set font-size: 0; on anchor

Additionally, in case you have to show some text in anchor as well then wrap it in a span and add font size on that span.

a { 
    text-decoration: none;
    color: inherit;
    display: inline-block;
    position: relative;
    margin: 0;
    padding: 0;
    border: 1px solid red; 
   font-size: 0;
}

img {  
    margin: 0;
    padding: 0;
}
<span><a target="_blank" href="#test"><img border="0" referrerpolicy="origin" src="http://via.placeholder.com/300/E8117F/ffffff" height="300" width="300"></a></span>
Nandita Sharma
  • 13,287
  • 2
  • 22
  • 35
1

Been there, add display:block to your img

img {  
    margin: 0;
    padding: 0;
    display:block;
}

Fiddle: https://jsfiddle.net/hfpwn2a6/

Claire
  • 3,146
  • 6
  • 22
  • 37
-1

a { 
    text-decoration: none;
    color: inherit;
    display: inline-block;
    position: relative;
    margin: 0;
    padding: 0;
    border: 1px solid red; 
}

img {  
    margin: 0;
    padding: 0; display:block
}
<span><a target="_blank" href="#test"><img border="0" referrerpolicy="origin" src="http://via.placeholder.com/300/E8117F/ffffff" height="300" width="300"></a></span>

Here I have just added display block to image. Hope it works for you.

Silu K
  • 238
  • 2
  • 13