flex布局实战之自动填充剩余

案例目标

文字部分自适应并且居中

在这里插入图片描述

图中是一个弹窗,我现在使用flex的布局来实现,标题和关闭按钮。因为是uni-app,所以标签是view 。你可以自行替换为

代码

<view class="popup-box">
    <view class="title">
        <view class="text">报案成功</view>
        <image style="width: 32rpx;" mode="widthFix" src="close-icon.png"></image> 
    </view> 
</view>
.popup-box{
  width: 80vw;
  margin: 0 auto;
  .title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    .text{
      text-align: center;
      flex: auto;
    }
  }
}

实现效果如下:

在这里插入图片描述

总结

这里的title 文字部分是自适应剩余宽度的。想要自适应剩余宽度的话,需要满足以下条件:

  • 父级dispalay : flex;
  • 其中一个子级的宽度或者高度为固定。
  • 另外一个子级的 flex: auto;

案例二:子级宽度超过父级

<!DOCTYPE html>
<html>
<head>
  <style>
    .parent {
      display: flex;
      flex-direction: row;
      width: 500px; /* 可以根据需要设置父级div的宽度 */
      height: 200px;
      background-color: lightgray;
    }

    .child1 {
      width: 100px;
      flex-shrink: 0;/*防止第二个子div的内容超出了父级div的宽度,其在空间不足时缩小*/
      background-color: red;
    }

    .child2 {
      flex-grow: 1;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div class="parent">
    <div class="child1"></div>
    <div class="child2">
        很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》很多汉字》
    </div>
  </div>
</body>
</html>

当第二个子div的内容超出了父级div的宽度时,父级div的剩余空间将被第二个子div占据,导致第一个子div的宽度变小。
在这里插入图片描述

此时要将第一个子div的flex-shrink属性设置为0,可以防止其缩小。

.child1 {
     flex-shrink: 0;/*防止第二个子div的内容超出了父级div的宽度,其在空间不足时缩小*/
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

usp1994

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值