“margin:0 auto”需要什么,上班? [英] What, exactly, is needed for "margin: 0 auto;" to work?
问题描述
我知道在元素上设置 margin:0 auto;
可以使它居中(左 - 右)。但是,我知道元素和它的父类必须满足一定的标准,汽车保证金工作,我永远不会得到神奇的权利。
I know that setting margin: 0 auto;
on an element is used to centre it (left-right). However, I know that the element and its parent must meet certain criteria for the auto margin to work, and I can never seem to get the magic right.
所以我的问题很简单:为了 margin:0 auto;
,要在元素及其父元素上设置什么CSS属性,使子元素左右居中?
So my question is simple: what CSS properties have to be set on an element and its parent in order for margin: 0 auto;
to left-right centre the child?
推荐答案
离开我的头顶:
- 元素必须
display:block
- 元素不得浮动
- 固定或绝对位置
别人的头顶:
- 元素必须有
width
,不是auto
sup> 1
- The element must have a
width
that is notauto
1
sub>技术上, margin:0 auto
对自动宽度工作,但自动宽度优先于自动边距,并且自动边距被归零,使它看起来好像他们不工作。
1 Technically, margin: 0 auto
does work with an auto width, but the auto width takes precedence over the auto margins, and the auto margins are zeroed out as a result, making it seem as though they "don't work".
这篇关于“margin:0 auto”需要什么,上班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!