首页  >  文章  >  web前端  >  为什么margin的auto属性值失效了_html/css_WEB-ITnose

为什么margin的auto属性值失效了_html/css_WEB-ITnose

WBOY
WBOY原创
2016-06-24 11:31:321424浏览

为什么margin的auto属性失效了:
建议:尽可能的手写代码,可以有效的提高学习效率和深度。
通常情况下,可以使用margin:0px auto将对象设置为水平居中对齐。这是因为auto属性值能够使浏览器自动为对象判断外边距大小,但是在下面的情况下却失效了。代码如下:

<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>蚂蚁部落</title><style type="text/css">.parent{  width:300px;  height:200px;  border:1px solid red;}.children{  width:200px;  height:100px;  background-color:green;  position:absolute;  margin:0px auto;}</style></head><body><div class="parent">  <div class="children"></div></div></body></html>

因为子对象是绝对定位,所以margin:0px auto无法将它居中对齐。解决方法可以参阅如何实现div水平垂直和居中效果。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=4707

更多内容可以参阅:http://www.softwhy.com/divcss/

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn