亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

css中常用的幾種居中方法(推薦)

  發(fā)布時(shí)間:2016-05-25 14:50:34   作者:佚名   我要評論
下面小編就為大家?guī)硪黄猚ss中常用的幾種居中方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

在前端面試中,大都會問你div居中的方法:

文筆不好,就隨便寥寥幾句話概括了,

不過以后文筆肯定會變得更好一些的。

今天我們就細(xì)數(shù)一下幾種方法:

1,使用position:absolute,設(shè)置left、top、margin-left、margin-top的屬性

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .one{   
  2.            position:absolute;   
  3.            width:200px;   
  4.            height:200px;   
  5.            top:50%;   
  6.            left:50%;   
  7.            margin-top:-100px;   
  8.            margin-left:-100px;   
  9.            background:red;    
  10. }  

這種方法基本瀏覽器都能夠兼容,不足之處就是需要固定寬高。

2,使用position:fixed,同樣設(shè)置left、top、margin-left、margin-top的屬性

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .two{   
  2.             position:fixed;   
  3.             width:180px;   
  4.             height:180px;   
  5.             top:50%;   
  6.             left:50%;   
  7.             margin-top:-90px;   
  8.             margin-left:-90px;   
  9.             background:orange;   
  10. }  

大家都知道的position:fixed,IE是不支持這個(gè)屬性的

3,利用position:fixed屬性,margin:auto這個(gè)必須不要忘記了。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .three{   
  2.             position:fixed;   
  3.             width:160px;   
  4.             height:160px;   
  5.             top:0;   
  6.             rightright:0;   
  7.             bottombottom:0;   
  8.             left:0;   
  9.             margin:auto;   
  10.             background:pink;   
  11. }  

4,利用position:absolute屬性,設(shè)置top/bottom/right/left

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .four{   
  2.             position:absolute;   
  3.             width:140px;   
  4.             height:140px;   
  5.             top:0;   
  6.             rightright:0;   
  7.             bottombottom:0;   
  8.             left:0;   
  9.             margin:auto;   
  10.             background:black;   
  11. }  

5,利用display:table-cell屬性使內(nèi)容垂直居中

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .five{   
  2.             display:table-cell;   
  3.             vertical-align:middle;   
  4.             text-align:center;   
  5.             width:120px;   
  6.             height:120px;   
  7.             background:purple;   
  8. }  

6,最簡單的一種使行內(nèi)元素居中的方法,使用line-height屬性

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .six{   
  2.             width:100px;   
  3.             height:100px;   
  4.             line-height:100px;   
  5.             text-align:center;   
  6.             background:gray;   
  7.  }  

這種方法也很實(shí)用,比如使文字垂直居中對齊

7,使用css3的display:-webkit-box屬性,再設(shè)置-webkit-box-pack:center/-webkit-box-align:center

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .seven{   
  2.             width:90px;   
  3.             height:90px;   
  4.             display:-webkit-box;   
  5.             -webkit-box-pack:center;   
  6.             -webkit-box-align:center;   
  7.             background:yellow;   
  8.             color:black;   
  9. }  

8,使用css3的新屬性transform:translate(x,y)屬性

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .eight{   
  2.             position:absolute;   
  3.             width:80px;   
  4.             height:80px;   
  5.             top:50%;   
  6.             left:50%;   
  7.             transform:translate(-50%,-50%);   
  8.             -webkit-transform:translate(-50%,-50%);   
  9.             -moz-transform:translate(-50%,-50%);   
  10.             -ms-transform:translate(-50%,-50%);   
  11.             background:green;   
  12. }  

這個(gè)方法可以不需要設(shè)定固定的寬高,在移動端用的會比較多,在移動端css3兼容的比較好

9、最高大上的一種,使用:before元素

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .nine{   
  2.             position:fixed;   
  3.             display:block;   
  4.             top:0;   
  5.             rightright:0;   
  6.             bottombottom:0;   
  7.             left:0;   
  8.             text-align:center;   
  9.             background:rgba(0,0,0,.5);   
  10. }   
  11. .nine:before{   
  12.             content:'';   
  13.             display:inline-block;   
  14.             vertical-align:middle;   
  15.             height:100%;   
  16. }   
  17.  .nine .content{   
  18.             display:inline-block;   
  19.             vertical-align:middle;   
  20.             width:60px;   
  21.             height:60px;   
  22.             line-height:60px;   
  23.             color:red;   
  24.             background:yellow;   
  25. }  

這種方法在我的前面一片文章有詳細(xì)的介紹:彈窗居中的簡單實(shí)現(xiàn)方法

以上這篇css中常用的幾種居中方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論