为什么网格子元素的大小会根据内容而改变

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

Why do the grid children change size depending on the content

问题

以下是使用普通网格实现的期望结果:

为什么网格子元素的大小会根据内容而改变

原始代码:

import { Card } from "@/components/ui/card";

export default function Home() {
  return (
    <div className="mx-auto max-w-screen-xl xl:h-[50rem] px-4 xl:px-0 xl:grid xl:grid-flow-col xl:grid-rows-4 xl:gap-6">
      <div className="h-[20rem] xl:h-auto xl:row-span-2">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[20rem] xl:h-auto xl:row-span-2">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[40rem] xl:h-auto xl:row-span-4">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[10rem] xl:h-auto xl:row-span-1">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[30rem] xl:h-auto xl:row-span-3">
        <Card className="w-full h-full">hello</Card>
      </div>
    </div>
  );
}

然而,当我在其中添加内容时,我所做的形状会改变。

但是如果我添加宽度,它就不会改变:

import { Card } from "@/components/ui/card";

export default function Home() {
  return (
    <div className="mx-auto max-w-screen-xl xl:h-[50rem] px-4 xl:px-0 xl:grid xl:grid-flow-col xl:grid-rows-4 xl:gap-6">
      <div className="h-[20rem] xl:h-auto xl:row-span-2 xl:w-[27rem]">
        <Card className="w-full h-full overflow-auto">lorem1000</Card>
      </div>
      <div className="h-[20rem] xl:h-auto xl:row-span-2 xl:w-[27rem]">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[40rem] xl:h-auto xl:row-span-4 xl:w-[27rem]">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[10rem] xl:h-auto xl:row-span-1 xl:w-[27rem]">
        <Card className="w-full h-full">hello</Card>
      </div>
      <div className="h-[30rem] xl:h-auto xl:row-span-3 xl:w-[27rem]">
        <Card className="w-full h-full">hello</Card>
      </div>
    </div>
  );
}

是否有一种方法可以在不明确定义宽度,即w-[27rem]的情况下实现这一点,或者这是CSS的工作方式?

英文:

Here is the desired result with plain grid

为什么网格子元素的大小会根据内容而改变

original code:

import { Card } from &quot;@/components/ui/card&quot;;

export default function Home() {
  return (
    &lt;div className=&quot;mx-auto max-w-screen-xl xl:h-[50rem] px-4 xl:px-0 xl:grid xl:grid-flow-col xl:grid-rows-4 xl:gap-6&quot;&gt;
      &lt;div className=&quot;h-[20rem] xl:h-auto xl:row-span-2&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[20rem] xl:h-auto xl:row-span-2&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[40rem] xl:h-auto xl:row-span-4&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[10rem] xl:h-auto xl:row-span-1&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[30rem] xl:h-auto xl:row-span-3&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

However when i add content inside of it, the shape i made changes.

But if i add width it dose not

import { Card } from &quot;@/components/ui/card&quot;;

export default function Home() {
  return (
    &lt;div className=&quot;mx-auto max-w-screen-xl xl:h-[50rem] px-4 xl:px-0 xl:grid xl:grid-flow-col xl:grid-rows-4 xl:gap-6&quot;&gt;
      &lt;div className=&quot;h-[20rem] xl:h-auto xl:row-span-2 xl:w-[27rem]&quot;&gt;
        &lt;Card className=&quot;w-full h-full overflow-auto&quot;&gt;lorem1000&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[20rem] xl:h-auto xl:row-span-2 xl:w-[27rem]&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[40rem] xl:h-auto xl:row-span-4 xl:w-[27rem]&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[10rem] xl:h-auto xl:row-span-1 xl:w-[27rem]&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
      &lt;div className=&quot;h-[30rem] xl:h-auto xl:row-span-3 xl:w-[27rem]&quot;&gt;
        &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

is there a way to achieve this without me defining the width specifically, i.e w-[27rem] or is this how css works ?

答案1

得分: 1

你可以设置网格容器的列数,以确保它们保持像上面照片中的格式,然后你可以指定网格元素占据的列数。这基本上与你使用 row-span 做的事情一样:

<div className="mx-auto max-w-screen-xl xl:h-[50rem] px-4 xl:px-0 xl:grid xl:grid-flow-col xl:grid-rows-4 xl:gap-6 xl:grid-cols-3">
  <div className="h-[20rem] xl:h-auto xl:row-span-2 xl:col-span-1">
    <Card className="w-full h-full">hello</Card>
  </div>
  <div className="h-[20rem] xl:h-auto xl:row-span-2 xl:col-span-1">
    <Card className="w-full h-full">hello</Card>
  </div>
  <div className="h-[40rem] xl:h-auto xl:row-span-4 xl:col-span-1">
    <Card className="w-full h-full">hello</Card>
  </div>
  <div className="h-[10rem] xl:h-auto xl:row-span-1 xl:col-span-1">
    <Card className="w-full h-full">hello</Card>
  </div>
  <div className="h-[30rem] xl:h-auto xl:row-span-3 xl:col-span-1">
    <Card className="w-full h-full">hello</Card>
  </div>
</div>
英文:

You can set the grid container to have a number of columns to ensure that they stay formatted like you have in the photo above, and then you can dictate the number of columns that the grid elements take up. Its pretty much the same idea as what you've done with the row-span:

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

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

&lt;div className=&quot;mx-auto max-w-screen-xl xl:h-[50rem] px-4 xl:px-0 xl:grid xl:grid-flow-col xl:grid-rows-4 xl:gap-6 xl:grid-cols-3&quot;&gt;
  &lt;div className=&quot;h-[20rem] xl:h-auto xl:row-span-2 xl:col-span-1&quot;&gt;
    &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
  &lt;/div&gt;
  &lt;div className=&quot;h-[20rem] xl:h-auto xl:row-span-2 xl:col-span-1&quot;&gt;
    &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
  &lt;/div&gt;
  &lt;div className=&quot;h-[40rem] xl:h-auto xl:row-span-4 xl:col-span-1&quot;&gt;
    &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
  &lt;/div&gt;
  &lt;div className=&quot;h-[10rem] xl:h-auto xl:row-span-1 xl:col-span-1&quot;&gt;
    &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
  &lt;/div&gt;
  &lt;div className=&quot;h-[30rem] xl:h-auto xl:row-span-3 xl:col-span-1&quot;&gt;
    &lt;Card className=&quot;w-full h-full&quot;&gt;hello&lt;/Card&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定