从Position:absolute到margin的合并

本文探讨了在CSS中,将父元素设置为Position:absolute如何影响元素的显示,尤其是margin的合并行为。通过实例展示了Position:absolute如何触发display:inline-block效果,并解释了如何阻止外边距折叠,涉及到BFC(块级格式化上下文)的概念。总结了position:absolute与display:inline-block触发BFC并阻止margin合并的现象。

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

转载请注明出处:http://blog.youkuaiyun.com/qq_31359337/article/details/60152277



问题起因:

在设置父元素Position:absolute之后发现了一些有趣的事情

Html & Css

<style type="text/css">
.a{
    position:absolute;
    background-color: #cad5eb;
}
</style>


<body>
    <p class="a">SOME</p>
</body>

效果:

初始的状态

当我们将Css代码改为:

<style type="text/css">
.a{
    background-color: #cad5eb;
}
</style>

效果:
这里写图片描述
在不加absolute时,有两种不同:

  1. a所占的是完整的一行
  2. 元素距离上边框的距离也有变化。(此时不太明显,稍后再看)

为什么?

一、通过网上的信息,发现Position:absolute是可以触发display:inline-block效果的,于是我把Css代码再次替换为:

.a{
    display:inline-block;
    background-color: #cad5eb;
}.a{
    background-color: #cad5eb;
}

效果:

发现用display:inline-block是可以实现和Position:absolute一样的效果的。
而display:inline-block会使元素以内联块状元素显示.
我们直观的上看两种盒子模型的区别:
1. 块状(Block)类型的元素可以设置width、height属性,而行内(Inline)类型设置无效。
2. 块状(Block)类型的元素会独占一行(直观的说就是会换行显示,无法与其他元素在同一行内显示,除非你主动修改元素的样式),而行内(Inline)类型的元素则都会在一行内显示。。
3. 块状(Block)类型的元素的width默认为100%,而行内(Inline)类型的元素则是根据自身的内容及子元素来决定宽度

因此可以理解第一处不同,因为使用inline-block后其大小与内部文字大小相符合。

二、第二处不同目前看来不明显,但是改动一处就会看起来很明显。
我在Chrome浏览器下使a的margin改为50px,且父元素body的margin-top变为50px,之后再执行

.a{
    display:inline-block;
    background-color: #cad5eb;
}.a{
    background-color: #cad5eb;
}

前后的到顶端的距离差距就很明显了。
不用inline-block:
这里写图片描述
使用inline-block:
这里写图片描述
那么我们可以推测这和margin有关,而这里又要提到BFC了,我们知道BFC可以阻止外边框折叠,具体如下说来:仅当两个块级元素相邻并且在同一个块级格式化上下文时,它们垂直方向之间的外边距才会叠加。也就是说,即便两个块级元素相邻,但当它们不在同一个块级格式化上下文时它们的边距也不会折叠。因此,阻止外边距折叠只需产生新的 BFC 。
而BFC是可以通过Position:absolute和Display:inline-block触发的,下面我们来验证。

.a{
    display: inline-block;
    margin: 50px;
    background-color: #cad5eb;
  }
body{
    margin-top: 50px;
    margin-bottom: 0px;
}
  1. 不用inline-block时(橙色区域为margin)
    1.父元素body的margin
    这里写图片描述
    2.子元素p的margin
    这里写图片描述
  2. 用inline-block时
    1.父元素body的margin
    这里写图片描述
    2.子元素p的margin
    这里写图片描述

经过对比可以发现当使用inline-block时,子元素框的margin-top和父元素的margin未合并,而是像图中相邻:
这里写图片描述
而未使用inline-block时,则会发生合并:
这里写图片描述

总结:
1. position:absolute导致dispaly:inline
2. position:absolute和display:lnline都会触发BFC
3. BFC可以阻止margin合并

参考:理解CSS外边距margin

