Maison  >  Article  >  interface Web  >  如何理解body的margin属性_html/css_WEB-ITnose

如何理解body的margin属性_html/css_WEB-ITnose

WBOY
WBOYoriginal
2016-06-24 11:47:531502parcourir

<html><head><style type="text/css">#outerBox{	width:128px;	height:128px;	border:10px black dashed;	background:silver;}</style></head><body>	<div id="outerBox">	</div></body>

上面代码在IE中的效果是
div这个盒子与浏览器边界有一定距离,但是如果html代码改为
<html><head><style type="text/css">#outerBox{	width:128px;	height:128px;	border:10px black dashed;	background:silver;}body{margin:0px;}</style></head><body>	<div id="outerBox">	</div></body>

此时的显示效果就是
div的边框几乎挨着浏览器边界了
想问下margin不是指的是盒子边框外的距离吗?而div不是在body盒子的内部码?为什么设置body的margin会导致div与body的距离缩小了,我理解的应该是设置body的padding为0px才会使div与body的距离变为0啊


回复讨论(解决方案)

大盒子包含着小盒子,body为大盒子,div为小盒子  整个文档可视为包含body的更大的盒子

body自身带有margin值,去掉了body外围的外边距,那么 body整体会做调整,就像 一个大盒子的位置移动,那么大盒子里面的东西 当然会随之移动

margin 是外边距,在边框外。padding 是内边距,在边框内,是内容区与边框之间的距离。
body 本身也是个盒子,可能默认的 margin 不为 0,所以与窗口边缘有一定距离。
body 的 margin 设为 0 之后,就紧挨着窗口边界了。outerBox 是 body 盒子里面的元素。
你给 body 在加一个 boder,看一下效果。

BODY 默认的
margin 为 4px;
padding 为 0px
请注意与其他 html 标记的不同

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn