当前位置:网站首页>如何解决高度塌陷
如何解决高度塌陷
2022-08-11 01:51:00 【恁180】
什么是高度塌陷?
父元素的高度一般是被子元素或者叫内容撑开的,若设置子元素浮动,则子元素就会脱离文档流,也就撑不开父元素的高度了,从而导致父元素的高度丢失,页面布局混乱
如何解决高度塌陷?
方法一:
设置父元素的高度(这种方法一般不推荐使用)
例如不设置高度的情况下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.outer {
border: 10px red solid;
}
.inner {
width: 100px;
height: 100px;
background-color: blue;
float: left;
}
.box3 {
height: 100px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
<div class="box3"></div>
</body>
</html>
效果图:

这样蓝色盒子就脱离文档流了,页面布局就混乱了。
设置父元素高度:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.outer {
border: 10px red solid;
height: 100px;
}
.inner {
width: 100px;
height: 100px;
background-color: blue;
float: left;
}
.box3 {
height: 100px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
<div class="box3"></div>
</body>
</html>
效果图:

设置父元素高度就解决了高度塌陷
方法二:
页面元素中的隐含属性:Block Formatting Context 即块格式化上下文,简称BFC
当开启元素的BFC以后,元素会变成一个独立的布局区域,不会在布局上影响到外面的元素
BFC 理解为一个封闭的大箱子,箱子内部的元素不会影响到外部。
如何开启元素的BFC
1.设置元素浮动(不推荐)
- 使用这种方式开启,虽然可以撑开父元素,但是会导致父元素的宽度丢失
而且使用这种方式也会导致下边的元素上移,不能解决问题
2.设置元素为inline-block(不推荐)
- 可以解决高度丢失的问题,但是会导致宽度丢失,而且会有一个三像素的空白,不推荐使用这种方式
3.将元素的overflow设置为一个非visible的值
副作用比较小,推荐使用
4.设置元素绝对定位
元素也会脱离文档流,虽然可以撑开父元素,但是会导致父元素的宽度丢失
而且使用这种方式也会导致下边的元素上移,不能解决问题
推荐方式:将overflow设置为hidden是副作用最小的开启BFC的方式。
开启BFC后,元素将会具有如下的特性:
1.父元素的垂直外边距不会和子元素重叠(解决父子外边距重叠的问题)
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.outer {
width: 400px;
height: 300px;
background-color: #ccc;
}
.inner {
width: 100px;
height: 100px;
background-color: blue;
margin-top: 100px;
}
*/
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>效果图:
可以看到外边距重叠的问题并没有解决
若我们设置灰色的盒子浮动,就可以解决外边距重叠的问题
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.outer {
float: left;
width: 400px;
height: 300px;
background-color: #ccc;
}
.inner {
width: 100px;
height: 100px;
background-color: blue;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>效果图:
2.开启BFC的元素不会被浮动元素所覆盖
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.outer {
width: 400px;
height: 300px;
background-color: #ccc;
}
.inner {
width: 100px;
height: 100px;
background-color: blue;
}
.box3 {
width: 100%;
height: 100px;
background-color: yellow;
float: left;
}
</style>
</head>
<body>
<div class="box3"></div>
<div class="outer">
<div class="inner"></div>
</div>
<div class="box3"></div>
</body>
</html>如图所示:
蓝色的盒子就被覆盖了
我们可以设置元素为inline-block(不推荐)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.outer {
display: inline-block;
width: 400px;
height: 300px;
background-color: #ccc;
}
.inner {
width: 100px;
height: 100px;
background-color: blue;
}
.box3 {
width: 100%;
height: 100px;
background-color: yellow;
float: left;
}
</style>
</head>
<body>
<div class="box3"></div>
<div class="outer">
<div class="inner"></div>
</div>
<div class="box3"></div>
</body>
</html>如图所示:
3.开启BFC的元素可以包含浮动的子元素(可解决高度塌陷)
方法三:
可以直接在高度塌陷的父元素的最后,添加一个空白的块元素,由于这个块元素并没有浮动,所以他是可以撑开父元素的高度的,然后在对其进行清除浮动,这样可以通过这个空白的块元素来撑开父元素的高度,基本没有副作用。
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<style type="text/css">
.box1 {
border: 10px solid red;
}
.box2 {
width: 100px;
height: 100px;
background-color: blue;
float: left;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">box2</div>
</div>
</body>
</html>效果图:
我们设置一个空白的块元素
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<style type="text/css">
.box1 {
border: 10px solid red;
}
.box2 {
width: 100px;
height: 100px;
background-color: blue;
float: left;
}
.box3{
clear:both ;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">box2</div>
<div class="box3">box3</div>
</div>
</body>
</html>效果如图:
方法四:
可以通过after伪类向元素的最后添加一个空白的块元素,然后对其清除浮动, 这样做和添加一个div的原理一样,可以达到一个相同的效果,而且不会在页面中添加多余的div,这是我们最推荐使用的方式,几乎没有副作用
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<style type="text/css">
.box1 {
border: 10px solid red;
}
.box2 {
width: 100px;
height: 100px;
background-color: blue;
float: left;
}
.c::after {
content: "";
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="box1 c">
<div class="box2"></div>
</div>
</body>
</html>
这样也能解决高度塌陷的问题,这是我们最推荐使用的方式,几乎没有副作用
边栏推荐
猜你喜欢
随机推荐
络达开发---自定义BLE服务(二):功能实现
一次简单的 JVM 调优,拿去写到简历里
OpenWrt之opkg详解
漏洞管理计划的未来趋势
进程间通信(IPC)的分类以及通信方式的发展
leetcode 739. Daily Temperatures 每日温度(中等)
最新国产电源厂家及具体型号pin-to-pin替代手册发布
Js prototype and prototype chain and prototype inheritance
想进阿里?先来搞懂一下分布式事务
Deep Learning【第二章】
生信实验记录(part3)--scipy.spatial.distance_matrix
How to realize the repeatable design of FPGA
Construction inspection, no rules and no square
async和await的理解和用法
软件测试面试题:对 RUP,CMM,CMMI,XP,PSP,TSP 的认识?
划分字母区间[贪心->空间换时间->数组hash优化]
php 判断数组是否为多维数组
软件测试面试题:性能测试工作?
Data Filters in ABP
22、库存服务









