当前位置:网站首页>面试题:外边距折叠问题 (块级元素在普通文档流中的BUG)
面试题:外边距折叠问题 (块级元素在普通文档流中的BUG)
2022-07-17 06:26:00 【The..Fuir】
一、概念
一般是指垂直方向相邻的外边距会发生重叠现象,大多发生在
兄弟元素和父子元素之间。二、兄弟元素
box2是box1的兄弟元素
当两者都是正值时:取二者之间的最大值
<!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>
一正一负:取二者之和
<!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> --> <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>
两个都负值:取绝对值较大的
<!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> --> <!-- 一正一负:取二者之和 <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>
三、父子元素
box2是box1的子元素
<!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>
- 当box2的上外边距设置为100:
- 这时父元素也会向下移动,即:子元素的外边距会传递给父元素。
四、四种解决方案
1.给父元素加上内边距,然后再裁去多余的长度
<!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> --> <!-- 使用padding不会产生边距折叠问题 --> <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.用外边距但是不相邻,给父元素加一个边框,然后把边框颜色改成父元素的颜色,再裁去多余的边框长度:
<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.为子元素开启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:在父元素的最前边增加一个空白元素,然后将元素作为块元素来显示
<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>
边栏推荐
- 912. 排序数组(数组排序)
- 通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
- 牛客题目——打家劫舍一、打家劫舍二
- [MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine
- Flutter3.0(framework框架)——UI渲染
- 1669. 合并两个链表(两个链表的合并)
- MongoDB的使用
- [C language] user defined type details: structure, enumeration, union
- Question 114 of Li Kou: binary tree expansion linked list
- Flink introduction to practice - phase II (illustrated runtime architecture)
猜你喜欢

912. Sort array (array sort)

Pytoch notes (3)

【JVM】之虚拟机栈

会话技术【黑马入门系列】

MongoDB的使用

High performance integrated video image processing board based on ultrascale FPGA + Huawei Hisilicon arm / fpga+arm

Flink introduction to practice - phase II (illustrated runtime architecture)

串口通讯到底有没有累积误差及对时钟精度的要求

Pytoch notes (1)

RNN convolutional neural network
随机推荐
MongoDB的下载、安装和使用
the max_iter was reached which means the coef_ did not converge “the coef_ did not converge“
JS array intersection, subtraction and union
企业微信上的应用如何与门店的sybase数据库关联
How to associate the application on enterprise wechat with the Sybase database of the store
美联储降息,为何长期利好数字货币市场? 2020-03-05
flowable 查询、完成、作废、删除 任务
[C# Console]-C# 控制台类
YOLOV5-打标签建立自己的数据集
代码学习(DeamNet)CVPR | Adaptive Consistency Prior based Deep Network for Image Denoising
Titanic passenger rescue prediction (Advanced)
Pytoch notes (1)
[C# 类和对象]-C# 中的方法以及类和对象编程
Machine learning interview questions (Reprinted)
Real time data warehouse - Design & Implementation of real-time data warehouse from 0 to 1 (sparkstreaming3.x)
CCF-CSP《202206-2—寻宝!大冒险!》
redis分布式锁
Understand LSTM and Gru
Jira --- workflow call external api
Mongodb index





