html+css基础2(外边距)

外边距

注意:
1.行内元素不支持垂直外边距
2.取值类型px(正值/负值,可以微调位置)、%(参考父元素),
auto(水平居中,自动分布页面的空白区域,必须给块元素宽度)
3.垂直外边距合并
当两个垂直往外边距合并的时候,会产生合并现象,取其中的较大值
4.垂直外边距溢出
当父元素没有边框时,给子元素设置垂直外边距会产生溢出现象,作用到父元素上

解决方法
①给父元素增加边框(尺寸增加)
②取消子元素的垂直外边距,改成父元素的垂直内边距(尺寸增加)
③在子元素的前面加一个空的table挡住外边距(增加页面结构)
④父元素加一个:before

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0 auto;
				padding: 0;
			}
			.box1{
				width: 398px;
				height: 398px;
				background-color: #ba0005;
				border-radius:50%;
				border: 1px solid #ba0005;		/* 方法一:设置边框透明,但是边框会使盒子尺寸增加 */
				/* position: relative; */
			}
			.f1{
				font-size: 46px;
				color: #fff5f8;
				/* position: absolute;
				left: 106px; top: 110px; */
				margin: 100px;
				
			}
			.box2{
				width: 200px;
				height: 200px;
				background-color: #ba0005;
				border-radius:50%;
				padding: 100px;						/* 方法二:设置内边距,需要父级宽高减去相应距离 */
			}
			.f2{
				font-size: 46px;
				color: #fff5f8;
			}
			.box3{
				width: 400px;
				height: 400px;
				background-color: #ba0005;
				border-radius:50%;
			}
			.f3{
				font-size: 46px;
				color: #fff5f8;
				margin: 100px;
			}
			.box4{
				width: 400px;
				height: 400px;
				background-color: #ba0005;
				border-radius:50%;
			}
			.box4:before{							/* 方法四:父级前加一个before,使其content为空 */
				content: "";
				display: table;
			}
			.f4{
				font-size: 46px;
				color: #fff5f8;
				margin: 100px;
			}
		</style>
	</head>
	<body>
		<div class="box1">
			<h1 class="f1">产品展示</h1>
		</div>
		<div class="box2">
			<h1 class="f2">产品展示</h1>
		</div>
		<div class="box3">
			<table border="" cellspacing="" cellpadding="">		<!-- 方法三:父级前加个table,但会增加页面结构 -->
			</table>
			<h1 class="f3">产品展示</h1>
		</div>
		<div class="box4">
			<h1 class="f4">产品展示</h1>
		</div>
	</body>
</html>

:before和::before的区别
https://www.cnblogs.com/1115changhao/p/10418332.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值