【问题标题】:Responsive Flexbox Image Grid响应式 Flexbox 图像网格
【发布时间】:2016-08-25 20:04:16
【问题描述】:

我无法让我的一张图片正确排列。我已经完成了大部分 align vertical-align: middle; 并显示 display: block;options 以删除底部填充。我还将容器设置为width: 100%; 以及图像width: 100%;,因此它应该是响应式的。我不确定是什么导致这张图片有差距。我已将背景颜色设置为黄色,以便显示需要填充的区域。我还注释掉了我尝试但没有成功的所有替代选项。

例子:

 * {
   box-sizing: border-box;
   font-size: 100%;
 }
 
 .container {
   display: flex;
   flex-direction: column;
   max-width: 90%;
   margin: 0 auto;
   background-color: white;
 }
 
 .img__container {
   display: flex;
/*    justify-content: space-between; */
   align-content: center;
   background: yellow;
 }
 
 a .img_item {
   width: 100%;
   height: auto;
   vertical-align: middle;
/*    display: block; */
/*      flex-grow: 1;
  flex-shrink: 0;
  flex-basis: auto; */
 }
 
 .img_item_1,
 .img_item_3,
 .img_item_2 {
   width: 33.33%;
 }
 
 .img_item_4,
 .img_item_5 {
   width: 50%;
 }
 
 .img_item img {
   vertical-align: middle;
/*    display: block; */
   max-width: 100%;

 }
<div class="container">

  <div class="img__container">
    <a href="#" class="img_item img_item_1"><img src="https://images.unsplash.com/photo-1460626399219-57a00a2361cb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=ea8025ac5c503a77aaf3197534af535b" alt=""></a>
    <a href="#" class="img_item img_item_2"><img src="https://images.unsplash.com/photo-1460400355256-e87506dcec4f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=65ebb274e22b4db0f6cef789563020c5" alt=""></a>
    <a href="#" class="img_item img_item_3"><img src="https://images.unsplash.com/photo-1453668069544-b8dbea7a0477?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=3693c161a8cf1e3299c913eede08005a" alt=""></a>
  </div>
  <div class="img__container">
    <a href="#" class="img_item img_item_4"><img src="https://images.unsplash.com/photo-1428189923803-e9801d464d76?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=467ee7b8a091aa5cb8bc9b496aada853" alt=""></a>
    <a href="#" class="img_item img_item_5"><img src="https://images.unsplash.com/photo-1458724338480-79bc7a8352e4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=0e8fe82e7f50091319fdc635582bf62d" alt=""></a>
  </div>
  <div class="img__container">
    <a href="#" class="img_item img_item_6"><img src="https://images.unsplash.com/photo-1421749810611-438cc492b581?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=072549d9d9ee6a1f78d91081068c6ad1" alt=""></a>
    <a href="#" class="img_item img_item_7"><img src="https://images.unsplash.com/photo-1433190152045-5a94184895da?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=57115141c5d099ff83a0aa55c0b219a9" alt=""></a>
  </div>
</div>

