【问题标题】:How to stretch an image to cover full width and height of the div? (Using Flexbox)如何拉伸图像以覆盖 div 的全宽和全高? (使用弹性盒)
【发布时间】:2016-03-17 10:43:29
【问题描述】:

我想调整图像的大小以覆盖整个 div。如果你们可以在 CSS 中提出任何建议而不是使用 javascript,那将非常有帮助。

这是我现在拥有的样本

.third{
  width:30%;
  float:left;
  margin:8px;
}

.second{
  display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-flex-flow: row wrap;
    -ms-flex-flow: row wrap;
    flex-flow: row wrap;
}
img{
  width:100%;
  object-fit:fill;
}

<div class="first">
  <div class="second">
    <div class="third">
      <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
    <div class="third">
      <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third">
      <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
    <div class="third">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
  </div>
</div>

这里是codepen的链接了解更多详情:http://codepen.io/gruler05/pen/vGyObo

第一个、第三个和第四个图像比其他图像小。谁能告诉我如何将这些图像拉伸到其容器 div 的大小,以使所有图像看起来都具有相同的大小。先感谢您。 P.S:我尝试了对象拟合,但没有奏效。

【问题讨论】:

  • 您可以将图像设置为 div 的背景图像并使用 background-size:cover。
  • 我想过,但有没有其他方法可以在不使用背景图像的情况下做到这一点?
  • 你需要图片本身来设置高度/宽度吗?background-image不需要,图片需要保持纵横比吗?
  • @LGSon 如果某些图像失去了它的纵横比,那很好。因为我知道 15 张图片中只有 3-4 张的高度会比其他图片略低。
  • 我发布了一个更新,给你一些选择。

标签: html angularjs css flexbox


【解决方案1】:

使用背景尺寸:封面;在元素的 CSS 规则中可能是获得完整图像覆盖率的最简单方法。不过,这可能会截断一些内容,具体取决于您是否将背景图像置于 div 中或如何调整它的大小。

背景尺寸:包含;可能更有用,但我是 cover size 属性的忠实粉丝。

更新

如果您想避免使用背景图像,那么您可以尝试将 img 标签设置为 100% 宽度和/或高度,但这会扭曲您的图像并导致其他问题,并且您可能需要执行一些 JS 计算防止这种情况发生。

为什么不喜欢使用背景图片?

【讨论】:

  • 我正在使用 ng-repeat 从 json 文件中获取图像的来源。然后我想必须将它用作内联 css 属性以将其设置为背景图像,然后循环遍历它。
【解决方案2】:

这里有几种方法,具体取决于您是要拉伸还是剪裁图像。

在示例 1 和 2 中,我使用的技巧是将 img 保持为 visibility: hidden,而不必在 third div 上设置固定高度。

此外,使用图像 url 的内联样式,您可以轻松地使用 angular 使其工作,而不会弄乱外部 CSS。

示例 1 - 拉伸

.third {
  flex: 1 0 calc(33% - 16px);
  margin: 8px;
  position: relative;
  background-size: 100% 100%;
}
.second{
  display: flex;
  flex-flow: row wrap;
}
img {
  visibility: hidden;
  width: 100%;
  height: auto;
}
<div class="first">
  <div class="second">
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
        <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
        <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
        <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
  </div>
</div>

示例 2 - 剪裁,居中对齐

.third {
  flex: 1 0 calc(33% - 16px);
  margin: 8px;
  position: relative;
  background-size: cover;
  background-position: center;
}
.second{
  display: flex;
  flex-flow: row wrap;
}
img {
  visibility: hidden;
  width: 100%;
  height: auto;
}
<div class="first">
  <div class="second">
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
        <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
        <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
        <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
      <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
    </div>
  </div>
</div>

示例 3 - 剪裁、居中对齐、固定高度(也删除了 img

.third {
  flex: 1 0 calc(33% - 16px);
  margin: 8px;
  position: relative;
  background-size: cover;
  background-position: center;
  height: 150px;
}
.second{
  display: flex;
  flex-flow: row wrap;
}
img {
  visibility: hidden;
  width: 100%;
  height: auto;
}
<div class="first">
  <div class="second">
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
    </div>
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
    </div>
    <div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
    </div>
    <div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
    </div>
  </div>
</div>

【讨论】:

    【解决方案3】:

    尝试调整您的 .third,它有一个默认填充,您可以通过添加以下 CSS 来删除它。您可以根据需要调整高度。这样你可以避免使用背景图片,但背景图片仍然是最有效的。

    .third {
      width: 33.33%;
      height: 100px;
      float: left;
      margin:0;
      padding:0;
    }
    

    【讨论】:

      【解决方案4】:

      你可以这样做,但它会丢失图像的纵横比。

      HTML:添加一个 class="row" 的 div 并在每个 div 中放置 3 张图片

      <div class="first">
        <div class="second">
        <div class="row">
          <div class="third">
            <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
          </div>
          <div class="third">
            <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
          </div>
          <div class="third">
            <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
          </div>
        </div>
        <div class="row">
          <div class="third">
            <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
          </div>
          <div class="third">
            <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
          </div>
          <div class="third">
            <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
          </div>
        </div>
        </div>
      </div>

      CSS:添加以下代码给 .row 一个固定的高度,并将图像高度设置为 95%

      .row{
      	height:150px; //whatever height you want
      }
      
      .third img{
      	height:95%;
      }

      【讨论】:

        【解决方案5】:

        先删除所有&lt;img&gt;标签
        然后使用这个 css

        .third{
          width:290px;
          height:180px;
          float:left;
          margin:8px;
          background-image:url("https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg");
          background-size:cover;
        }
        

        【讨论】:

          【解决方案6】:

          虽然 flexbox 是一个很好的方法,但最简单的方法是使用表格,并将图像作为数据插入。 这是代码

          <!DOCTYPE html>
          <html>
          <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width">
            <title>Images</title>
          </head>
          <body>
          <table>
            <tr>
              <td>    <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg" width="200" height="200"></td>
           
             <td>   <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg"  width="200" height="200"> </td>
           
              <td>  <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg"   width="200" height="200"> </td>
            </tr> 
            
            <tr>
              <td>   <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg"  width="200" height="200"> </td>
           
               <td> <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg"  width="200" height="200"> </td>
            
             <td>   <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg"  width="200" height="200"> </td>
            </tr> 
            
            </table>
          </body>
          </html>

          【讨论】:

            猜你喜欢
            • 2016-05-24
            • 1970-01-01
            • 1970-01-01
            • 2011-06-04
            • 1970-01-01
            • 1970-01-01
            • 2013-05-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多