CSS image scaling to fit within area not distort

后端 未结 6 650
栀梦
栀梦 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:39

    This won't work in IE6 (as required by the OP), but for completeness you can achieve the required effect on newer browsers using CSS3's background-size:cover and setting the image as a centered background image. Like so:

    div { 
      width:150px;
      height:100px;
      background-size:cover; 
      background-position:center center; 
      background-repeat:no-repeat; 
      background-image:url('somepic.jpg');
    }
    

提交回复
热议问题