div居中的幾種方式

#pp{

position: absolute;

display: flex;

height: 200px;

width: 200px;

margin: auto;

top: 0;

left: 0;

right: 0;

bottom: 0;

/*text-align: center;*/

background-color: pink;

}

1、絕對定位加top: 0;left: 0;right: 0;bottom: 0;可居中

display: flex;

margin: 0 auto;

align-items: center;

align-self: center;

justify-content: center;

height: 100px;

width: 100px;

background-color: green;

2、使用伸縮盒子(display: flex;)的align-self: center;垂直居中,margin: 0 auto;實現水平居中

3、其中align-items: center;justify-content: center可使子元素居中。

#pp div div div{

position: absolute;

display: table;

height: 50px;

width: 50px;

background-color: black;

top: 50%;

left: 50%;

transform: translate(-50%,-50%);

}

4、position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);利用這幾個css也可實現居中

#pp div div div div{

display: table-cell;

height: 20px;

width: 20px;

background-color: blue;

}

5、父元素使用display:table,子元素display:table-cell也可實現居中;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 各種純css圖標 CSS3可以實現很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,700評論 0 8
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,814評論 1 92
  • div水平居中的N種方法 一、單行垂直居中 如果一個容器中只有一行文字,對它實現居中相對比較簡單,我們只需要設置它...
    fredah閱讀 5,961評論 0 1
  • 聚散 緣份來掌握 緣份 它又是什么 世間業力的結果 從天上忽然降落 一直都讓人迷惑 心動 是最美一刻 迷戀 是跳進...
    酒求閱讀 298評論 0 0
  • HTTP Network Overview HTTP Request and Response URL: unif...
    mlya閱讀 427評論 0 2