CSS解決網頁HTML元素DIV等無法居中的方法

1.水平居中

<div class="box"></div>
 
.box{
    width: 100px;
    height: 100px;
    margin: 0 auto;
    background: #E33;
}

2.水平垂直居中

(1)絕對定位水平垂直居中

<div class="box">
     <div></div>
</div>
 
.box {
    width: 100%;
    height: 500px;
    background: #e33;
    position: relative;
}
.box>div{
    width: 100px;
    height: 100px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background: gold;
}

(3)flex彈性佈局垂直水平居中

<div class="box">
   <div></div>
</div>
 
.box {
    width: 100%;
    height: 500px;
    background: #e33;
    display: flex;
    align-items: center;
    justify-content: center;
}
.box>div{
    width: 100px;
    height: 100px;
    background: gold;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章