【问题标题】:Track not shrinking with resize function使用调整大小功能不缩小轨道
【发布时间】:2019-12-28 15:18:24
【问题描述】:

我想用CSS grid 模拟拆分窗格的行为,但是当我缩小“顶部”可调整大小的 div 时,“底部”div 不会向上,在顶部之间留下难看的空白和底部 div。

我在 Brave 0.70.122(基于 Chromium 78.0.3904.87)和 Firefox 71(两者都在 Ubuntu 64 位上,如果重要的话)都有问题。

如何让两个 div 填满整个可用空间?是否有我不知道的 CSS 属性可以设置?我可以设置grid-template-rows: auto 1fr;,但这会将顶部 div 设置为最小尺寸,我希望最初设置为 75%(但底部 div 不会超过剩余的 25%)。

div {
  /* To visualize boundaries */
  border: 1px solid;
}

#main {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: 3fr 1fr;  /* 3/4 split */
}

#top {
  grid-area: 1 / 1 / span 1 / span 1;
  resize: vertical;
  overflow: auto;
}

#bottom {
  grid-area: 2 / 1 / span 1 / span 1;
}
<div id="main">
  <div id="top">Top</div>
  <div id="bottom">Bottom</div>
</div>

【问题讨论】:

    标签: html css user-interface css-grid


    【解决方案1】:

    看起来一旦浏览器呈现轨道大小(在本例中为 3fr 1fr),它们就会变成固定长度。所以resize工具只能缩小轨道的内容,不能缩小轨道本身。

    尝试不同的方法。

    而不是这个:

    #main {
      grid-template-rows: 3fr 1fr;
    }
    

    试试这个:

    #main {
      grid-template-rows: auto 1fr;
    }
    
    #top {
      height: 75vh;
    }
    

    #main {
      height: 100vh;
      display: grid;
      grid-template-rows: auto 1fr;
    }
    
    #top {
      height: 75vh;
      resize: vertical;
      overflow: auto;
      background-color: lightgreen;
    }
    
    #bottom {
      background-color: orangered;
    }
    
    body {
      margin: 0;
    }
    <div id="main">
      <div id="top">Top</div>
      <div id="bottom">Bottom</div>
    </div>

    jsFiddle demo

    【讨论】:

    • 太棒了 :) 所以如果我理解正确,#main { ...; grid-template-rows: auto 1fr; } 为顶部 div 提供了最小空间,然后 #top { height: 75vh; ...; } 将其返回到视口高度的 75% , 正确的?我尝试将75vh 替换为75%,但结果不一样。
    • 设置为auto 的网格行意味着高度是基于内容的,或者您为网格项设置的任何高度。然后您可以在网格项级别控制行的高度。
    • 要了解为什么 75vh75% 不一定做同样的事情,请参阅这篇文章:stackoverflow.com/a/31728799/3597276
    • 感谢您的见解(以及我的另一个 +1);但我试图强制#main { height: 600px; } 的大小,但#top { height: 75%; } 仍然无法正常工作。网格项目在这方面的处理方式是否不同?
    猜你喜欢
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多