[英]css margin auto display inline-table
I am trying to center a div (popup dialog) with css. 我正在尝试使用CSS将div(弹出对话框)居中。 I would like to have a universal css declaration, and I'm nearly there. 我想有一个通用的CSS声明,而且我已经快到了。 The css I'm using works, but I have to explicitly define a width to de div. 我正在使用的CSS可以工作,但是我必须明确定义de div的宽度。
in following image the width of the popup is defined as 415px. 在下图中,弹出窗口的宽度定义为415px。
In following image the width is defined as AUTO. 在下图中,宽度定义为AUTO。
When I look at the boxmodel in the downright corner, both are identical. 当我看右下角的boxmodel时,两者是相同的。 My question is why is the last div wrapping to the left while it has a left margin? 我的问题是,为什么最后一个div在有左边距的情况下会在左边换行?
I already tried all the display types. 我已经尝试了所有显示类型。 It seems the "table" and "inline-table" style are optimal for centering purpose. 似乎“表格”和“内联表格”样式最适合居中。
Because margin: auto
only works with a defined width
. 因为margin: auto
仅适用于定义的width
。 Otherwise the browser doesn't know how to calculate the distance from each side. 否则,浏览器将不知道如何计算两边的距离。 And yes an alternative to using margin
would be to use display: inline-block
and adding text-align: center
to the parent. 是的,使用margin
一种替代方法是使用display: inline-block
并将text-align: center
添加到父级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.