<网页布局>解决margin-top塌陷,实现子元素动态改变父元素尺寸

本文介绍如何使用CSS伪类解决浮动元素引起的布局问题,包括margin-top塌陷及父元素高度自适应。

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

1.伪类解决margin-top塌陷:

如果两个不浮动的盒子相互嵌套,对内部的盒子设置margin-top会导致属性被自动转移到外部的盒子上,导致内部盒子的margin-top设置失败最靠谱的解决方式是为父元素添加一个伪类:
/*伪类解决margin-top塌陷*/
.clearfix:before{
    content:"";
    display: table;
}

解决margin-top塌陷
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>margin-top塌陷</title>
    <style>
        body, a, p{
            margin:0;
            padding:0;
        }
        .outside{
            width:300px;
            height: 400px;
            background-color: green;
            margin: 0 auto;
        }
        .inside{
            width: 50px;
            height:50px;
            background-color: gold;
            margin-top: 50px;
        }
        /*伪类解决margin-top塌陷*/
        .clearfix:before{
            content:"";
            display: table;
        }
    </style>
</head>
<body>

<div class="outside clearfix">

    <div class="inside">

    </div>

</div>

</body>
</html>

2.浮动子元素动态改变父元素宽度:

在设计网页布局时,为了便于快速布局,会将子元素设为浮动元素,父元素宽度设为固定,高度不设置,这样随着子元素的添加,父元素的高度就会动态变化,但尴尬的是,浮动子元素并不能"撑开父元素的高度",于是为父元素增加伪类便成了解决这种尴尬局面最好的方式;
.clearfix:after{
    content: "";
    display: table;

    clear:both;
}
子元素动态改变父元素尺寸
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>浮动子元素宽度</title>

    <style>

        .outside{
            width: 200px;
            border: 2px solid red;
        }
        .inside{
            float: left;

            width: 200px;
            height: 100px;
            margin: 20px 0 20px;
            border: 1px solid green;
        }
        .clearfix:after{
            content: "";
            display: table;

            clear:both;
        }
    </style>
</head>
<body>

<div class="outside clearfix">
    <div class="inside">
    </div>
    <div class="inside">
    </div>

</div>


</body>
</html>

小结:

以上两种情况极其常见,解决方式也很类似,所以可对两种方式做一个封装:

.clearfix:before, .clearfix:after{
    content: "";
    display: table;
}

.clearfix:after{

    clear:both;
}

/*适配非主流浏览器(IE浏览器)*/

.clearfix{

    zoom:1;

}

以后遇到上面两种问题,只要将 小结 中的源码引入到对应的css(层叠样式表),最后在父元素class属性中 中引入clearfix即可


<div class="outside clearfix">
    <div class="inside">
    </div>
    <div class="inside">
    </div>

</div>
reset.css
h1,h2,h3,h4,h5,h6,p,body,ul,ol,dl,dd,dt,input{
    /*去掉默认样式*/
    margin: 0;
    padding: 0;
}

ul,ol{
    /*去掉左边的点或者数字*/
    list-style: none;
}

a{
    /*去掉下划线*/
    text-decoration: none;
}

em,i{
    /*去掉斜体*/
    font-style: normal;
}

b,strong{
    /*去掉加粗*/
    font-weight: normal;
}


/*清除浮动影响 和清除margintop塌陷 合在一起的写法*/
.clearfix:before,.clearfix:after{
    content: "";
    display: table;
}

.clearfix:after{
    clear: both;
}

.clearfix{
    /*兼容ie*/
    zoom:1;
}

