【问题标题】:Make grid item use remaining space like flex item with flex-grow: 1 [duplicate]使用 flex-grow: 1 [重复] 使网格项目使用剩余空间,如 flex 项目
【发布时间】:2018-05-14 19:44:58
【问题描述】:

在一个 flexbox 中,我可以在一个孩子上指定 flex-grow:1 来表示它应该占用剩余空间。

html, body {
  height: 100%
}
<div style="display:flex;flex-direction:column;height:100%">     
  <div style="background-color:yellow;height:50px"></div>
  <div style="background-color:red;flex-grow:1"></div>
</div>

是否可以用 css 网格做同样的事情?即针对子项指定它应该使用剩余空间而不使用 grid-template-rows 显式定义它?

html, body {
  height: 100%
}
<div style="display:grid;grid-template-columns:auto;height:100%">
  <div style="background-color:yellow;height:50px"></div>
  <!-- what to use instead of flex-grow:1? -->
  <div style="background-color:red;flex-grow:1;"></div>
</div>

真实场景涉及一个网格,其中包含运行时定义的列数,以及运行时定义的一组子项,其中一些可能是 display:none。

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    您可以使用grid-template-rows: min-content auto; 指定行高,这会最小化第一行并拉伸第二行。

    html,
    body {
      height: 100%
    }
    <div style="display:grid;grid-template-columns:auto;height:100%;grid-template-rows: min-content auto;">
      <div style="background-color:yellow;height:50px"></div>
      <div style="background-color:red;"></div>
    </div>

    【讨论】:

    • 谢谢,但我的问题明确指出“没有使用 grid-template-rows 明确定义它”。
    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 2020-04-24
    • 2020-07-20
    • 1970-01-01
    • 2016-10-16
    • 2018-12-23
    • 2021-12-07
    • 2017-08-03
    相关资源
    最近更新 更多