Home > Web Front-end > HTML Tutorial > 为什么margin的auto属性值失效了_html/css_WEB-ITnose

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

WBOY
Release: 2016-06-24 11:31:32
Original
1492 people have browsed it

为什么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>
Copy after login

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

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template