margin: 0 auto;为什么会居中呢???html
div{ height: 200px; width: 200px; background: red; margin: 0 auto; }
那么若是只有一侧设置了auto会产生什么效果学习
div{ height: 200px; width: 200px; background: red; margin-right: auto; }
那么如何垂直方向居中呢code
div{ position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; height: 200px; width: 200px; background: red; }
原理: 若是设置了绝对定位而且上下左右所有为0(不能只设置上左或者其余的任意两方向),那么他会填充整个父元素的全部可用空间,那么auto就有了做用,会平均分配剩余的空间htm
分享不易,感谢star^-^blog