css样式里(水平居中)
1.text-align:center
2.margin 左右设置具体值
3.给居中div的父元素设置宽度+margin左右为auto,上下随意
垂直居中:
1.position:absolute/fixed;+width+height+top:50% +left:50% +transform:(或是 margin-top+margin-left)
2.position:fixed/absolute;+width+height+top:0+right:0+bottom:0+left:0+margin:auto;
3.line-height每个div间隔
4.不知道自己高度和父容器高度的情况下(https://www.zhihu.com/question/20543196)
parentElement{ position:relative; } childElement{ position: absolute; top: 50%; transform: translateY(-50%); }
5.若父容器下只有一个元素,且父元素设置了高度
parentElement{ height:xxx; } .childElement { position: relative; top: 50%; transform: translateY(-50%); }