在Tailwind中,如何填充父元素的剩余空间而不溢出?

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

In Tailwind, how do I fill the remaining space of the parent without overflowing?

问题

我正在使用Tailwind CSS来为我的React应用程序设置样式。我在处理从弹性容器中溢出的文本方面遇到了问题。

为简化起见,我将屏幕分成了4个象限。

在第四象限,我的“Lorem Ipsum”文本溢出了其容器。正如预期的那样,Q4容器有自己的滚动条(图像1)。

我的问题是整个应用程序也会出现滚动条。此外,Q4容器也会溢出“flex-col”元素(图像2)。

我的理解(可能是错误的)是,因为我的父元素使用了className='h-screen',并且子元素使用了className='h-full'(即父元素的100%),没有容器应该溢出屏幕大小。

这是我的代码,其中Lorem ipsum文本被截断:

import React from 'react';

const exampleText = `Lorem ipsum dolor sit amet...`;

const Test = () => {
  return (
    <div className='h-screen'>
      <div id='flex-col' className='flex flex-col border-2 border-slate-600 h-full'>
        <div id='row1' className='flex flex-row border-2 border-red-600'>
          <div id='q1' className='basis-1/2 border-2 border-blue-500'>
            Q1
          </div>
          <div id='q2' className='basis-1/2 border-2 border-blue-500'>
            Q2
          </div>
        </div>
        <div id='row2' className='flex flex-row border-2 border-red-600 h-full'>
          <div id='q3' className='basis-1/2 border-2 border-blue-500'>
            Q3
          </div>
          <div id='q4' className='h-full basis-1/2 border-2 border-blue-500'>
            <div className='h-full overflow-auto'>{exampleText}</div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Test;

图像1:
在Tailwind中,如何填充父元素的剩余空间而不溢出?

图像2:
在Tailwind中,如何填充父元素的剩余空间而不溢出?

英文:

I am using Tailwind CSS to style my React app. I am having trouble dealing with text which overflows from a flex container.

For simplicity, I have divided my screen into 4 quadrants.

In Quadrant 4, my "Lorem Ipsum" text overflows it's container. The Q4 container gets it's own scrollbar as expected (Image 1).

My issue is that the entire app also gets a scrollbar. Furthermore, the Q4 container overflows the "flex-col" element (Image 2).

My understanding (which must be incorrect), is that because my parent element uses className='h-screen', and because the children use className='h-full' (i.e. 100% of the parent), no container should overflow the screen size.

This is my code with the Lorem ipsum text truncated:

import React from &#39;react&#39;;

const exampleText = `Lorem ipsum dolor sit amet...`;

const Test = () =&gt; {
  return (
    &lt;div className=&#39;h-screen&#39;&gt;
      &lt;div id=&#39;flex-col&#39; className=&#39;flex flex-col border-2 border-slate-600 h-full&#39;&gt;
        &lt;div id=&#39;row1&#39; className=&#39;flex flex-row border-2 border-red-600&#39;&gt;
          &lt;div id=&#39;q1&#39; className=&#39;basis-1/2 border-2 border-blue-500&#39;&gt;
            Q1
          &lt;/div&gt;
          &lt;div id=&#39;q2&#39; className=&#39;basis-1/2 border-2 border-blue-500&#39;&gt;
            Q2
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div id=&#39;row2&#39; className=&#39;flex flex-row border-2 border-red-600 h-full&#39;&gt;
          &lt;div id=&#39;q3&#39; className=&#39;basis-1/2 border-2 border-blue-500&#39;&gt;
            Q3
          &lt;/div&gt;
          &lt;div id=&#39;q4&#39; className=&#39;h-full basis-1/2 border-2 border-blue-500&#39;&gt;
            &lt;div className=&#39;h-full overflow-auto&#39;&gt;{exampleText}&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

export default Test;

Image 1:
在Tailwind中,如何填充父元素的剩余空间而不溢出?
Image 2:

在Tailwind中,如何填充父元素的剩余空间而不溢出?

答案1

得分: 1

考虑从#row2中删除height: 100% (h-full)。这会使#row2的高度实际上成为height: 100vh,因为它从其父级继承了height 100%。由于它之前有一个元素,这意味着#row1 + #row2的总高度会超过100vh,因此会出现文档滚动。

通过growflex-grow: 1 应用于#row2,以填充 flex 布局的“剩余”高度。

通过min-h-0min-height: 0 应用于#row2,以覆盖隐式的min-height: min-content,使其在容器之外增长高度。

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->
    <script src="https://cdn.tailwindcss.com/3.3.2"></script>

    <div class='h-screen'>
      <div id='flex-col' class='flex flex-col border-2 border-slate-600 h-full'>
        <div id='row1' class='flex flex-row border-2 border-red-600'>
          <div id='q1' class='basis-1/2 border-2 border-blue-500'>
            Q1
          </div>
          <div id='q2' class='basis-1/2 border-2 border-blue-500'>
            Q2
          </div>
        </div>
        <div id='row2' class='flex flex-row border-2 border-red-600 grow min-h-0'>
          <div id='q3' class='basis-1/2 border-2 border-blue-500'>
            Q3
          </div>
          <div id='q4' class='basis-1/2 border-2 border-blue-500 h-full'>
            <div class='h-full overflow-auto'>
              Lorem ipsum dolor sit amet...
              <div class="h-[200vh]"></div>
            </div>
          </div>
        </div>
      </div>
    </div>

<!-- end snippet -->
英文:

Consider removing height: 100% (h-full) from #row2. This is making the height of #row2 effectively height: 100vh after "inheriting" down the height 100% from its parents. Since there is an element before it, this would mean that the overall height of #row1 + #row2 would be more than the 100vh, hence the document scroll.

Apply flex-grow: 1 via grow to #row2 so that it fills the "leftover" height of the flex layout.

Apply min-height: 0 via min-h-0 to #row2 to override the implicit min-height: min-content so it does grow in height beyond the container.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com/3.3.2&quot;&gt;&lt;/script&gt;

&lt;div class=&#39;h-screen&#39;&gt;
  &lt;div id=&#39;flex-col&#39; class=&#39;flex flex-col border-2 border-slate-600 h-full&#39;&gt;
    &lt;div id=&#39;row1&#39; class=&#39;flex flex-row border-2 border-red-600&#39;&gt;
      &lt;div id=&#39;q1&#39; class=&#39;basis-1/2 border-2 border-blue-500&#39;&gt;
        Q1
      &lt;/div&gt;
      &lt;div id=&#39;q2&#39; class=&#39;basis-1/2 border-2 border-blue-500&#39;&gt;
        Q2
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&#39;row2&#39; class=&#39;flex flex-row border-2 border-red-600 grow min-h-0&#39;&gt;
      &lt;div id=&#39;q3&#39; class=&#39;basis-1/2 border-2 border-blue-500&#39;&gt;
        Q3
      &lt;/div&gt;
      &lt;div id=&#39;q4&#39; class=&#39;basis-1/2 border-2 border-blue-500 h-full&#39;&gt;
        &lt;div class=&#39;h-full overflow-auto&#39;&gt;
          Lorem ipsum dolor sit amet...
          &lt;div class=&quot;h-[200vh]&quot;&gt;&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月5日 00:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614506.html
匿名

发表评论

匿名网友

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

确定