Heim  >  Artikel  >  Web-Frontend  >  为什么margin的auto属性值失效了_html/css_WEB-ITnose

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

WBOY
WBOYOriginal
2016-06-24 11:31:321423Durchsuche

为什么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/

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn