如何使 Flexbox 列具有相同的高度?

2024-02-13

我正试图将我的头围绕在弹性盒上,但我却因为一些非常简单的事情而碰壁,我似乎无法做到正确。

我有 6 列,我希望它们的高度相同,每行 3 列。我已将 Flex 项目的宽度设置为 33%,但没有设置任何内容的高度。我认为一旦弹性项目有了内容,它就会选择高度最大的项目并将所有内容与其匹配。

这是我的标记:

.container {
  width: 800px;
  padding: 0 15px;
}

.row-flex {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  margin-right: -15px;
  margin-left: -15px;
  flex-wrap: wrap;
}

.flexbox {
  flex: 0 0 33%;
  padding: 0 15px;
}

.icon-box-1 {
  position: relative;
  margin-bottom: 50px;
  background: #fff;
  text-align: center;
  border: 1px solid #eee;
  padding: 10px;
}
<div class="container">
  <div class="row-flex">
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          One
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Two
        </h1>
        <h3>Title</h3>
        <p>lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Three
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Four
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Five
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Six
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
  </div>
</div>

等高列功能来自align-items: stretch,这是 Flex 容器上的初始设置。它使弹性项目占用了所有可用空间交叉轴.

如果您检查每个弹性项目的实际大小,您会发现它们实际上在每行上具有相同的高度(demo https://jsfiddle.net/x7a37s7d/).

问题是每个项目的内容(.icon-box-1) 未拉伸至全高。只是height: auto(内容高度),默认情况下。

为了使内容伸展到整个高度,添加display: flex对于每个弹性项目,所以align-items: stretch生效,就像对父级 (demo https://jsfiddle.net/x7a37s7d/1/).

然后使用flex: 1使内容填满剩余空间主轴 (demo https://jsfiddle.net/x7a37s7d/2/).

.container {
  width: 800px;
  padding: 0 15px;
}

.row-flex {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  margin-right: -15px;
  margin-left: -15px;
  flex-wrap: wrap;
}

.flexbox {
  flex: 0 0 33%;
  padding: 0 15px;
  border: 1px dashed red; /* for illustration */
  display: flex; /* new */
}

.icon-box-1 {
  flex: 1; /* NEW */
  position: relative;
  margin-bottom: 50px;
  background: #fff;
  text-align: center;
  border: 1px solid #eee;
  padding: 10px;
}
<div class="container">
  <div class="row-flex">
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          One
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Two
        </h1>
        <h3>Title</h3>
        <p>lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Three
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Four
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Five
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
    <div class="flexbox">
      <div class="icon-box-1">
        <h1>
          Six
        </h1>
        <h3>Title</h3>
        <p>lots of random text, lots of random text, lots of random text</p>
      </div>
    </div>
  </div>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使 Flexbox 列具有相同的高度? 的相关文章

随机推荐

  • 如何在 C# 中计算“五的中位数”?

    五的中位数有时被用作算法设计的练习 并且众所周知是可计算的仅使用 6 次比较 实施这个的最好方法是什么 使用 6 次比较得出 5 的中位数 在 C 中 我所有的尝试似乎都导致了尴尬的代码 我需要漂亮且可读的代码 同时仍然只使用 6 次比较
  • 带 order by 和 limit 的 SQL Union All (Postgresql)

    在以下查询中我收到语法错误 SELECT
  • C++ 对象初始化(堆栈)

    今天我看到一个我不熟悉的类的 C 初始化 CPrice price CPrice 初始化通常应该是这样的 CPrice price 我猜第一个应该抛出错误或其他东西 这里会发生什么 我猜想该变量位于堆栈上 因为它没有用new 我使用 Vis
  • Core Data 管理对象上下文设计建议

    我们正在开发一个企业级应用程序 它将使用核心数据存储数以万计的对象 但我们在多个方面都遇到了问题 我们的应用程序有几个独立的系统 可以在需要时对数据进行操作 这些系统包括项目的发现 项目的加载 同步和UI显示 如果我们正确地设计我们的软件
  • 打开 SQL Server 实例的端口

    他们最近封锁了我们 SQL Server 上的所有端口 服务器有不同的实例 打开端口 1433 不起作用 我们需要开放什么才能允许访问SQL Server 在安装服务器实例的计算机上 启动 SQL Server 配置管理器实用程序 打开树形
  • 更改 R 中的绘图标签大小,cex 不起作用

    我正在 R 中制作一个非常简单的绘图 并且希望更改 y 轴 年份 上的字体大小 我已经用了所有的cex 命令 可以改变一切 除了这些年 这是我的矩阵 输入 2010 2011 CC 0 5550 0 480 P 3 6700 3 865 P
  • 如何避免重复代码?

    我对编程还很陌生 我注意到我在重复代码 protected void FillTradeSetups DBUtil DB new DBUtil DataTable dtTradeSetups dtTradeSetups DB GetTrad
  • 仅获取朋友的total_count - facebook-graph-api

    me fields friends获取 friends summary total count 72 我只是想total count 但它似乎不是我可以放入查询中的有效子字段 me fields friends summary or me
  • @code 在 Google Closure 中意味着什么?

    一个例子在这里 An implementation of code goog events Listenable with full W3C EventTarget like support capture bubble mechanism
  • 如何从 Hibernate 调用带有返回参数的 Oracle 函数?

    我的问题很像通过 Hibernate 获取 PL SQL 函数的返回值 https stackoverflow com questions 1068974 getting the return value of a pl sql funct
  • Spark 从 DataFrame 中删除重复行 [duplicate]

    这个问题在这里已经有答案了 假设我有一个像这样的 DataFrame val json sc parallelize Seq a 1 b 2 c 22 d 34 a 3 b 9 c 22 d 12 a 1 b 4 c 23 d 12 val
  • 当调试符号被分割成 dwo dwarf 文件时,gdb 找不到符号

    我想构建一个二进制文件g 并将调试信息分割到一个单独的文件中 所谓的DebugFission 假设您位于一个文件夹中 有一个文件src main cpp内容琐碎 int main 我想使用编译它 gsplit dwarf mkdir p o
  • “使用命名空间 std”有什么用? [复制]

    这个问题在这里已经有答案了 有什么用using namespace std 我希望看到外行术语的解释 using 你会用它 名称空间 用什么 一个命名空间 std The std命名空间 C 标准库的功能 例如string or vecto
  • 在 Web 应用程序机器人和功能机器人之间进行选择 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 根据本文 在 Azure 机器人服务中创建机器人时 您有两种托管选项 https learn microsoft com en us bot f
  • 反应电话号码输入格式和无格式输出

    我正在尝试在输入中创建电话号码格式 并且其下方的输出中没有电话号码格式 我还想添加一个条件 如果用户输入超过 10 个条目 则输入中将不再有电话格式 另外 请告诉我是否有任何方法可以在没有react number format 库的情况下执
  • ReactNative:将 JS 变量传递给 AppDelegate

    正如标题中的那样 我已经通过变量作为参数传递给本机模块RCT EXPORT MODULE 我知道RCT CUSTOM VIEW PROPERTY 虽然这是用于 可调用 组件 但我不确定它是否可以用于将 JS 变量传递给AppDelegate
  • 在oracle for循环中动态传递表名

    是否可以在oracle for循环中动态传递表名 e g for nm in select from table name loop dbms output put line chetan end loop 您可以使用 REF CURSOR
  • 这个任务正确吗? [复制]

    这个问题在这里已经有答案了 在一些js库中我发现了这个代码片段 var start end sel scrollPos subst start end scrollPos sel getSelection 在我看来 这不是有效的分配 但代码
  • 如何找出实际安装的 Bower 软件包的版本?

    通常是一个bower json文件指定了一些依赖项 但这些依赖项通常被表达为允许使用一系列版本的 Bower 包 例如 gt 1 0 这意味着高于 1 0 版本的任何版本 我有一个自动化流程 需要查找 Bower 包的版本实际安装现在就在这
  • 如何使 Flexbox 列具有相同的高度?

    我正试图将我的头围绕在弹性盒上 但我却因为一些非常简单的事情而碰壁 我似乎无法做到正确 我有 6 列 我希望它们的高度相同 每行 3 列 我已将 Flex 项目的宽度设置为 33 但没有设置任何内容的高度 我认为一旦弹性项目有了内容 它就会