適用于固定寬高:
.center1{
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width:100px;
height:100px;
}
.center2{
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width:100px;
height:100px;
}
適用于固定寬高和非固定寬高:
.center3{
position:absolute;
width:140px;
height:140px;
top:0;
right:0;
bottom:0;
left:0;
margin:auto;
}
- 使用position:absolute以及css3的新屬性transform:translate(x,y)
.center4 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
.center5{
position:fixed;
width:140px;
height:140px;
top:0;
right:0;
bottom:0;
left:0;
margin:auto;
}
.center6 {
display: table-cell;
vertical-align: middle;
text-align: center;
}
- 利用偽元素和vertical-align:middle(常用語彈窗居中)
.outbox::before{
content:"";
width:0;
height:100%;
display:inline-block;
vertical-align:middle;
}
.outbox{
position:fixed;
top:0;
right:0;
bottom:0;
left:0;
text-align:center;
}
.content{
width:200px;
height:200px;
background-color:#ccc;
display:inline-block;
vertical-align:middle;
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。