本篇內(nèi)容主要講解“css中常用的幾種居中方法介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“css中常用的幾種居中方法介紹”吧!

在聶拉木等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需策劃設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),聶拉木網(wǎng)站建設(shè)費(fèi)用合理。
今天我們就細(xì)數(shù)一下幾種方法:
1,使用position:absolute,設(shè)置left、top、margin-left、margin-top的屬性
CSS Code復(fù)制內(nèi)容到剪貼板
.one{
position:absolute;
width:200px;
height:200px;
top:50%;
left:50%;
margin-top:-100px;
margin-left:-100px;
background:red;
}
這種方法基本瀏覽器都能夠兼容,不足之處就是需要固定寬高。
2,使用position:fixed,同樣設(shè)置left、top、margin-left、margin-top的屬性
CSS Code復(fù)制內(nèi)容到剪貼板
.two{
position:fixed;
width:180px;
height:180px;
top:50%;
left:50%;
margin-top:-90px;
margin-left:-90px;
background:orange;
}
大家都知道的position:fixed,IE是不支持這個(gè)屬性的
3,利用position:fixed屬性,margin:auto這個(gè)必須不要忘記了。
CSS Code復(fù)制內(nèi)容到剪貼板
.three{
position:fixed;
width:160px;
height:160px;
top:0;
rightright:0;
bottombottom:0;
left:0;
margin:auto;
background:pink;
}
4,利用position:absolute屬性,設(shè)置top/bottom/right/left
CSS Code復(fù)制內(nèi)容到剪貼板
.four{
position:absolute;
width:140px;
height:140px;
top:0;
rightright:0;
bottombottom:0;
left:0;
margin:auto;
background:black;
}
5,利用display:table-cell屬性使內(nèi)容垂直居中
CSS Code復(fù)制內(nèi)容到剪貼板
.five{
display:table-cell;
vertical-align:middle;
text-align:center;
width:120px;
height:120px;
background:purple;
}
6,最簡單的一種使行內(nèi)元素居中的方法,使用line-height屬性
CSS Code復(fù)制內(nèi)容到剪貼板
.six{
width:100px;
height:100px;
line-height:100px;
text-align:center;
background:gray;
}
這種方法也很實(shí)用,比如使文字垂直居中對齊
7,使用css3的display:-webkit-box屬性,再設(shè)置-webkit-box-pack:center/-webkit-box-align:center
CSS Code復(fù)制內(nèi)容到剪貼板
.seven{
width:90px;
height:90px;
display:-webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;
background:yellow;
color:black;
}
8,使用css3的新屬性transform:translate(x,y)屬性
CSS Code復(fù)制內(nèi)容到剪貼板
.eight{
position:absolute;
width:80px;
height:80px;
top:50%;
left:50%;
transform:translate(-50%,-50%);
-webkit-transform:translate(-50%,-50%);
-moz-transform:translate(-50%,-50%);
-ms-transform:translate(-50%,-50%);
background:green;
}
這個(gè)方法可以不需要設(shè)定固定的寬高,在移動(dòng)端用的會(huì)比較多,在移動(dòng)端css3兼容的比較好
9、最高大上的一種,使用:before元素
CSS Code復(fù)制內(nèi)容到剪貼板
.nine{
position:fixed;
display:block;
top:0;
rightright:0;
bottombottom:0;
left:0;
text-align:center;
background:rgba(0,0,0,.5);
}
.nine:before{
content:'';
display:inline-block;
vertical-align:middle;
height:100%;
}
.nine .content{
display:inline-block;
vertical-align:middle;
width:60px;
height:60px;
line-height:60px;
color:red;
background:yellow;
}
到此,相信大家對“css中常用的幾種居中方法介紹”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
標(biāo)題名稱:css中常用的幾種居中方法介紹
標(biāo)題路徑:http://www.chinadenli.net/article24/piijje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、定制開發(fā)、企業(yè)建站、外貿(mào)建站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)