如何在使用CSS Grid时设置跨多行的列的高度不确定?

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

How do I set an indeterminate height on a column that spans multiple rows with CSS Grid?

问题

I'm using Tailwind CSS and attempting to use a grid layout which looks like below:

如何在使用CSS Grid时设置跨多行的列的高度不确定?

我正在使用Tailwind CSS并尝试使用以下网格布局:

如何在使用CSS Grid时设置跨多行的列的高度不确定?

I have some dashboard-type metrics in Div 1. Now, when I add a very long ordered list to Div 2, for some reason, it's stretching Div 1's height to (what appears to me) to be the overall height of Div 2.

Hopefully this screenshot makes clear what is happening. You can see the height of that top Div (Div 1) is enormous after adding the content to the right Div column:

如何在使用CSS Grid时设置跨多行的列的高度不确定?

我在Div 1中有一些仪表板类型的指标。现在,当我将一个非常长的有序列表添加到Div 2时,由于某种原因,它会拉伸Div 1的高度,似乎是Div 2的整体高度。

希望这个屏幕截图能清楚地说明发生了什么。您可以看到在将内容添加到右侧Div列后,顶部Div(Div 1)的高度巨大:

如何在使用CSS Grid时设置跨多行的列的高度不确定?

The code I've used is basically copy/pasted directly from a Tailwind Grid generator as shown below:

我使用的代码基本上是直接从Tailwind Grid生成器复制/粘贴而来,如下所示:

1
2
3
4
5
6

I've simply placed my own content in their respective Div's, but it's behaving unexpectedly.

我只是将自己的内容放在它们各自的Div中,但它的行为出乎意料。

My goal is to basically have all Div's in the Grid have a height that is as big as the content in its respective Div.

我的目标基本上是使网格中的所有Div的高度与其各自Div中的内容一样大。

EDIT: Similar to how this pen works. Although the pen uses custom CSS stylings. Ideally, I'd like to use the Tailwind CSS classes to achieve the same result.

编辑:类似于此笔的工作原理。尽管该笔使用了自定义CSS样式,但我理想情况下希望使用Tailwind CSS类来实现相同的结果。

编辑:类似于此笔的工作原理。尽管该笔使用了自定义CSS样式,但我理想情况下希望使用Tailwind CSS类来实现相同的结果。

You can refer to the provided CodePen link for an example of how to achieve your goal using custom CSS stylings. However, if you want to use Tailwind CSS classes, you may need to adjust your grid layout and styles accordingly.

您可以参考提供的CodePen链接,了解如何使用自定义CSS样式来实现目标。不过,如果您想使用Tailwind CSS类,可能需要相应地调整您的网格布局和样式。

英文:

I'm using Tailwind CSS and attempting to use a grid layout which looks like below:

如何在使用CSS Grid时设置跨多行的列的高度不确定?

I have some dashboard-type metrics in Div 1. Now, when I add a very long ordered list to Div 2, for some reason, it's stretching Div 1's height to (what appears to me) to be the overall height of Div 2.

Hopefully this screenshot makes clear what is happening. You can see the height of that top Div (Div 1) is enormous after adding the content to the right Div column:

如何在使用CSS Grid时设置跨多行的列的高度不确定?

The code I've used is basically copy/pasted directly from a Tailwind Grid generator as shown below:

<div class="grid grid-cols-3 grid-rows-4 gap-4">
    <div class="col-span-3">1</div>
    <div class="row-span-3 col-start-3 row-start-2">2</div>
    <div class="col-start-1 row-start-2">3</div>
    <div class="col-start-2 row-start-2">4</div>
    <div class="col-span-2">5</div>
    <div class="col-span-2 row-start-4">6</div>
</div>

I've simply placed my own content in their respective Div's, but it's behaving unexpectedly.

My goal is to basically have all Div's in the Grid have a height that is as big as the content in its respective Div.

EDIT: Similar to how this pen works. Although the pen uses custom css stylings. Ideally, I'd like to use the Tailwind CSS classes to achieve the same result.

https://codepen.io/source-matters/pen/YzJaBRL?editors=1100

答案1

得分: 2

你所看到的行为是因为你通过grid-rows-4应用了grid-template-columns: repeat(4, 1fr)。这会告诉布局每个轨道应该是高度的四分之一。所以如果最后三个行轨道因内容而变高,这会拉长第一行轨道,使其符合四分之一高度的规定。相反,你可以考虑告诉布局,第一行轨道根本不应该是总高度的一部分:

<div class="grid grid-cols-3 grid-rows-[auto_repeat(3,1fr)] gap-4">
    <div class="col-span-3 bg-red-200">1</div>
    <div class="row-span-3 col-start-3 row-start-2 bg-green-200">
      <div class="h-96">Foo</div>
    </div>
    <div class="col-start-1 row-start-2 bg-yellow-200">3</div>
    <div class="col-start-2 row-start-2 bg-violet-200">4</div>
    <div class="col-span-2 bg-blue-200">5</div>
    <div class="col-span-2 row-start-4 bg-sky-200">6</div>
</div>

希望这有帮助。

英文:

The behavior you are seeing is because you have applied grid-template-columns: repeat(4, 1fr) via grid-rows-4. This instructs the layout that each track should be ¼ of the height. So if the last three row tracks are tall because of content, this will elongate the first row track so that it adheres to the ¼-height stipulation. Instead, you could consider telling the layout that the first row track should not be a fraction of the overall height at all:

<!-- 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;div class=&quot;grid grid-cols-3 grid-rows-[auto_repeat(3,1fr)] gap-4&quot;&gt;
    &lt;div class=&quot;col-span-3 bg-red-200&quot;&gt;1&lt;/div&gt;
    &lt;div class=&quot;row-span-3 col-start-3 row-start-2 bg-green-200&quot;&gt;
      &lt;div class=&quot;h-96&quot;&gt;Foo&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-start-1 row-start-2 bg-yellow-200&quot;&gt;3&lt;/div&gt;
    &lt;div class=&quot;col-start-2 row-start-2 bg-violet-200&quot;&gt;4&lt;/div&gt;
    &lt;div class=&quot;col-span-2 bg-blue-200&quot;&gt;5&lt;/div&gt;
    &lt;div class=&quot;col-span-2 row-start-4 bg-sky-200&quot;&gt;6&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 10:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214593.html
匿名

发表评论

匿名网友

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

确定