重新认识margin-top和margin-bottom

        margin-top和margin-bottom是HTML网页设计中常用的两个样式属性,提起margin和padding的区别想来无人不知无人不晓,那么让我们实现如下功能:

1、“应付总额”所在div距父div的距离为20px;

2、“寄送至”所在div距离上面兄弟div的距离为10px;

3、“寄送至”所在div距离父div的距离为20px;

有些童鞋会不假思索的这样写代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>重新认识margin-top和margin-bottom</title>
	</head>
	<body>
			<div style="background-color:red;">
				<div style="margin-top:20px;">应付总额: ¥45.00</div>
				<div style="margin-top:10px;margin-bottom:20px;">寄送至: 中国 河南省郑州市金水区</div>
			</div>
	</body>
</html>

浏览器渲染后的效果如图所示:

        看到这个图是不是很吃惊:我们只实现了第二个要求,为什么会这样?一句话,我们对margin-top和margin-bottom的真实作用还没有充分的认识:margin-top用于设置其所作用的标签的上外距离,可是该上外边距相对于谁呢?是父div吗?呵呵呵,不是,因为该父div没有边界,对于margin-top来说根就参照不了,其实现在参照的是body;margin-bottom同样如此。可以如何解决这个问题呢?

第一种方法:最简单的方式是为父div添加边框

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>重新认识margin-top和margin-bottom</title>
	</head>
	<body>
			<div style="background-color:red;border:2px solid green;">
				<div style="margin-top:20px;">应付总额: ¥45.00</div>
				<div style="margin-top:10px;margin-bottom:20px;">寄送至: 中国 河南省郑州市金水区</div>
			</div>
	</body>
</html>

浏览器渲染后的效果如图所示:

        第二种方法:使用padding-top和padding-bottom实现

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>重新认识margin-top和margin-bottom</title>
	</head>
	<body>
			<div style="padding-top:20px;padding-bottom:20px;background-color:red;">
				<div>应付总额: ¥45.00</div>
				<div style="margin-top:10px;">寄送至: 中国 河南省郑州市金水区</div>
			</div>
	</body>
</html>

浏览器渲染后的效果如图所示:

margin-top用于设置其所作用的标签的上外距离,但是该上外边距相对于谁呢?根据引用\[1\]中的解释,对于margin-top来说,它的参照物不是父div,而是body。这是因为如果父div没有边界,margin-top就无法参照。所以,margin-top实际上是相对于body来设置的。这也解释了为什么在某些情况下,我们设置了margin-top,但效果并不如预期。 #### 引用[.reference_title] - *1* [重新认识margin-top和margin-bottom](https://blog.csdn.net/wangshuxuncom/article/details/53214104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [详述外边距样式属性(margin-top margin-right margin-bottom margin-left margin)](https://blog.csdn.net/IT_Helianthus/article/details/90356824)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [CSS中的四种定位以及top和margin-top的区别](https://blog.csdn.net/qq_40935723/article/details/100182374)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值