【问题标题】:Bootstrap 4 - trying to keep flex wrapped images overflowing previous columnBootstrap 4 - 试图保持 flex 包装的图像溢出上一列
【发布时间】:2021-07-21 02:07:54
【问题描述】:

我想要一个包含较大图像的左列和一个包含较小缩略图的包装列表的右列。当我将窗口缩小时,最终缩略图会溢出左列。我想让两列始终分开。在最小分辨率下,它确实会移动到下方,但仅在图像叠加之后。

    <div class="container">
      <div class="row">
        <div class="col-lg-4 col-md-6 col-sm-12">
          <div class="p-2">
            <img width="400" height="225" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
          </div>
        </div>
        <div class="col-lg-8 col-md-6 col-sm-12">
          <div class="container-fluid">
            <div class="d-flex flex-row flex-wrap">
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
              <div class="p-2">
                <img width="120" height="67" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">

【问题讨论】:

    标签: image bootstrap-4 flexbox


    【解决方案1】:

    您的 flex 包装图像没有溢出上一列 - 您的第一列中的图像侵入了 flex 包装图像的空间。在左侧图像上使用img-fluid,而不是定义宽度和高度以使其保留在自己的列中。

    您也可以使用img-fluid 来表示右侧的小图像,而不是指定宽度和高度,并且您可以使用row row-cols 来换行,而不是使用flex-wrap(行有flex-wrap )。

    .img-padding {
        padding-bottom: 1.875rem;
    }
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
    
    <div class="container-fluid pt-5">
        <div class="row">
            <div class="col-12 col-md-6 col-lg-4 img-padding">
                <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
            </div>
            <div class="col-12 col-md-6 col-lg-8">
                <div class="row row-cols-2 row-cols-lg-3 row-cols-xl-4">
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                    <div class="col img-padding">
                        <img class="img-fluid" src="https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1055&q=80" />
                    </div>
                </div>
            </div>
        </div>
    </div>

    我确实添加了一个自定义填充类,将 1.875rem (30px) 添加到每个图像的底部,以使图像行之间的间距与列之间的间距相同。

    我的示例是使用 container-fluid 设置的 - 如果您调整浏览器窗口的宽度,您会看到图像大小发生变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 2021-05-05
      • 2021-01-01
      • 2018-06-10
      • 2021-06-10
      • 2014-07-22
      • 1970-01-01
      相关资源
      最近更新 更多