【问题讨论】:

    标签: html css image responsive-design flexbox


    【解决方案1】:

    问题是您正在尝试将不同宽高比的图像放入相同大小的框中(我说图像是因为它还显示您的前两个图像在底部有 1 个像素的间隙。)

    您的选择是:

    在 CMS 端将所有图像裁剪为相同大小。

    或

    将您的图片附加为内嵌背景图片,并在您的 CSS 文件中将 background-size 设置为 cover 并将 background-position 设置为 center center。您还需要调整其他一些事情。如果您对这种方法感兴趣,请在 cmets 中告诉我,我可以使用 sn-p 编辑我的答案。

    编辑

    背景图片(为简洁起见,我只做了一张):

    * {
       box-sizing: border-box;
       font-size: 100%;
     }
     
     .container {
       display: flex;
       flex-direction: column;
       max-width: 90%;
       margin: 0 auto;
       background-color: white;
     }
     
     .img__container {
       display: flex;
       align-content: center;
       background: yellow;
     }
    
     .img_item {
       background-size: cover;
       background-position: center center;
     }
     
     .img_item_4,
     .img_item_5 {
       width: 50%;
     }
     
     .img_item img {
       vertical-align: middle;
       max-width: 100%;
    
     }
    <div class="container">
    
      <div class="img__container">
        <a href="#" class="img_item img_item_1"><img src="https://images.unsplash.com/photo-1460626399219-57a00a2361cb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=ea8025ac5c503a77aaf3197534af535b" alt=""></a>
        <a href="#" class="img_item img_item_2"><img src="https://images.unsplash.com/photo-1460400355256-e87506dcec4f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=65ebb274e22b4db0f6cef789563020c5" alt=""></a>
        <a href="#" class="img_item img_item_3"><img src="https://images.unsplash.com/photo-1453668069544-b8dbea7a0477?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=3693c161a8cf1e3299c913eede08005a" alt=""></a>
      </div>
      <div class="img__container">
        <a href="#" class="img_item img_item_4" style="background-image:url(https://images.unsplash.com/photo-1428189923803-e9801d464d76?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=467ee7b8a091aa5cb8bc9b496aada853);"></a>
        <a href="#" class="img_item img_item_5"><img src="https://images.unsplash.com/photo-1458724338480-79bc7a8352e4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=0e8fe82e7f50091319fdc635582bf62d" alt=""></a>
      </div>
      <div class="img__container">
        <a href="#" class="img_item img_item_6"><img src="https://images.unsplash.com/photo-1421749810611-438cc492b581?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=072549d9d9ee6a1f78d91081068c6ad1" alt=""></a>
        <a href="#" class="img_item img_item_7"><img src="https://images.unsplash.com/photo-1433190152045-5a94184895da?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=57115141c5d099ff83a0aa55c0b219a9" alt=""></a>
      </div>
    </div>

    编辑 2

    * {
       box-sizing: border-box;
       font-size: 100%;
     }
     
     .container {
       display: flex;
       flex-direction: column;
       max-width: 90%;
       margin: 0 auto;
       background-color: white;
     }
     
     .img__container {
       display: flex;
       align-content: center;
       background: yellow;
     }
    
     .img_item {
       background-size: cover;
       background-position: center center;
     }
    
     .img_item_1,
     .img_item_2,
     .img_item_3 {
       padding-bottom: 22%;
       width: 33.333%;
     }
     
     .img_item_4,
     .img_item_5,
     .img_item_6,
     .img_item_7 {
       padding-bottom: 33.333%;
       width: 50%;
     }
     
     .img_item img {
       vertical-align: middle;
       max-width: 100%;
    
     }
    <div class="container">
    
      <div class="img__container">
        <a href="#" class="img_item img_item_1" style="background-image:url(https://images.unsplash.com/photo-1460626399219-57a00a2361cb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=ea8025ac5c503a77aaf3197534af535b);"></a>
        <a href="#" class="img_item img_item_2" style="background-image:url(https://images.unsplash.com/photo-1460400355256-e87506dcec4f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=65ebb274e22b4db0f6cef789563020c5);"></a>
        <a href="#" class="img_item img_item_3" style="background-image:url(https://images.unsplash.com/photo-1453668069544-b8dbea7a0477?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=3693c161a8cf1e3299c913eede08005a);"></a>
      </div>
      <div class="img__container">
        <a href="#" class="img_item img_item_4" style="background-image:url(https://images.unsplash.com/photo-1428189923803-e9801d464d76?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=467ee7b8a091aa5cb8bc9b496aada853);"></a>
        <a href="#" class="img_item img_item_5" style="background-image:url(https://images.unsplash.com/photo-1458724338480-79bc7a8352e4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=0e8fe82e7f50091319fdc635582bf62d);"></a>
      </div>
      <div class="img__container">
        <a href="#" class="img_item img_item_6" style="background-image:url(https://images.unsplash.com/photo-1421749810611-438cc492b581?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=072549d9d9ee6a1f78d91081068c6ad1);"></a>
        <a href="#" class="img_item img_item_7" style="background-image:url(https://images.unsplash.com/photo-1433190152045-5a94184895da?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=57115141c5d099ff83a0aa55c0b219a9);"></a>
      </div>
    </div>

    【讨论】:

    • 我确实尝试将图像设置为背景图像。我发现调整大小时结果有很多移动。我可能做错了,请告诉我。我希望图像按照我目前的大小调整大小。我喜欢它们在调整页面大小时的外观。示例(背景图像):codepen.io/vaarellano/pen/vGQOvG?editors=1100
    • 请查看我的编辑 :) 我没有注意到您所指的运动。为了填充空间,必须以一种或另一种方式裁剪图像。
    • 感谢您的编辑。当我尝试使用背景图像更新所有代码并更新背景大小和背景位置的 CSS 时,图像折叠了。您将如何解决此问题,再次感谢您的帮助,这在过去 6 小时里一直让我发疯。
    • 啊,是的,对不起。添加带有百分比值的padding-bottom(参见我的第二次编辑)(较小的三行为 22%,二行为 33% 左右)可以让您接近原始示例。 SO上有一个问题和答案,它解释了它是如何/为什么起作用的。让我看看我能不能把它挖出来。
    猜你喜欢
    • 2019-09-19
    • 2017-08-28
    • 2016-07-11
    • 2017-01-12
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2021-08-23
    • 2013-09-29
    相关资源
    最近更新 更多