当前位置:网站首页>Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
2022-07-28 02:15:00 【Xiao Lin loves to share】
problem :
When using floating to adjust the style , The area where the float is located will not occupy a position in the page ( In the air , Not occupy the actual position ), The problem is as follows , It will cause the lower element block to top directly , Can not achieve the effect of separating up and down :
meaning : Remove the effect of floating
~ influence : If the child element floats , At this time, the child element cannot open the block level parent element of the standard flow
~ reason : The sub element floats and is de labeled → Don't occupy a place
~ Purpose : The parent element is required to have a height , So as not to affect the layout of other web page elements
Method :
① Set the height of the parent element directly
characteristic :
• advantage : Simple and crude , convenient
• shortcoming : In some layouts, the height of the parent element cannot be fixed . Such as : News list 、 JD recommends modules
Because the height of the content is uncertain , Easy enough or leave blank , It will not be used in practice .
H5
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
style
<style>
.father {
/* height: 400px; */ If it is not set , The parent element cannot be displayed , The height of the parent element will be 0, Not supported by child elements
width: 400px;
background-color: pink;
}
.son {
float: left;
width: 100px;
height: 400px;
background-color: blue;
}
</style>
② Extra label method
• operation :
- Add a block level element at the end of the parent element content
- Set... For the added block level element clear:both
• shortcoming : Additional tags will be added to the page , Will make the page HTML The structure becomes complicated
H5:
<div class="father">
<div class="son"></div>
<div class="abc"></div>
</div>
style :
<style>
.father {
width: 400px;
background-color: pink;
}
.son {
float: left;
width: 100px;
height: 400px;
background-color: blue;
}
.abc{
clear: both;
}
</style>
③ Single pseudo element elimination method

H5 in :
<div class="father clearfix"> Set him two class names , At the same time
<div class="son"></div>
</div>
Subsequent classes include clearfix Can
<div class="clearfix"></div>
<div class="clearfix"></div>
<div class="clearfix"></div>
<div class="clearfix"></div>
In the style :
<style>
.father {
width: 400px;
background-color: pink;
}
.son {
float: left;
width: 100px;
height: 400px;
background-color: blue;
}
.clearfix::after { /*clearfix Class is set here */
content: ''; /* Is a pseudo element must be added , It can be set to null */
display: block;
clear: both; /* It is equivalent to extracting the previous method */
/* Supplementary code : You can't see pseudo elements in the web page */
height: 0;
visibility: hidden;
}
</style>
④ Double pseudo element elimination

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father {
width: 400px;
background-color: pink;
}
.son {
float: left;
width: 100px;
height: 400px;
background-color: blue;
}
.clearfix::before, /* It can solve the collapse problem of the upper part of the box */
.clearfix::after {
content: '';
display: table;
}
.clearfix::after {
clear: both;
}
</style>
</head>
<body>
<div class="father clearfix">
<div class="son"></div>
</div>
</body>
</html>
⑤ Set the parent element overflow : hidden
H5
<div class="father">
<div class="son"></div>
</div>
style :
<style>
.father {
width: 400px;
background-color: pink;
overflow: hidden; /* Operate directly on the parent element */
}
.son {
float: left;
width: 100px;
height: 400px;
background-color: blue;
}
</style>
⑥BFC The box ( understand )

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father {
/* 1、 hold father Box converted to BFC The box , solve : Remove the floating */
/* float: left; */
/* display: inline-block; */
overflow: hidden;
width: 400px;
background-color: pink;
}
.son {
float: left;
width: 100px;
height: 400px;
background-color: blue;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
边栏推荐
- FreeRTOS kernel summary
- 云原生爱好者周刊:Prometheus 架构演进之路
- 软件测试面试题:你认为做好测试用例设计工作的关键是什么?
- Principle and implementation of focal loss
- Vxe Table/Grid 单元格分组合并
- Go learn 02 basic knowledge
- Software test interview question: please introduce the meaning of various test types in detail?
- MySQL的pymysql操作
- Common video resolution
- Uniapp summary (applet)
猜你喜欢

In it, there is a million talent gap, and the salary rises, but it is not capped

C# 使用Abp仓储访问数据库时报错记录集

Go learning 01
![[Taichi] draw a regular grid in Tai Chi](/img/48/14e825562afa3ffba96296799617f7.png)
[Taichi] draw a regular grid in Tai Chi

考研数学一元微分学证明题常见题型方法

Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework

Promise from getting started to mastering (Chapter 3: customize (handwriting) promise)

HyperMesh circular array - plug in

MySQL高可用和主从同步

Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
随机推荐
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Redis设计规范
Gbase 8C recovery control function
Sample imbalance - entry 0
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
记录一次生产死锁
执行 Add-Migration 迁移时报 Build failed.
Cloud native enthusiast weekly: the evolution of Prometheus architecture
WMS you don't know
Aike AI frontier promotion (7.14)
损失函数-交叉熵的原理及实现
synchronized详解
Record a production deadlock
Promise从入门到精通 (第2章 Promise的理解和使用)
C# 使用Abp仓储访问数据库时报错记录集
Interviewer: are you sure redis is a single threaded process?
Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
考研数学一元微分学证明题常见题型方法
[Taichi] draw a regular grid in Tai Chi
Codeforces Round #807 (Div. 2) A-C题解