Make an image width 100% of parent div, but not bigger than its own width

前端 未结 9 631
暗喜
暗喜 2020-11-30 19:53

I’m trying to get an image (dynamically placed, with no restrictions on dimensions) to be as wide as its parent div, but only as long as that width isn’t wider than its own

相关标签:
9条回答
  • 2020-11-30 20:42

    You should set the max width and if you want you can also set some padding on one of the sides. In my case the max-width: 100% was good but the image was right next to the end of the screen.

      max-width: 100%;
      padding-right: 30px;
      /*add more paddings if needed*/
    
    0 讨论(0)
  • 2020-11-30 20:43

    In line style - this works for me every time

    <div class="imgWrapper">
        <img src="/theImg.jpg" style="max-width: 100%">
    </div>
    
    0 讨论(0)
  • 2020-11-30 20:48

    If the image is smaller than parent...

    .img_100 {
      width: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题