(4)HTML&&CSS笔记(外边距合并)

外边距合并
一个元素与另一个元素之间在垂直方向上合并起来。

解决方案:
1、给父级加内边距来撑开垂直方向的内容。

<html>
	<head>
		<meta charset='utf-8'/>
		<title> Document</title>
		<meta name='keywords' content=''/>
		<meta name='description' content=''/>
		<style type="text/css">
			body{
				margin:0;
			}
			.mydiv1{
				width:500px;
				height:500px;
				background-color:#90f;
				/* margin-top:50px; */
				padding-top:50px;
				box-sizing:border-box;
			}
			.mydiv2{
				width:300px;
				height:300px;
				background-color:#ff0;
				/* margin-top:50px; */
			}
			.mydiv3{
				width:100px;
				height:100px;
				background-color:deeppink;
			}
		</style>
	</head>
	<body>
		<div class="mydiv1">
			<div class="mydiv2">
				<div class="mydiv3"></div>
			</div>
		</div>
	</body>
</html>

2、给父级加边框线来阻止触发BFC机制

<html>
	<head>
		<meta charset='utf-8'/>
		<title> Document</title>
		<meta name='keywords' content=''/>
		<meta name='description' content=''/>
		<style type="text/css">
			body{
				margin:0;
			}
			.mydiv1{
				width:500px;
				height:500px;
				background-color:#90f;
				/* margin-top:50px; */
				/* padding-top:50px; */
				/* box-sizing:border-box; */
				border:1px solid transparent;
			}
			.mydiv2{
				width:300px;
				height:300px;
				background-color:#ff0;
				margin-top:50px; 
			}
			.mydiv3{
				width:100px;
				height:100px;
				background-color:deeppink;
			}
		</style>
	</head>
	<body>
		<div class="mydiv1">
			<div class="mydiv2">
				<div class="mydiv3"></div>
			</div>
		</div>
	</body>
</html>

在这里插入图片描述
兄弟元素之间外边距合并:
元素1加下外边距,元素2加上外边距;
两者之间会产生外边距合并,外边距取两者中的最大值;

解决方案:
直接给一方加设定的外边距;

<!doctype html>
<html>
	<head>
		<meta charset='utf-8'/>
		<title> Document</title>
		<meta name='keywords' content=''/>
		<meta name='description' content=''/>
		<style type="text/css">
			body{
				margin:0;
			}
			.mydiv1{
				width:200px;
				height:200px;
				background-color:#90f;
				border:1px solid transparent;
				margin-bottom:130px;
			}
			.mydiv2{
				width:300px;
				height:300px;
				background-color:#ff0;
			}
		</style>
	</head>
	<body>
		<div class="mydiv1"></div>
		<div class="mydiv2"></div>
	</body>
</html>

布局技巧
1、从外面到里面
2、从简单到复杂
3、从大到小
4、使用辅助线(边框线,背景颜色)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值