父元素内多个子元素,子元素根据内容确定高度,子元素和最高的子元素等高

在CSS布局中,当父元素内的多个子元素根据内容动态调整高度时,通常难以实现所有子元素与最高子元素保持等高。通过采用Flex布局,可以简便地解决这一问题,使得子元素能自动拉伸至与内容最多的子元素相同的高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

父元素内多个子元素,子元素根据内容确定高度,子元素和最高的子元素等高

父元素中有多个子元素,父元素高度不确定,子元素高度不确定以内容的高度确定,但是这时候要求子元素等高,也就是按照子元素中内容最多的那个元素的高度确定所有元素的高度,这种需求以普通的布局是很难实现的.

<body>
  <div class="father">
    <div class="child"><span>hello</span><br><span>hello</span><br><span>hello</span><br></div>
    <div class="child"><span>hello</span><br><span>hello</span><br></div>
    <div class="child"><span>hello</span><br></div>
    <div class="child"></div>
  </div>
</body>
<style>
  .father {
    display: inline-block;
    background-color: deepskyblue;
  }

  .child {
    vertical-align: top;
    display: inline-block;
    margin-left: 10px;
    background-color: red;
  }
</style>

在这里插入图片描述

这种情况下要实现三者登高,很麻烦,因为无法使用height:100%,因为父元素高度.但是用flex布局就很高解决,设置子元素align-items:stretch,也就是拉升子元素到父元素高度

<body>
  <div class="father">
    <div class="child"><span>hello</span><br><span>hello</span><br><span>hello</span><br><span>hello</span><br></div>
    <div class="child"><span>hello</span><br><span>hello</span><br></div>
    <div class="child"><span>hello</span><br></div>
    <div class="child"></div>
  </div>
</body>
<style>
  .father {
    display: flex;
    background-color: deepskyblue;
  }

  .child {
    display: inline-block;
    align-items: stretch;
    vertical-align: top;
    margin-left: 10px;
    background-color: red;
  }
</style>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值