日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html盒子自动居中,css盒子怎么居中?

發(fā)布時間:2025/3/12 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html盒子自动居中,css盒子怎么居中? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在前端切圖的時候,可能經(jīng)常會遇到一個div盒子怎么在固定區(qū)域垂直居中的需求,下面我們來看一下css實現(xiàn)盒子居中的方法。

css設(shè)置盒子居中的方法:

第一種:

用css的position屬性

.div1 {

width: 100px;

height: 100px;

border: 1px solid #000000;

position: relative;

}

.div2 {

width: 40px;

height: 40px;

background-color: red;

position: absolute;

margin: auto;

top: 0;

left: 0;

right: 0;

bottom: 0;

}

第二種:

利用css3的新增屬性table-cell, vertical-align:middle;

.div1 {

width: 100px;

height: 100px;

border: 1px solid #000000;

display: table-cell;

vertical-align: middle;

}

.div2 {

width: 40px;

height: 40px;

background-color: red;

margin: auto;

}

第三種:

利用flexbox布局

.div1 {

width: 100px;

height: 100px;

border: 1px solid #000000;

display: flex;

/*!*flex-direction: column;*!可寫可不寫*/

justify-content: center;

align-items: center;

}

.div2 {

height: 40px;

width: 40px;

background-color: red;

}

第四種:

利用transform的屬性(缺點:需要支持Html5)

.div1 {

width: 100px;

height: 100px;

border: 1px solid #000000;

position: relative;

}

.div2 {

height: 40px;

width: 40px;

background-color: red;

position: absolute;

top: 50%;

left: 50%;

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

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

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

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

}

效果圖:

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的html盒子自动居中,css盒子怎么居中?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。