如何使子元素占据其余空间并仍然可以滚动

huangapple go评论65阅读模式
英文:

How do I make child take up rest of the space while still being scrollable

问题

使用Tailwind,我有一个标题和一个可滚动的子div,但可滚动的div占用的空间多于它应该的高度。我想确保可滚动的div不超过其容器的高度。目前,页面可以滚动,标题的高度不正确。

<div class="flex h-screen flex-col">
  <div class="h-48 bg-white">一些内容</div>
  <div class="h-full pb-9">
    <div class="h-full overflow-y-scroll bg-red-300 [其他样式]:px-4" id="content">
      <div class="sticky top-0 z-50 flex items-start justify-between bg-red-300 py-4">
        <div>
          <h2 class="text-xl">标题</h2>
        </div>
      </div>
      <div class="h-full">
        <h1 class="text-7xl">高度太高</h1>
        <!-- 更多相同的内容 -->
      </div>
    </div>
  </div>
</div>

https://play.tailwindcss.com/7ksq08Tm2P

我尝试在顶级父元素上设置overflow-hidden

英文:

Using Tailwind, I have a header and a child div that is scrollable but the scrollable div takes up more space than it should. I want to make sure that the scrollable div doesn't exceed the height of its container. Right now, the page scrolls and the header isn't the height it should be

&lt;div class=&quot;flex h-screen flex-col&quot;&gt;
  &lt;div class=&quot;h-48 bg-white&quot;&gt;some content&lt;/div&gt;
  &lt;div class=&quot;h-full pb-9&quot;&gt;
    &lt;div class=&quot;h-full overflow-y-scroll bg-red-300 [&amp;&gt;*]:px-4&quot; id=&quot;content&quot;&gt;
      &lt;div class=&quot;sticky top-0 z-50 flex items-start justify-between bg-red-300 py-4&quot;&gt;
        &lt;div&gt;
          &lt;h2 class=&quot;text-xl&quot;&gt;header&lt;/h2&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;h-full&quot;&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
        &lt;h1 class=&quot;text-7xl&quot;&gt;too much height&lt;/h1&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

https://play.tailwindcss.com/7ksq08Tm2P

I tried setting overflow-hidden on the top level parent

答案1

得分: 0

只需要将 min-h-0 类添加到 <div class="h-full pb-9">,因为 flex 子元素通常具有 min-height: auto,如下所示:

https://play.tailwindcss.com/BFQ0Xah5gA

英文:

You just need to add min-h-0 class to &lt;div class=&quot;h-full pb-9&quot;&gt;, because the flex child normally has min-height: auto, like this:

https://play.tailwindcss.com/BFQ0Xah5gA

huangapple
  • 本文由 发表于 2023年2月18日 12:27:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定