margin auto是什么意思? [英] What does margin auto mean?

查看:191
本文介绍了margin auto是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 MDN ,看看有什么办法自动为边距属性赋值,它表示:auto被替换为一个合适的值,例如它可以用于块的居中。

但是这是什么合适的价值,适合什么?



我尝试了一些实验,我发现如果我添加 margin-left:auto ,容器会向右移动(如浮动到右边):

  #container {
background:红;
width:120px;
margin-left:auto;
}

http://jsfiddle.net/sph2j6jx/



这是否意味着添加margin auto实际上就像是将所有可用空间?当你添加左右边距时,它会把div放在中间位置,因为它会试图从左向右取所有的空间?

解决方案

< blockquote>

自动页边距



根据具体情况,提供自动值指示
浏览器根据值在
自己的样式表中提供。但是,如果将这样的边距应用于具有有意义宽度的元素
,则自动边距会导致所有
可用空间都呈现为空白。
blockquote>

w3.org


I checked MDN to see what means to have an auto value for margin property and it says: "auto is replaced by some suitable value, e.g. it can be used for centering of blocks."

But what it is that suitable value, and suitable for what?

I tried myself some experiments and I saw that if I add margin-left: auto, the container goes to right (like is floating to right):

#container {
    background: red;
    width: 120px;
    margin-left: auto;
}

http://jsfiddle.net/sph2j6jx/

Does it mean that adding margin auto is actually something like "take all the space available"? And when you add both left and right margins it centers the div because it tries to take all the space from left and from right?

解决方案

Auto margins

Depending upon the circumstances, provision of an auto value instructs the browser to render a margin according to the value provided in its own stylesheet. However, when such a margin is applied to an element with a meaningful width, an auto margin instead causes all of the available space to be rendered as whitespace.

From w3.org

这篇关于margin auto是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