当前位置:网站首页>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>
边栏推荐
- focal loss原理及实现
- Flex layout learning completed on PC side
- 软件测试面试题:常见的 POST 提交数据方式
- Codeforces Round #807 (Div. 2) A-C题解
- Behind every piece of information you collect, you can't live without TA
- Promise从入门到精通 (第1章 Promise的介绍和基本使用)
- Software testing interview question: what is the purpose of test planning? What are the contents of the test plan? Which are the most important?
- 2022 software testing skills robotframework + selenium library + Jenkins web Keyword Driven Automation practical tutorial
- Shell regular and metacharacters
- LeetCode 热题 HOT 100 -> 2.两数相加
猜你喜欢

产品解读丨MeterSphere UI测试模块的设计与分布式扩展

Go learn 02 basic knowledge

11-Django-基础篇-数据库操作

uniapp 总结篇 (小程序)

Traversal and properties of binary trees

Aike AI frontier promotion (7.14)

Vxe table/grid cell grouping and merging

Common problem types and methods of mathematical univariate differential proof problems in postgraduate entrance examination

Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials

Linux Installation mysql8.0.29 detailed tutorial
随机推荐
软考 --- 数据库(2)关系模型
focal loss原理及实现
C# 使用Abp仓储访问数据库时报错记录集
软件测试面试题:请详细介绍一下各种测试类型的含义?
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
C # using ABP warehouse to access the database error record set
Talk to ye Yanxiu, an atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
go 学习01
53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b
go 学习02 基础知识
软件测试面试题:你认为做好测试用例设计工作的关键是什么?
Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials
Under the new retail format, retail e-commerce RPA helps reshape growth
Flex布局学习完成PC端
【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串
SFTP file / folder upload server
Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
萤石网络,难当「孤勇者」
Codeforces Round #810 (Div. 2)A~C题解