``` <!DOCTYPE html> <html> <head> <title></title> <style> body { background-color: #000; margin: 0; height: 100vh; display: flex; justify-content: center; align-items: center; } #centerCrosshair { position: absolute; width: 20px; /* 十字线宽度 */ height: 20px; /* 十字线高度 */ top: 50%; left: 50%; transform: translate(-50%, -50%); } /* 使用伪元素创建水平和垂直线 */ #centerCrosshair::before, #centerCrosshair::after { content: ''; position: absolute; background-color: red; } /* 水平线 */ #centerCrosshair::before { width: 100px; /* 水平线长度 */ height: 3px; /* 水平线粗细 */ left: 50%; transform: translateX(-55%); } /* 垂直线 */ #centerCrosshair::after { width: 3px; /* 垂直线粗细 */ height: 100px; /* 垂直线长度 */ top: 50%; transform: translateY(-55%); } #movableCrosshair { position: absolute; width: 0; /* 容器尺寸清零 */ height: 0; transform: translate(-50%, -50%); /* 中心点对齐 */ top: calc(50% + 20px); /* 初始位置 */ left: calc(50% + 20px); } /* 水平线 */ #movableCrosshair::before { content: ''; position: absolute; width: 100px; /* 十字线水平长度 */ height: 2px; /* 线粗 */ background: #00f7ff; transform: translateX(-45%); /* 向左偏移一半宽度 */ } /* 垂直线 */ #movableCrosshair::after { content: ''; position: absolute; width: 2px; /* 线粗 */ height: 100px; /* 十字线垂直长度 */ background: #00f7ff; transform: translateY(-45%); /* 向上偏移一半高度 */ } </style> </head> <body> <div id="centerCrosshair"></div> <div id="movableCrosshair"></div> <script> document.addEventListener('keydown', function(event){ const crosshair = document.getElementById('movableCrosshair'); let style = window.getComputedStyle(crosshair); let topValue = parseInt(style.getPropertyValue('top')); let leftValue = parseInt(style.getPropertyValue('left')); switch (event.key) { case 'ArrowUp': crosshair.style.top = `${topValue - 1}px`; break; case 'ArrowDown': crosshair.style.top = `${topValue + 1}px`; break; case 'ArrowLeft': crosshair.style.left = `${leftValue - 1}px`; break; case 'ArrowRight': crosshair.style.left = `${leftValue + 1}px`; break; } }); </script> </body> </html>```将修改后的代码整体输出
03-09
<template> <view :class="{ inComeItemWrap: true, lastDomWrap: lastDom }"> <view :class="{ dot: true, activeC: data.assessState == 1 }" /> <view class="time flex"> {{ data.checkDate}} <view v-if="data.approveState == 0" class="status status1">未提交</view> <view v-if="data.approveState == 1" class="status status2">1审批中</view> <view v-if="data.approveState == 2" class="status status2">2审批通过</view> <view v-if="data.approveState == 3" class="status status2">3审批失败</view> </view> <view class="con"> <view class="conItem1"> <view class="conItemT">失败原因</view> <view class="conItemV">{{ incomeAmountStr }}</view> </view> </view> </view> </template> <script> import dayjs from "@/uni_modules/uview-ui/libs/util/dayjs.js"; export default { name: "inComeItem", props: { data: { type: Object, default: () => { return {}; }, }, lastDom: { type: Boolean, default: false, }, }, data() { return {}; }, computed: { incomeAmountStr() { let { data } = this return this.formatMoney(Math.floor(data.incomeAmount/100 || 0)).replace(/\d/g, '*'); } }, methods: { dayjs, }, }; </script> <style lang="scss" scoped> .inComeItemWrap { position: relative; // margin-left: 20rpx; padding-top: 40rpx; padding-bottom: 36rpx; border-left: 1px dashed #ccc; padding-left: 20rpx; } .lastDomWrap { border-left: 1px dashed #f3f5f8; } .dot { position: absolute; left: -13rpx; top: 0; width: 20rpx; height: 20rpx; border-radius: 20rpx; background: #05c4c8; } .time { position: absolute; left: 16rpx; top: -12rpx; font-size: 28rpx; color: #000; font-weight: bold; } .activeC { background: #05c4c8; } .con { padding-left: 30rpx; display: flex; .conItem1 { flex: 1; } .conItem2 { width: 60%; } .conItemT { margin-bottom: 4rpx; white-space: nowrap; color: #7c7c7c; font-size: 24rpx; } .conItemV { color: #000; font-weight: bold; font-size: 30rpx; } } .status1 { display: flex; align-content: center; align-items: center; font-weight: normal; font-size: 24rpx; padding: 0 8rpx; margin-left: 8rpx; color: #fff; background: #caa658; border-radius: 40rpx; } .status2 { display: flex; align-items: center; align-content: center; justify-content: center; font-weight: normal; padding: 0 12rpx; margin-left: 8rpx; background: #05c4c8; color: #fff; border-radius: 40upx; font-size: 24rpx; } .status3 { font-weight: normal; margin-left: 8rpx; position: relative; width: 204upx; /* 按钮宽度 */ height: 40upx; /* 按钮高度 */ border-radius: 40upx; /* 圆角 */ overflow: hidden; /* 隐藏超出边界的部分 */ } .status3::before, .status3::after { content: ""; position: absolute; width: 100%; height: 100%; } .statusText { display: flex; justify-content: center; align-items: center; position: absolute; left: 0; right: 0; top: 0; bottom: 0; text-align: center; font-size: 24rpx; z-index: 1; } /* 左侧颜色部分 */ .status3::before { background-color: #00d3d6; /* 青色 */ clip-path: polygon(0 0, 54% 0, 46% 100%, 0 100%); } /* 右侧颜色部分 */ .status3::after { background-color: #ffe6e6; /* 粉色 */ clip-path: polygon(54% 0, 100% 0, 100% 100%, 46% 100%); } </style> 优化一下样式
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值