为什么父盒子加了 display: flex; flex-direction: column;这个display: inline-block就不生效了

在解释 align-self: flex-start 为什么能让子元素的 border-bottom 宽度与内容宽度一致前,先了解下弹性布局(Flexbox)的几个核心概念:
弹性布局基础概念
弹性容器(Flex Container):设置了 display: flex 或 display: inline-flex 的元素,它会创建一个新的弹性布局上下文。
弹性项目(Flex Items):弹性容器的直接子元素。
主轴(Main Axis):弹性项目排列的主要方向,由 flex-direction 属性决定,当 flex-direction 为 row 时,主轴是水平方向;为 column 时,主轴是垂直方向。
交叉轴(Cross Axis):与主轴垂直的方向。
分析你的情况
在你的代码里,父元素设置了 display: flex 和 flex-direction: column,这意味着父元素成为了弹性容器,子元素是弹性项目,且弹性项目沿着垂直方向排列,此时交叉轴是水平方向。
align-self 属性作用
align-self 属性用于为单个弹性项目在交叉轴上设置对齐方式,它可以覆盖弹性容器上 align-items 属性所设置的默认对齐方式。align-self 有多个取值,而 flex-start 表示让弹性项目在交叉轴上对齐到起始位置。
结合实际情况解释
在交叉轴方向上(这里是水平方向),默认情况下弹性项目会拉伸以填满交叉轴的可用空间。当你给子元素添加 align-self: flex-start 时,子元素就不再拉伸,而是根据自身内容的宽度来确定自身的宽度。
由于子元素宽度根据内容确定了,此时设置的 border-bottom 就只会在内容的宽度范围内显示,从而实现了 border-bottom 宽度与内容宽度一致的效果。
总结来说,align-self: flex-start 改变了子元素在交叉轴上的对齐和宽度表现,让其宽度由内容决定,进而影响了 border-bottom 的显示宽度。

<template> <div class="taskBox"> <div v-for="(item,index) in list" class="itemList"> <div class="top">【{{ item.userTypeName }}】{{ item.taskName }}</div> <div class="centre"> <p> <span><img :src="icon1"/></span> <span>起止时间</span> <span>:{{ item.startTime }}~{{ item.endTime }}</span> </p> <p> <span> <img :src="icon2"/></span> <span>适用范围</span> <span>:{{ item.useRange }}</span> </p> <p> <span> <img :src="icon3"/></span> <span>说明</span> <span>:{{ item.taskRemark }}</span> </p> </div> <div class="bottom"> <div class="statusBox statusBox1" v-if="item.status==&#39;正在进行&#39;">{{ item.status }}</div> <div class="statusBox statusBox2" v-else>{{ item.status }}</div> <div class="FillingBox" @click="goFillingFun(item.id)">快速填报&nbsp></div> </div> </div> </div> </template> <script setup> import {useRoute, useRouter} from &#39;vue-router&#39; import {post} from "@/frame/request/http"; import {ref} from "vue"; import icon1 from &#39;../../assets/image/taskFillingIcon1.png&#39; import icon2 from &#39;../../assets/image/taskFillingIcon2.png&#39; import icon3 from &#39;../../assets/image/taskFillingIcon3.png&#39; let router = useRouter() function goFillingFun(id) { router.push(&#39;/taskFilling?id=&#39; + id) } let list = ref([]) const search = ref({ pageNum: 1, pageSize: 2, total: 0, }); const currentDate = new Date(); // 状态判断函数 function getTaskStatus(startDateStr, endDateStr, currentDate) { // 将字符串转换为日期对象 const startDate = new Date(startDateStr); const endDate = new Date(endDateStr); // 清除时间部分,只比较日期 startDate.setHours(0, 0, 0, 0); endDate.setHours(0, 0, 0, 0); const current = new Date(currentDate); current.setHours(0, 0, 0, 0); if (current < startDate) { return "未开始"; } else if (current > endDate) { return "已过期"; } else { return "正在进行"; } } function getList() { post(&#39;/system/module/task/set/teacherPageList&#39;, search.value).then(res => { list.value = res.list list.value.forEach(task => { const status = getTaskStatus(task.startTime, task.endTime, currentDate); task.status = status; }); console.log(list.value, 78) }) } getList() </script> <style scoped lang="less"> .taskBox { height: 235px; width: 100%; display: flex; flex-wrap: wrap; gap: 24px; } .itemList { flex: 1; background-color: white; border-radius: 10px; padding: 24px; box-sizing: border-box; display: flex; flex-direction: column; justify-content: space-between; .top { font-size: 18px; font-weight: bolder; } .centre { height: 114px; width: 100%; background-color: #f7f9fc; padding: 20px; display: flex; flex-direction: column; justify-content: space-between; box-sizing: border-box; border-radius: 5px; p { display: flex; //flex-direction: column; //justify-content: center; span { &:nth-child(1) { display: flex; justify-content: center; align-items: center; img { width: 18px; height: 18px; margin-right: 5px; } } &:nth-child(2) { font-size: 14px; color: #666666; width: 72px; display: inline-block; text-align: justify; text-align-last: justify; } &:nth-child(3) { font-size: 14px; color: #333333; font-weight: 500; } } } } .bottom { display: flex; justify-content: space-between; .statusBox { width: 100px; height: 24px; line-height: 24px; display: flex; justify-content: center; color: white; border-radius: 5px; } .statusBox1 { background-image: linear-gradient(to right, #487ff7, #ffffff); } .statusBox2 { background-image: linear-gradient(to right, #cecece, #ffffff); } .FillingBox { color: #427bf7; cursor: pointer; } } } </style>优化一下,把适用范围和说明改成一行显示,超出显示省略号
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值