``` <div style=" float: left; width: 50%; height: 500px;background-color: white;border-radius: 10px ;margin-top: 10px "> <div style="width: 5px;height: 25px;background-color: #1f78ff;margin-top: 22px;margin-left: 10px;border-radius: 10px;float: left"> </div> <div style="float: left;margin-left: 10px;margin-top: 20px;font-size: 20px;font-weight: bold"> 今日有效单量:{{ userinfo.today_order || '0' }}单 </div> <div style="float: right;font-size: 14px;color: gray;padding-top: 20px;padding-right: 20px">更新时间:{{ userinfo.nowtime }} <button @click="showdrawer" type="primary" style="color: rgb(64,158,255);background-color: white;border: none">数据表格 ></button> </div> <div style="clear: both"></div> <div style="float: left;margin-left: 8px;margin-top: 10px"> 日期A:<span style="color: rgb(64,158,255);font-weight: bold">{{ this.totalBa }}</span>单 日期B:<span style="color: rgb(64,158,255);font-weight: bold">{{ this.totalAa }}</span>单 升幅: <span style="font-weight: bold">{{ this.totalBa - this.totalAa }}</span>单</div> <div style="clear: both"></div> <div style="float: left;margin-left: 8px;padding-top: 20px"> 对比日期A: </div> <div style="clear: both"></div> <div style="float: left;margin-left: 106px;margin-top: -26px"> <el-date-picker value-format="yyyy-MM-dd" style="width: 140px;" v-model="timeA" type="date" @change="getOrderChart" placeholder="选择日期"> </el-date-picker> </div> <div style="clear: both"></div> <div style="float: left;margin-left: 260px;margin-top: -32px"> 对比日期B: </div> <div style="clear: both"></div> <div style="float: left;margin-left: 360px;margin-top: -36px"> <el-date-picker value-format="yyyy-MM-dd" style="width: 150px;" v-model="timeB" type="date" @change="getOrderChart" placeholder="选择日期"> </el-date-picker> </div> <div style="margin-top: -36px;float: right;margin-right: 150px"> <el-select style="width: 120px" v-model="current_time" placeholder="当前对比" @change="getOrderChart"> <el-option v-for="item in option_time" :label=item.label :value="item.value"> </el-option> </el-select> </div> </div> <div style="clear: both"></div> <div style="background-color: #FFFFFF;float: right;sborder-radius: 10px;margin-top: -29%;width: 30%"> <div style="width: 5px;height: 25px;background-color: #1f78ff;margin-top: 32px;margin-left: 10px;border-radius: 10px;float: left"> </div> <div style="float: left;margin-left: 10px;margin-top: 30px;font-size: 20px;font-weight: bold"> 我的寄件数量 </div> <div style="clear: both"></div> <div style="float: left;margin-top: 20px;font-size: 14px;color: gray;padding-left: 10px"> 更新时间:{{ userinfo.nowtime }} </div> <div style="clear: both"></div> <div ref="chinaMap" style="width: 600px; height: 400px;background-color: white"> </div> <div style="clear: both"></div> <div style="float: right;margin-top:-470px;margin-right: 30px"> <el-radio-group v-model="radio1"> <el-radio-button label="今日"></el-radio-button> <el-radio-button label="昨日"></el-radio-button> <el-radio-button label="本周"></el-radio-button> </el-radio-group> </div> </div>```<div style="margin-top: -36px;float: right;margin-right: 150px"> <el-select style="width: 120px" v-model="current_time" placeholder="当前对比" @change="getOrderChart"> <el-option v-for="item in option_time" :label=item.label :value="item.value"> </el-option> </el-select> </div> <div style="background-color: #FFFFFF;float: right;sborder-radius: 10px;margin-top: -29%;width: 30%">窗口缩小后这俩块地方位置错乱,要求在我的代码基础上改动
03-20
``` <template> <view class="find-container "> <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="width: 100%;height: 100%;background-color: red;"> 5555 </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> <uni-calendar ref="calendar" class="uni-calendar--hook" :clear-date="true" :date="info.date" :insert="false" :lunar="true" :range="true" @confirm="confirm" @close="calendarClose" /> <uni-drawer ref="showRight" mode="right" :mask-click="true" width="300" @change="drchange($event,'showRight')"> <view class="scroll-view"> <scroll-view class="scroll-view-box" scroll-y="true"> <view class="info"> <input class="ipt"placeholder="订单号/快递单号/手机号/寄件人/收件人" placeholderStyle="font-size: 24rpx;" type="text" v-model="mailNo"></input> </view> <view class="close"> <button type="primary" @click="closeDrawer('showRight')"><text class="word-btn-white">搜索</text></button> </view> </scroll-view> </view> </uni-drawer> </view> </template>```<view style="width: 100%;height: 100%;background-color: red;"> 5555 </view>这个代码没有任何改变页面上
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值