css 当一个项目大于一列时,如何计算网格宽度?

zaqlnxep  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(151)

我有一个图像大小的网格。其尺寸可以减小和扩大

我有6个项目在网格中。

每个项目都大于网格的列。
我想设置如图所示的网格宽度。我应该怎么计算?

tailwind.config = {
  theme: {
    extend: {
      zIndex: {
        1: 1,
        2: 2,
        3: 3,
        4: 4,
        5: 5,
        6: 6,
        7: 7,
        8: 8,
        9: 9,
      }
    }
  }
}
<script src="https://cdn.tailwindcss.com"></script>

<div class="w-64 bg-red-200 p-4">
  <div class="grid grid-cols-6 relative">
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-10">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-9">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-8">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-7">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-6">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-5">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
  </div>
</div>
w46czmvw

w46czmvw1#

您可以考虑通过Tailwind的w-max应用width: max-contentmax-content关键字“表示内容的固有最大宽度或高度”。

tailwind.config = {
  theme: {
    extend: {
      zIndex: {
        1: 1,
        2: 2,
        3: 3,
        4: 4,
        5: 5,
        6: 6,
        7: 7,
        8: 8,
        9: 9,
      }
    }
  }
}
<script src="https://cdn.tailwindcss.com"></script>

<div class="w-max bg-red-200 p-4">
  <div class="grid grid-cols-6 relative">
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-10">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-9">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-8">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-7">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-6">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
    <div class="absolute w-20 aspect-[2/3] rounded-sm inline-block overflow-hidden relative z-5">
      <img src="https://www.themoviedb.org/t/p/original/wZp8xcADdOZ0FK6FEEtrKXjjdKJ.jpg" alt="" class="w-full h-full">
    </div>
  </div>
</div>

相关问题