Flexbox 孩子的身高不同

Idi*_*iot 4 html css alignment flexbox tailwind-css

我认为 flexbox 使其孩子具有相同的高度,这意味着所有孩子都将与最高(最高)孩子一样高,而父母通过为横轴设置默认值 align-stretch (如果它是 flex-row)来实现这一点。就我而言,情况并非如此。我有以下笔: Codepen 链接

<div class="flex w-full items-center flex-wrap">
   <div class="flex flex-col md:w-1/2 items-center px-16 py-20 bg-red">
      <h2 class="marjan-col">Smaller text</h2>
      <p>This one has smaller text</p>
   </div>
   <div class="flex flex-col md:w-1/2 items-center px-16 py-20 bg-blue">
      <h2 class="text-white">Bigger text that controls element height</h2>
      <p class="text-white">
         Blue element has more text thus making red element smaller and unable to fit entire height 
      </p>
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)
  • 请注意,较高的元素(背景红色)与蓝色元素的高度不同。

  • 我在这里使用 tailwind.css,但我认为代码是不言自明的。

kuk*_*kuz 9

items-center包装类中删除类- 这将添加align-items: center到您的flexbox并覆盖默认的拉伸行为 - 请参见下面的演示:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/0.7.4/tailwind.min.css">

<div class="flex w-full flex-wrap">
  <!-- CHANGED HERE -->

  <div class="flex flex-col md:w-1/2 items-center px-16 py-20 bg-red">
    <h2 class="marjan-col">Smaller text</h2>
    <p>This one has smaller text</p>
  </div>

  <div class="flex flex-col md:w-1/2 items-center px-16 py-20 bg-blue">
    <h2 class="text-white">Bigger text that controls element height</h2>
    <p class="text-white">
      Blue element has more text thus making red element smaller and unable to fit entire height
    </p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)