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

前端 未结 9 629
暗喜
暗喜 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

    Setting a width of 100% is the full width of the div it's in, not the original full-sized image. There is no way to do that without JavaScript or some other scripting language that can measure the image. If you can have a fixed width or fixed height of the div (like 200px wide) then it shouldn't be too hard to give the image a range to fill. But if you put a 20x20 pixel image in a 200x300 pixel box it will still be distorted.

提交回复
热议问题