当前位置:网站首页>Interview question: outer margin folding problem (bug of block level elements in ordinary document flow)
Interview question: outer margin folding problem (bug of block level elements in ordinary document flow)
2022-07-19 08:08:00 【The.. Fuir】
One 、 Concept
Generally, it refers to the overlapping phenomenon of adjacent outer margins in the vertical direction , Most of it happens in
Brother elementandFather and son elementsBetween .Two 、 Brother element
box2 yes box1 Brother element of
When both are positive : Take the maximum value between the two
<!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> </head> <body> <style> .box1{ width: 200px; height: 200px; background-color: tomato; margin-bottom:50px; } .box2{ width: 200px; height: 200px; background-color: #bfa; margin-top:100px; } </style> <body> <div class="box1"></div> <div class="box2"></div> </body> </body> </html>
One is one minus one. : Take the sum of the two
<!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> </head> <body> <!-- When both are positive : Take the maximum value between the two <style> .box1{ width: 200px; height: 200px; background-color: tomato; margin-bottom:50px; } .box2{ width: 200px; height: 200px; background-color: #bfa; margin-top:100px; } </style> --> <style> .box1{ width: 200px; height: 200px; background-color: tomato; margin-bottom:-50px; } .box2{ width: 200px; height: 200px; background-color: #bfa; margin-top:50px; } </style> <body> <div class="box1"></div> <div class="box2"></div> </body> </body> </html>
Both are negative : Take the larger absolute value
<!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> </head> <body> <!-- When both are positive : Take the maximum value between the two <style> .box1{ width: 200px; height: 200px; background-color: tomato; margin-bottom:50px; } .box2{ width: 200px; height: 200px; background-color: #bfa; margin-top:100px; } </style> --> <!-- One is one minus one. : Take the sum of the two <style> .box1{ width: 200px; height: 200px; background-color: tomato; margin-bottom:-50px; } .box2{ width: 200px; height: 200px; background-color: #bfa; margin-top:50px; } </style> --> <style> .box1{ width: 200px; height: 200px; background-color: tomato; margin-bottom:-50px; } .box2{ width: 200px; height: 200px; background-color: #bfa; margin-top:-100px; } </style> <body> <div class="box1"></div> <div class="box2"></div> </body> </body> </html>
3、 ... and 、 Father and son elements
box2 yes box1 Child elements
<!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> </head> <body> <style> .box1{ width: 200px; height: 200px; background-color: tomato; } .box2{ width: 100px; height: 100px; background-color: #bfa; } </style> <body> <div class="box1"> <div class="box2"></div> </div> </body> </html> </body> </html>
- When box2 The upper and outer margins of are set to 100:
- At this time, the parent element will also move down , namely : The outer margin of the child element is passed to the parent element .
Four 、 Four solutions
1. Add an inner margin to the parent element , Then cut off the extra length
<!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> </head> <body> <!-- <style> .box1{ width: 200px; height: 200px; background-color: tomato; } .box2{ width: 100px; height: 100px; background-color: #bfa; margin-top:100px; } </style> --> <!-- Use padding There will be no margin folding problem --> <style> .box1{ width: 200px; height: 100px; padding-top:100px; background-color: tomato; } .box2{ width: 100px; height: 100px; background-color: #bfa; } </style> <body> <div class="box1"> <div class="box2"></div> </div> </body> </html> </body> </html>2. Use outer margins but not adjacent , Add a border to the parent element , Then change the border color to the color of the parent element , Then cut the extra border length :
<style> .box1{ width: 200px; height: 199px; background-color: tomato; border-top: 1px solid tomato; } .box2{ width: 100px; height: 99px; background-color: #bfa; margin-top:100px; } </style>
3. Open for child elements BFC:
<style> .box1{ width: 200px; height: 200px; background-color: tomato; } .box2{ width: 100px; height: 100px; background-color: #bfa; float:left; margin-top:100px; } </style>4. clearfix: Add a blank element to the front of the parent element , Then display the element as a block element
<style> .box1{ width: 200px; height: 200px; background-color: tomato; } .box2{ width: 100px; height: 100px; background-color: #bfa; margin-top:100px; } .box1::before{ content:""; display:table; } </style>
边栏推荐
- Use of fiddler packet capturing tool
- 黑马程序员-软件测试-16阶段3-功能测试-175-198,URL组成介绍,请求内容以及组成说明行功能测试与数据库,url组成扩展说明,客户端与服务器请求与响应,-Fiddler按照以及功能检查确认,
- Modify scroll bar style
- Pytorch notes (5)
- cad填充转多段线脚本
- Export file or download file
- Complete square number
- 【MySQL】 锁机制:InnoDB引擎中锁分类以及表锁、行锁、页锁详解
- openvino机器学习初体验
- 面试题:外边距折叠问题 (块级元素在普通文档流中的BUG)
猜你喜欢

Export file or download file

Pytoch notes (2)

Discussion sur la technologie RISC - V

1669. Merge two linked lists (merge of two linked lists)

Modify the select style

Ruffian Heng embedded bimonthly issue 58

Jd.com's purchase intention forecast (IV)

@How can conditionalonmissingbean cover beans in third-party components

网站APP数据库里的用户信息被泄露篡改怎么办

Pytorch notes (5)
随机推荐
Will it be a little late to realize your "wonderful" 360?
【刷题篇】完全平方数
本地存储 sessionStorage
Ccf-csp "202206-2 - treasure hunt! Adventure!"
Export file or download file
Flowable query, complete, void, delete tasks
Modify scroll bar style
KingbaseES 中可以通过构造一个聚集函数来实现mysql的any_value功能。
csp-2020-6-《角色授权》
Junit5
神经网络和自动控制的联系
[day01] preface, introductory program, constant variables
openvino机器学习初体验
CAD fill to polyline script
[C classes and objects] - Methods and class and object programming in C
What if the user information in the website app database is leaked and tampered with
Facial key point detection CNN
How to associate the application on enterprise wechat with the Sybase database of the store
Modify radio style
2022 review questions and mock exams for main principals of hazardous chemical business units





