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

前端 未结 9 671
暗喜
暗喜 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:41

    I was also having the same problem, but I set the height value in my CSS to auto and that fixed my problem. Also, don't forget to do the display property.

      #image {
          height: auto;
          width: auto;
          max-height: 550px;
          max-width: 1200px;
          margin-left: auto;
          margin-right: auto;
          display: block;
     }  
    

提交回复
热议问题