CSS ReactJS – div未将其高度设置为全高

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

CSS ReactJS - div not setting his height to full

问题

我有一个导航栏和一个组件,需要填充屏幕高度的剩余空间。

绿色 - 我想要得到的结果
蓝色 - 当前的样子。

我尝试设置高度为h-screen,但也不起作用...(过度拟合)
我在使用tailwind进行CSS

Home.tsx

import Navbar from './components/navbar';
import Cart from './components/Cart';

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col">
      <Navbar />
      <div className="flex h-full">
        <Cart />
      </div>
    </main>
  )
}

Cart.tsx

export default function Cart() {
  return (
    <div className="w-1/5 p-5 text-left border h-full">
      <p>我的购物车</p>
    </div>
  )
}
英文:

I have a navbar and a component that needs to fill the remaining space of the screen height.

CSS ReactJS – div未将其高度设置为全高
Green - the result I want to get
Blue - how it looks right now.

I have tried to set the height to h-screen but it doesn't work too... (its overfitting)
I am using tailwind for CSS

Home.tsx

import Navbar from &#39;./components/navbar&#39;;
import Cart from &#39;./components/Cart&#39;;

    export default function Home() {
      return (
        &lt;main className=&quot;flex min-h-screen flex-col&quot;&gt;
          &lt;Navbar /&gt;
          &lt;div className=&quot;flex h-full&quot;&gt;
            &lt;Cart /&gt;
          &lt;/div&gt;
    
        &lt;/main&gt;
      )
    }

Cart.tsx

export default function Cart() {
  return (
    &lt;div className=&quot;w-1/5 p-5 text-left border h-full&quot;&gt;
        &lt;p&gt;הסל קניות שלי&lt;/p&gt;
    &lt;/div&gt;
  )
}

答案1

得分: 1

你可以考虑将flex-grow: 1应用于&lt;Cart&gt;&lt;div&gt;包装器,以使&lt;div&gt;包装器在屏幕高度中占用&lt;NavBar&gt;未占用的部分。然后,从&lt;Cart&gt;的根级&lt;div&gt;中移除height: 100%,以便默认的align-items: stretch生效,允许&lt;Cart&gt;的根级&lt;div&gt;的高度等于&lt;Cart&gt;&lt;div&gt;包装器的高度。

英文:

You could consider applying flex-grow: 1 to the &lt;div&gt; wrapper of &lt;Cart&gt; so that the &lt;div&gt; wrapper grows in height to the remainder of the screen height not occupied by &lt;NavBar&gt;. Then, remove height: 100% from the &lt;Cart&gt; root level &lt;div&gt; so that the default align-items: stretch takes effect, allowing the &lt;Cart&gt; root level &lt;div&gt; to be the height of the &lt;div&gt; wrapper of &lt;Cart&gt;.

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

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

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

&lt;main class=&quot;flex min-h-screen flex-col&quot;&gt;
  &lt;Navbar&gt;Navbar&lt;/Navbar&gt;

  &lt;div class=&quot;flex grow&quot;&gt;
    &lt;div class=&quot;w-1/5 p-5 text-left border&quot;&gt;
      &lt;p&gt;הסל קניות שלי&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/main&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 01:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425821.html
匿名

发表评论

匿名网友

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

确定