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
  • 9,379
  • 14
  • 65
  • 136

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,310
  • 2
  • 27
  • 46
0

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

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

tosc
  • 759
  • 4
  • 16
0

try this one:

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

#img {
    width: 100%;
}

DEMO HERE

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
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,693
  • 3
  • 18
  • 42
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
  • 493
  • 6
  • 27