0

I have a link (a element) with an image (img element) inside. I'm trying to make the parent element (a element) the same width as its child, which has a width of 75%.

I made the link to display: inline-block, just like this answer says to do, but it's not working. The parent element is still larger than its child. What am I doing wrong, and how can I fix it?

JSFiddle

#img-link {
  display: inline-block;
  background-color: lightblue;
}
#img {
  width: 75%;
}
<a href="#" id="img-link">
  <img src="http://i.imgur.com/VROQLTe.jpg" id="img">
</a>
Community
  • 1
  • 1
Jessica
  • 8,469
  • 12
  • 52
  • 100

5 Answers5

1

You need to set 75% for a and 100% for img

#img-link {
  display: inline-block;
  background-color: lightblue;
  width: 75%;
}
#img {
  width: 100%;
}
<a href="#" id="img-link">
  <img src="http://i.imgur.com/VROQLTe.jpg" id="img">
</a>
Banzay
  • 9,080
  • 2
  • 23
  • 44
0

Remove display: inline-block; from your #img-link rule.

Otherwise the image is going to be 75% of the a block.

dwtm.ts
  • 729
  • 4
  • 16
0

try this one:

#img-link {
    display: inline-block;
    background-color: lightblue;
}

#img {
    width: 100%;
}

DEMO HERE

Ivin Raj
  • 3,308
  • 2
  • 23
  • 55
0

set width 100% and 75% on link

#img-link {
  display: inline-block;
  background-color: lightblue;
  width: 75%;
}
#img {
  width: 100%;
}
Web Dev Guy
  • 1,525
  • 3
  • 13
  • 38
0

There can be multiple solutions:

  1. The first one is to remove display: inline-block; css from the anchor that wraps image. Then, it will resize according to the size of the image.

#img-link {
  /*display: inline-block;*/
  background-color: lightblue;
}
#img {
  width: 75%;
}
<a href="#" id="img-link">
  <img src="http://i.imgur.com/VROQLTe.jpg" id="img">
</a>
  1. Change the width of the image from 75% to 100%
    #img-link {
      display: inline-block;
      background-color: lightblue;
    }
    #img {
      width: 100%;
    }
    <a href="#" id="img-link">
      <img src="http://i.imgur.com/VROQLTe.jpg" id="img">
    </a>
Rahul
  • 463
  • 5
  • 25