CSS object-fit: contain; is keeping original image width in layout

后端 未结 2 1262
半阙折子戏
半阙折子戏 2020-11-29 12:35

I\'m trying to make my images responsive inside some flexbox containers using object-fit: contain, and while the images do resize, the layout seems to be keepin

2条回答
  •  攒了一身酷
    2020-11-29 12:48

    You can wrap img tag with a div with width: 100% and set image height to 100% and do that without object-fit:

    html,
    body {
      margin: 0;
      height: 100%;
    }
    
    .page {
      height: 100%;
      display: flex;
    }
    
    .main-container {
      flex: 1 1 0;
      display: flex;
      flex-direction: column;
    }
    
    .half-containers {
      flex: 0 1 50%;
      overflow: auto;
      box-sizing: border-box;
      border: 0.5px solid red;
      display: flex;
    }
    
    .page-header {
      flex: 0 0 auto;
      background-color: #dcdcdc;
    }
    
    .page-footer {
      flex: 0 0 auto;
      background-color: #dcdcdc;
    }
    
    
    .img-container {
      width: 100%;
    }
    
    img {
      height: 100%;
    }

提交回复
热议问题