CSS image scaling to fit within area not distort

后端 未结 6 673
栀梦
栀梦 2020-12-30 20:19

Is there a way with CSS or otherwise of making an image fit within an area. Lets say I have multiple images of different sizes and I want them all to fit within a div of 150

6条回答
  •  情话喂你
    2020-12-30 20:46

    You should try using this:

    img{
      width: auto;
      max-width: 150px;
      height: auto;
      max-height: 100px;
    }
    

    Edit: Looks like IE6 doesn't support max-width and max-height properties. However, you can implement the workaround given here: max-width, max-height for IE6

    Excerpt (in case linked article stops working):

    img {
      max-height: 100px;
      max-width: 100px;
      width: expression(document.body.clientWidth > 150? “150px”: “auto”);
      height: expression(document.body.clientHeight > 100? “100px”: “auto”);
    }
    

提交回复
热议问题