html在同级元素下方添加,CSS 同级元素position:fixed和margin-top共同使用的问题

本文探讨了CSS中position:fixed元素在遇到margin-top塌陷时的异常行为,解释了为何不按预期固定在顶部,并提供了通过设置top属性、调整body样式和解决margin-top问题的方法。阅读本文将帮助你避免position:fixed布局的常见陷阱。

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

问题描述

想用CSS实现顶部固定的效果:

d3cb9013e4bff15dfbbb9a922dedc5ef.png

尝试margin-top加position:fixed实现,代码如下:

Test

.header {

position: fixed;

height: 20px;

width: 100%;

}

.content {

margin-top: 30px;

}

.aside {

float: left;

width: 200px;

background: orange;

}

.main {

overflow: auto;

background: yellow;

}

123
aside
main

结果header没有定位在顶部,而是空出了content的margin-top距离:

3552f44cb462b323a696b2e3d9fd3254.png

按照position:fixed的定义,header已经脱离文档流,应该不会受到content布局影响,但结果并非如此。

问题探究

1.content的margin-top改为padding-top:可实现预期效果。

2.content同时设置margin-top和padding-top:仍会空出margin-top的距离。

3.body设置padding-top:会空出body的padding-top的距离,可实现预期效果。

4.body设置margin-top:会空出max(body->margin-top,content->margin-top)的距离。

5.给header设置top,如top: 0;:不受body和content的布局影响。

TBD:之后补充详细的测试代码和效果图( ̄∇ ̄)...

总结

归根结底是margin-top塌陷问题对position:fixed的影响。首先,对于position:fixed元素,如果不指定top,它在垂直方向上的参考原点是body盒模型的content的上边界。如果指定top,它在垂直方向上的参考原点才是我们常说的视窗(viewport)的上边界,left和水平方向同理。这里的参考原点是指fixed元素布局时的参考对象,一旦确定,即便页面被下拉,body上边界上移,fixed元素位置也不再改变。其次,因为margin-top塌陷问题,设置content的margin-top后,body的content部分会下移,即参考原点下移,所以fixed元素会空出margin-top的距离。

所以,可以从两方面解决这个问题:

1.将参考原点改为视窗:给fixed元素设置top。

2.解决margin-top塌陷问题,更多方法见下方链接:

1)给body设置padding-top。

2)给body设置border,border颜色和背景色一致。

3)给body设置position:fixed,这种会导致body的滚动条消失。

先将就看着呀~忙过这阵来完善(允悲)(允悲)。。。

TBD:content名字和盒模型content重啦待改...

不懂position:fixed?=> position|MDN

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

``` <view class="head"> <view class="tab-list2"> <view @tap="changeTab(1,1)" :class="['tab-item2','',activeTab1==1?'active':'']"> <text>待打印</text> </view> <!-- #ifdef MP-WEIXIN --> <view @tap="changeTab(2,1)" :class="['tab-item2','',activeTab1==2?'active':'']"> <text>已打印</text> </view> <!-- #endif --> <view @click="showDrawer('showRight')" :class="['tab-item2']" > <image class="icon" mode="aspectFit" src="@/static/image/search-icon.png"></image> </view> </view> <view class="tab-list " > <view v-for="(item,index) in order_status" @tap="changeTab(1,index)" :class="['tab-item','',activeTab2==index?'active':'']"> <uni-badge class="uni-badge-left-margin" max-num="99999" :is-dot="item.activeTabJiNum==0" :text="item.activeTabJiNum" absolute="rightTop" :offset="[-6, -3]" size="small" v-if="activeTab2==index"><text>{{item.text}}</text></uni-badge> <text v-else>{{item.text}}</text> </view> </view> <view class="tab-list " v-if="activeTab1==2"> <view v-for="(item,index) in team_status" @tap="changeTab(2,index)" :class="['tab-item','',activeTab2==index?'active':'']"> <uni-badge class="uni-badge-left-margin" max-num="99999" :is-dot="item.activeTabJiNum==0" :text="item.activeTabJiNum" absolute="rightTop" :offset="[-6, -3]" size="small" v-if="activeTab2==index"><text>{{item.text}}</text></uni-badge> <text v-else>{{item.text}}</text> </view> </view> </view> <view style="height: 90vh;background-color: rgb(243, 246, 249);flex: 1;z-index: 9999;display: flex;"> <view style="flex: 1;z-index: 9999;display: flex;">5555</view> </view> <view class="fixed-bottom"> <view style="display: flex;margin-left: 50rpx;float: left;margin-top: 20rpx;font-weight: bold;">打印机:</view> <view style="display: flex;float: right;margin-top: 20rpx;font-weight: bold;padding-right: 10rpx;">无打印机,去连接 ></view> <view style="margin-top: 100rpx;display: flex;margin-left: 70rpx;color: gainsboro;"> <checkbox-group> <label class="checkbox"> <checkbox :value="checked" :checked="checked" /> 全选 </label> </checkbox-group> </view> <view style="display: flex;margin-left: 340rpx;margin-top: -60rpx;"> <view class="btn">立即打印</view> </view> </view>```<view style="height: 90vh;background-color: rgb(243, 246, 249);flex: 1;z-index: 9999;display: flex;"> <view style="flex: 1;z-index: 9999;display: flex;">5555</view> </view> 这块我想让他在class="tab-list "的下面显示
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值