CSS实现居中

一、水平居中的方法
1.元素为行内元素,设置父元素text-align:center
2.如果元素宽度固定,可以设置左右margin为auto;
3.如果元素为绝对定位,设置父元素position为relative,元素设left:0;right:0;margin:auto;
4.使用flex-box布局,指定justify-content属性为center
5.display设置为tabel-ceil
二、垂直居中的方法
1.将显示方式设置为表格,display:table-cell,同时设置vertial-align:middle
2.使用flex布局,设置为align-item:center
3.绝对定位中设置bottom:0,top:0,并设置margin:auto
4.绝对定位中固定高度时设置top:50%,margin-top值为高度一半的负值
5.文本垂直居中设置line-height为height值
posted @ 2017-11-04 22:00  Samsara315  阅读(128)  评论(0编辑  收藏  举报