【问题标题】:Equal Column Heights with Boostrap and Flexbox使用 Bootstrap 和 Flexbox 等列高度
【发布时间】:2017-09-02 00:16:00
【问题描述】:

我正在尝试使用 Bootstrap 和 Flexbox 设置一个画廊的样式,其中有一个大图像,旁边是两个较小的堆叠图像,并且两者的高度相同。

容器列似乎在完成它们的工作并保持相同的高度,但我需要列中的img 来填充容器的高度。

如果我在 img 上使用 height: 100%,它可以在 Firefox 中运行,但这在 Chrome 和 Safari 中都会中断。

img {
  max-width: 100%;
  width: auto\9;
  height: auto;
  vertical-align: middle;
  border: 0;
  -ms-interpolation-mode: bicubic;
}
figure {
  margin-bottom: 0;
}
.rts-col {
  margin-bottom: 30px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="rts-col col-8">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
    <div class="rts-col col-4">
      <figure style="margin-bottom: 30px;">
        <img src="http://via.placeholder.com/1400x933">
      </figure>
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
  </div>
</div>

这是标记和样式表:https://jsfiddle.net/tylorreimer/q8qe5p80/2/

【问题讨论】:

  • @Paulie_D 我没有意识到我可以将代码插入到关于 SO 的问题中。我刚刚更新了我的问题以包含最少的代码标记。

标签: css flexbox bootstrap-4


【解决方案1】:

在我看来,您好像需要嵌套的弹性框,其中一侧有多个图像。

img {
  max-width: 100%;
  width: auto\9;
  height: auto;
  vertical-align: middle;
  border: 0;
  -ms-interpolation-mode: bicubic;
}

figure {
  margin: 0;
}

.rts-col.split {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="rts-col col-8">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
    <div class="rts-col col-4 split">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>

  </div>
</div>

【讨论】:

  • 你和@LGSon 基本上有相同的解决方案,但我选择了你的解决方案,因为我的 HTML 标记中的类更少。干杯!
【解决方案2】:

如果您将col-4 设为具有column 方向的弹性容器,则可以使用justify-content 及其space-between 来对齐这两个图像的顶部/底部

d-flex flex-column justify-content-between 添加到您的&lt;div class="rts-col col-4"&gt;,使其变为&lt;div class="rts-col col-4 d-flex flex-column justify-content-between"&gt;

请注意,我还删除了一些边距以使它们更好地对齐

img {
  display: block;
  max-width: 100%;
}
figure {
  margin: 0;
}
.rts-col {
  margin-bottom: 30px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="rts-col col-8">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
    <div class="rts-col col-4 d-flex flex-column justify-content-between">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
    <div class="rts-col col-6">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
    <div class="rts-col col-6">
      <figure>
        <img src="http://via.placeholder.com/1400x933">
      </figure>
    </div>
  </div>
</div>

【讨论】:

  • 非常感谢!我最终选择了@Paulie_D 解决方案,以使我的 HTML 更简洁。
  • @tylorreimer 非常好,不过不要这样,html 中的 3 个类比创建额外的 CSS 规则要小,这是使用框架的全部意义......并使其更容易在标记发生了什么。
  • 我实际上只为基本网格使用了精简版的 Bootstrap。无论如何,我都必须添加这些额外的类,因此将样式添加到一个类中仍然更加简单。
猜你喜欢
  • 2017-02-13
  • 1970-01-01
  • 2023-02-22
  • 2021-04-22
  • 1970-01-01
  • 2018-11-08
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多