英文:
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;
英文:
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 '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
得分: 1
考虑从#row2
中删除height: 100%
(h-full
)。这会使#row2
的高度实际上成为height: 100vh
,因为它从其父级继承了height 100%
。由于它之前有一个元素,这意味着#row1
+ #row2
的总高度会超过100vh
,因此会出现文档滚动。
通过grow
将flex-grow: 1
应用于#row2
,以填充 flex 布局的“剩余”高度。
通过min-h-0
将min-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 -->
<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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论