自动页边距不会在页面中居中放置图像
在这个例子中 ,图像不居中。 为什么? 我的浏览器是Windows 7上的Google Chrome v10,不是IE。
<img src="/img/logo.png" style="margin:0px auto;"/>
添加display:block; 它会工作。 图像默认是内联的
为了澄清, block元素的默认宽度是auto ,这当然会填充包含元素的整个可用宽度。
通过将边距设置为auto ,浏览器将余下的一半空间分配给margin-left ,另一半分配给margin-right 。
在某些情况下(比如早期版本的IE,Gecko,Webkit)和inheritance,元素的position:relative; 将防止margin:0 auto; 即使没有设置top , right , bottom和left也是如此。
将元素设置为position:static; (默认)可以在这种情况下修复它。 通常,具有指定宽度的块级元素将遵循margin:0 auto; 使用relative或static定位。
在我的情况下,问题是我已经设置了最大和最小 宽度本身没有宽度 。
每当我们不添加宽度和添加margin:auto ,我想这是行不通的。 这是我的经验。 宽度给出了它需要提供相等的利润的地方。
你可以使用display:table;来自动调整div的宽度display:table;
div{ margin: 0px auto; float: none; display: table; }
还有一个可选的margin-left:auto; margin-right: auto; margin-left:auto; margin-right: auto; 或margin:0 auto; 对于使用position:absolute; 这是如何:
您将元素的左侧位置设置为50%( left:50%; ),但不会正确居中,以便元素正确居中,您需要给它一个宽度的一半的余量,这将完美的中心你的元素
这里是一个例子: http : //jsfiddle.net/35ERq/3/
对于引导button:
display: table; margin: 0 auto;