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