Tailwind: equal height columns

Viewed 954

I have 3 columns that each contain a persons image, name, function and a quote. I want all 3 columns to be equal in height.

So far i have tried setting p class="flex-1" which did nothing, i also changed the 2nd inner div to class="flex flex-1 p-6 gap-y-4". This did make the columns equal in height, but it made the div a row instead of a column, if i use flex-1 together with flex-col it does not set the columns equal in height.

How can achieve said behaviour without using a static height in tailwind?

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>
2 Answers

You should use utility classes grid and grid-cols-3 to create desired layout.

You can find below a code example based on the code you sent. By the way, for future questions you might ask on stack overflow, please note that the good practice is to share code that actually works, ie self-contained, preferably in a code snippet. As such, your code references an image and employee object which are not contained in your code.

<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>

EDIT: Following OP's comment, I added flex-auto to the employee card so that it can grows if needed to match the height of other columns. Intrinsically, all columns in a grid have same height, you only need to let its inner elements grow if you want same to take all possible height.

You can use a flex wrapper instead of using grid and specifying columns count.

<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>

Related