总结Margin重叠现象

普通与普通大者取大

<!DOCTYPE html>
<html>
<head>
	<title>box test</title>
	<style type="text/css">
		#box{
			width: 200px;
			height: 200px;
			background: black;
			margin-bottom: 50px;
			
		}
		#box1{
			width: 300px;
			height: 100px;
			background: blue;
			margin-top: 50px;
			
		}
	
	</style>
</head>
<body>
	<div id="box"></div>
	<div id="box1"></div>
	
</body>
</html>





float 与 float两者相加

<!DOCTYPE html>
<html>
<head>
	<title>box test</title>
	<style type="text/css">
		#box{
			width: 200px;
			height: 200px;
			background: black;
			margin-bottom: 50px;
			float: left;
		}
		#box1{
			width: 1900px;
			height: 100px;
			background: blue;
			margin-top: 50px;
			float: left;
		}
		
	</style>
</head>
<body>
	<div id="box"></div>
	<div id="box1"></div>
</body>
</html>





float对普通无影响

<!DOCTYPE html>
<html>
<head>
	<title>box test</title>
	<style type="text/css">
		#box{
			width: 200px;
			height: 200px;
			background: black;
			margin-bottom: 50px;
			float: left;
		}
		#box1{
			width: 300px;
			height: 100px;
			background: blue;
			margin-top: 50px;
			float: left;
		}
		#box2{
			background: green;
	
			height: 300px;
			width: 1800px;
			
		}
	</style>
</head>
<body>
	<div id="box"></div>
	<div id="box1"></div>
	<div id="box2"></div>
</body>
</html>





总结:普通对普通大者取大;

            float与普通无影响;

            float与float两者相加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当两个相邻的块元素(block element)的上下 margin 相遇时,会发生 margin 重叠(collapsing margins)现象。这种情况下,两个块元素的 margin 可能会合并为一个较大的 margin 值。 要解决这个问题,您可以尝试以下方法之一: 1. 使用 `padding` 替代 `margin`:将其中一个块元素的 margin 改为 padding,或者在两个块元素之间插入一个包裹元素,并给这个包裹元素设置 padding。这样可以避免 margin 重叠。 ```html <style> .element1 { margin-bottom: 20px; background-color: lightgray; } .element2 { padding-top: 20px; background-color: lightblue; } </style> <div class="element1">块元素1</div> <div class="element2">块元素2</div> ``` 2. 使用 `border` 或 `outline`:给其中一个块元素添加一个看不见的边框(border)或轮廓(outline),这样也可以阻止 margin 重叠。 ```html <style> .element1 { margin-bottom: 20px; background-color: lightgray; } .element2 { border-top: 1px solid transparent; background-color: lightblue; } </style> <div class="element1">块元素1</div> <div class="element2">块元素2</div> ``` 3. 使用 `float` 属性:将其中一个块元素设置为浮动(float),这样也可以解决 margin 重叠的问题。 ```html <style> .element1 { margin-bottom: 20px; background-color: lightgray; } .element2 { float: left; background-color: lightblue; } </style> <div class="element1">块元素1</div> <div class="element2">块元素2</div> ``` 这些方法中的任何一种都可以解决块元素之间的 margin 重叠问题。您可以根据具体情况选择适合您的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值