图片垂直居中:
display:table-cell;
vertical-align:middle;
缺点:display:table-cell IE6/7不兼容
大小不固定 多行文本 上下居中:
一:
div{
vertical-align:middle;
display:table-cell;
}
span{
display:inline-block;
vertical-align:middle;
}
二:positioin+margin
.wrapper{
position:relative;
}
.wrapper .center{
margin:auto;
position:absolute;
left:0;
top:0;
right:0;
bottom:0;
}
三:position+transform
.wrap {
position: relative;
background: yellow;
width: 200px;
height: 200px;
}
.center {
position: absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
}
问题: IE9一下不支持 移动端较好
四:flex;align-items: center;justify-content: center 移动端首选
.wrap{
display: flex;
align-items: center;
justify-content: center;
}
五:
.wrap{
display: flex;
}
.center{
margin:auto;
}
六:纯position:适用于所有浏览器
/**方法一**/
.center {
background: green;
position: absolute;
width: 100px;
height: 100px;
left: 50px;
top: 50px;
}
/**方法二**/
.center {
background: green;
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%;
margin-left:-50px;
margin-top:-50px;
}
七:
方法七:兼容低版本浏览器,不固定宽高
<div class="table">
<div class="tableCell">
<div class="content">不固定宽高,自适应</div>
</div>
</div>
.table {
height: 200px;/*高度值不能少*/
width: 200px;/*宽度值不能少*/
display: table;
position: relative;
float:left;
background: yellow;
}
.tableCell {
display: table-cell;
vertical-align: middle;
text-align: center;
*position: absolute;
padding: 10px;
*top: 50%;
*left: 50%;
}
.content {
*position:relative;
*top: -50%;
*left: -50%;
background: green;
}