【问题标题】:Tailwind: equal height columns顺风:等高列
【发布时间】:2022-01-17 14:05:24
【问题描述】:

我有 3 列,每列包含人物图像、姓名、功能和报价。我希望所有 3 列的高度都相等。

到目前为止,我已经尝试设置 p class="flex-1" 什么也没做,我还将第二个内部 div 更改为 class="flex flex-1 p-6 gap-y-4"。这确实使列的高度相等,但它使 div 成为行而不是列,如果我将 flex-1 与 flex-col 一起使用,它不会设置列的高度相等。

如何在顺风不使用静态高度的情况下实现上述行为?

HTML

<div class="flex flex-col">
    <div class="w-full">
        <img class="h-full w-full" [src]="'/api'" alt="employee image">
    </div>
    <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300">
        <h3 class="text-3xl font-bold text-white">{{employee?.name}}</h3>
        <h4 class="text-lg uppercase text-black">{{employee?.function}}</h4>
        <p class="text-center text-xl font-bold text-black" [innerHtml]="employee?.quote"></p>
    </div>
</div>

【问题讨论】:

    标签: html css flexbox tailwind-css


    【解决方案1】:

    您可以使用flex 包装器而不是使用grid 并指定列数。

    <script src="https://cdn.tailwindcss.com"></script>
    
    <div class="flex gap-3">
      <div class="flex flex-col w-full">
        <div>
          <div class="h-20 bg-slate-400">Image</div>
        </div>
        <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
          <h3 class="text-3xl font-bold text-white">Employee Name</h3>
          <h4 class="text-lg uppercase text-black">Employee Function</h4>
          <p class="text-center text-xl font-bold text-black">Employee Quote</p>
        </div>
      </div>
      <div class="flex flex-col w-full">
        <div>
          <div class="h-20 bg-slate-400">Image</div>
        </div>
        <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
          <h3 class="text-3xl font-bold text-white">Employee Name</h3>
          <h4 class="text-lg uppercase text-black">Employee Function</h4>
          <p class="text-center text-xl font-bold text-black">Employee Quote</p>
        </div>
        <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
          <h3 class="text-3xl font-bold text-white">Employee Name</h3>
          <h4 class="text-lg uppercase text-black">Employee Function</h4>
          <p class="text-center text-xl font-bold text-black">Employee Quote</p>
        </div>
      </div>
      <div class="flex flex-col w-full">
        <div>
          <div class="h-20 bg-slate-400">Image</div>
        </div>
        <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
          <h3 class="text-3xl font-bold text-white">Employee Name</h3>
          <h4 class="text-lg uppercase text-black">Employee Function</h4>
          <p class="text-center text-xl font-bold text-black">Employee Quote</p>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      您应该使用实用程序类gridgrid-cols-3 来创建所需的布局。

      您可以在下面找到基于您发送的代码的代码示例。顺便说一句,对于将来您可能会提出的有关堆栈溢出的问题,请注意,良好的做法是共享实际有效的代码,即自包含的代码,最好是在代码 sn-p 中。因此,您的代码引用了未包含在您的代码中的图像和员工对象。

      <script src="https://cdn.tailwindcss.com"></script>
      
      <div class="grid grid-cols-3 gap-3">
        <div class="flex flex-col">
          <div>
            <div class="h-20 bg-slate-400">Image</div>
          </div>
          <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
            <h3 class="text-3xl font-bold text-white">Employee Name</h3>
            <h4 class="text-lg uppercase text-black">Employee Function</h4>
            <p class="text-center text-xl font-bold text-black">Employee Quote</p>
          </div>
        </div>
        <div class="flex flex-col">
          <div>
            <div class="h-20 bg-slate-400">Image</div>
          </div>
          <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
            <h3 class="text-3xl font-bold text-white">Employee Name</h3>
            <h4 class="text-lg uppercase text-black">Employee Function</h4>
            <p class="text-center text-xl font-bold text-black">Employee Quote</p>
          </div>
          <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
            <h3 class="text-3xl font-bold text-white">Employee Name</h3>
            <h4 class="text-lg uppercase text-black">Employee Function</h4>
            <p class="text-center text-xl font-bold text-black">Employee Quote</p>
          </div>
        </div>
        <div class="flex flex-col">
          <div>
            <div class="h-20 bg-slate-400">Image</div>
          </div>
          <div class="flex flex-col p-6 gap-y-4 text-center bg-indigo-300 flex-auto">
            <h3 class="text-3xl font-bold text-white">Employee Name</h3>
            <h4 class="text-lg uppercase text-black">Employee Function</h4>
            <p class="text-center text-xl font-bold text-black">Employee Quote</p>
          </div>
        </div>
      </div>

      编辑:根据 OP 的评论,我将 flex-auto 添加到员工卡中,以便在需要时可以增长以匹配其他列的高度。本质上,网格中的所有列都具有相同的高度,如果您希望相同的列占据所有可能的高度,您只需要让其内部元素增长。

      【讨论】:

      • 你说得对,我应该提到 html sn-p 是一个单独的组件,它加载到另一个组件中,该组件具有 3 列的网格布局,正如你已经建议的那样。也就是说,这是刚刚包装到网格中的完全相同的代码,并没有解决所提出的问题/问题。
      • 本质上,网格的所有元素都具有相同的高度,这就是为什么我认为我的回答就足够了。我编辑了我的答案,为每张卡片添加了 flex-auto 并在第二列中添加了一张卡片以说明每列如何具有相同的高度。
      猜你喜欢
      • 1970-01-01
      • 2021-10-30
      • 2020-10-19
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2020-12-04
      相关资源
      最近更新 更多