TailwindCSS 具有特定元素顺序的砌体

发布于 2025-01-11 17:30:22 字数 990 浏览 1 评论 0原文

我有一个包含 4 个元素的网格布局,这些元素应该在桌面和移动设备尺寸中占有特定的位置,除了砌体之外,它还可以。第三元素和第四元素并不靠近!

请参阅 jsfiddle 示例: https://jsfiddle.net/amnshojaei/0krpufxa/6/

<div class="grid grid-cols-12 gap-[24px]">
  <div class="h-[200px] col-span-full lg:col-start-1 lg:col-end-9 lg:row-start-1 lg:row-end-2">1</div>
  <div class="h-[200px] col-span-full lg:col-start-1 lg:col-end-9 lg:row-start-2 lg:row-end-3">2</div>
  <div class="h-[100px] col-span-full lg:col-start-9 lg:col-end-13 lg:row-start-1 lg:row-end-2">3</div>
  <div class="h-[100px] col-span-full lg:col-start-9 lg:col-end-13 lg:row-start-2 lg:row-end-3">4</div>
</div>

我想要有这样的东西带有自动高度元素输入图片此处描述

I have a grid layout with 4 elements that should have a specific place in desktop and mobile sizes and it's ok besides the masonry. the third and fourth elements are not near to each other!

see jsfiddle example: https://jsfiddle.net/amnshojaei/0krpufxa/6/

<div class="grid grid-cols-12 gap-[24px]">
  <div class="h-[200px] col-span-full lg:col-start-1 lg:col-end-9 lg:row-start-1 lg:row-end-2">1</div>
  <div class="h-[200px] col-span-full lg:col-start-1 lg:col-end-9 lg:row-start-2 lg:row-end-3">2</div>
  <div class="h-[100px] col-span-full lg:col-start-9 lg:col-end-13 lg:row-start-1 lg:row-end-2">3</div>
  <div class="h-[100px] col-span-full lg:col-start-9 lg:col-end-13 lg:row-start-2 lg:row-end-3">4</div>
</div>

I want to have something like this with auto height elements:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2025-01-18 17:30:22

您想要做的是通过使用列而不是行来打破网格布局。不幸的是这是不可能的。实现此目的的唯一方法是将内容放入两个“容器”列中,从而切断行连接。

这是最接近原始代码的方法:

<div class="p-[36px] grid grid-cols-12 gap-[24px]">
  <div class="col-span-8 space-y-[24px]">
    <div class="bg-gray-200 h-[200px] w-full">1</div>
    <div class="bg-gray-200 h-[200px] w-full">2</div>
  </div>
  <div class="col-span-4 space-y-[24px]">
    <div class="bg-gray-200 h-[100px] w-full">3</div>
    <div class="bg-gray-200 h-[100px] w-full">4</div>
  </div>
</div>

代码结果截图:

内容的屏幕截图,分成单独的 2:1 比例列

这里是相同的,但使用 Flexbox 而不是网格:

<div class="p-[36px] flex gap-[24px]">
  <div class="flex flex-col w-2/3 gap-[24px]">
    <div class="bg-gray-200 h-[200px] w-full">1</div>
    <div class="bg-gray-200 h-[200px] w-full">2</div>
  </div>
  <div class="flex flex-col w-1/3 gap-[24px]">
    <div class="bg-gray-200 h-[100px] w-full">3</div>
    <div class="bg-gray-200 h-[100px] w-full">4</div>
  </div>
</div>

代码结果截图:

按 2:1 比例分割成单独列的内容的屏幕截图

Tailwind 列(砖石布局)

如果您不想手动将您的内容分成不同的“容器”列,您可以使用 Tailwind 。这更像是真正的砖石布局,其中内容将自动流入 2 个(或更多)列。

不幸的是,使用这种方法,无法在原始代码中保持 2:1 的列宽比。列的宽度相等。

<div class="p-[36px] columns-2 gap-[24px] space-y-[24px]">
  <div class="bg-gray-200 h-[200px] w-full break-inside-avoid-column">1</div>
  <div class="bg-gray-200 h-[200px] w-full break-inside-avoid-column">2</div>
  <div class="bg-gray-200 h-[100px] w-full break-inside-avoid-column">3</div>
  <div class="bg-gray-200 h-[100px] w-full break-inside-avoid-column">4</div>
</div>

代码结果截图:

具有相同大小列的砌体布局的屏幕截图

What you're trying to do is break the grid layout by using the columns but not the rows. Unfortunately this is impossible. The only way you can achieve this is by putting the content into two "container" columns thereby severing the row connections.

Here is the method that is closest to your original code:

<div class="p-[36px] grid grid-cols-12 gap-[24px]">
  <div class="col-span-8 space-y-[24px]">
    <div class="bg-gray-200 h-[200px] w-full">1</div>
    <div class="bg-gray-200 h-[200px] w-full">2</div>
  </div>
  <div class="col-span-4 space-y-[24px]">
    <div class="bg-gray-200 h-[100px] w-full">3</div>
    <div class="bg-gray-200 h-[100px] w-full">4</div>
  </div>
</div>

Screenshot of code result:

screenshot of content split up into separate 2:1 ratio columns

Here is the same but using Flexbox instead of grid:

<div class="p-[36px] flex gap-[24px]">
  <div class="flex flex-col w-2/3 gap-[24px]">
    <div class="bg-gray-200 h-[200px] w-full">1</div>
    <div class="bg-gray-200 h-[200px] w-full">2</div>
  </div>
  <div class="flex flex-col w-1/3 gap-[24px]">
    <div class="bg-gray-200 h-[100px] w-full">3</div>
    <div class="bg-gray-200 h-[100px] w-full">4</div>
  </div>
</div>

Screenshot of code result:

screenshot of content split up into separate 2:1 ratio columns

Tailwind Columns (Masonry Layout)

If you don't want to manually break your content up into distinct "container" columns, you can use Tailwind columns. This is more like a true masonry layout where content will automatically flow into 2 (or more) columns.

Unfortunately, using this method, there is no way to maintain the 2:1 column width ratio in your original code. The columns will be equal widths.

<div class="p-[36px] columns-2 gap-[24px] space-y-[24px]">
  <div class="bg-gray-200 h-[200px] w-full break-inside-avoid-column">1</div>
  <div class="bg-gray-200 h-[200px] w-full break-inside-avoid-column">2</div>
  <div class="bg-gray-200 h-[100px] w-full break-inside-avoid-column">3</div>
  <div class="bg-gray-200 h-[100px] w-full break-inside-avoid-column">4</div>
</div>

Screenshot of code result:

screenshot of masonry layout with equally sized columns

嘿看小鸭子会跑 2025-01-18 17:30:22

Tailwind 的 columns 实用程序应该适合您的用例!

Tailwind's columns utility should work for your use case!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文