代码之家  ›  专栏  ›  技术社区  ›  fightstarr20

CSS-匹配高度

  •  0
  • fightstarr20  · 技术社区  · 6 年前

    我有一个简单的flexbox布局像这样。。。

    html, body {
    margin:0;
    padding:0;
    }
    
    .container {
    display:flex;
    flex-direction:row;
    }
    
    .section1 {
    width:50%;
    }
    
    .section2 {
    width:50%;
    }
    
    .img_16x9_holder {
        display: block;
        height: 0;
        padding-bottom: 56.25%;
        overflow: hidden;
    }
    
    img {
    max-width:100%;
    }
    <div class="container">
      <div class="section1">
        <div class="img_16x9_holder">
          <img src="http://lorempixel.com/g/800/800/" alt="800x800image">
        </div>
      <div class="section2">
        <div class="img_matchheight_holder">
          <img src="http://lorempixel.com/g/300/650/" alt="300x650image">
        </div>
      </div>
    </div>

    我正在尝试将左边的图像设置为16x9的比例,然后右手图像应该裁剪和填充以匹配左侧的高度。

    这就是我要达到的目标。。

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   octano    6 年前

    here

    下面是一个例子:

    html, body {
        margin:0;
        padding:0;
    }
    
    .container {
        display:flex;
    
    }
    
    .section img{
        height:100%;
    }
    
    #sec-1 img{
        /*resize image with % or fixed width depending on the image size*/
        width:50%;
    }
    
    #sec-2 img{
        /*resize image with % or fixed width depending on the image size*/
        width:50%;
    }
    <div class="container">
        <div id="sec-1" class="section">
            <img src="http://lorempixel.com/g/800/800/" alt="800x800image">
        </div>
        <div id="sec-2" class="section">
            <img src="http://lorempixel.com/g/300/650/" alt="300x650image">
        </div>
    </div>
        2
  •  0
  •   Temani Afif    6 年前

    html,
    body {
      margin: 0;
      padding: 0;
    }
    
    .container {
      display: flex;
      flex-direction: row;
    }
    
    .section1 {
      width: 50%;
    }
    
    .section2 {
      width: 50%;
    }
    
    .img_16x9_holder {
      display: block;
      height: 0;
      padding-bottom: 56.25%;
      overflow: hidden;
    }
    
    .img_matchheight_holder {
      background-size: cover;
      background-position: center;
      flex-grow:1;
      margin-left:5px;
    }
    
    img {
      max-width: 100%;
    }
    <div class="container">
      <div class="section1">
        <div class="img_16x9_holder">
          <img src="http://lorempixel.com/g/800/800/" alt="800x800image">
        </div>
      </div>
      <div class="img_matchheight_holder" style="background-image:url(http://lorempixel.com/g/300/650/)">
      </div>
    </div>