css中常用的幾種置中方法,css幾種置中

來源:互聯網
上載者:User

css中常用的幾種置中方法,css幾種置中

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

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

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

今天我們就細數一下幾種方法:

1,使用position:absolute,設定left、top、margin-left、margin-top的屬性

 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,同樣設定left、top、margin-left、margin-top的屬性

 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是不支援這個屬性的

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

 1 .three{ 2             position:fixed; 3             width:160px; 4             height:160px; 5             top:0; 6             right:0; 7             bottom:0; 8             left:0; 9             margin:auto;10             background:pink;11 }

4,利用position:absolute屬性,設定top/bottom/right/left

.four{            position:absolute;            width:140px;            height:140px;            top:0;            right:0;            bottom:0;            left:0;            margin:auto;            background:black;}

5,利用display:table-cell屬性使內容垂直置中

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,最簡單的一種使行內元素置中的方法,使用line-height屬性

1 .six{2             width:100px;3             height:100px;4             line-height:100px;5             text-align:center;6             background:gray;7  }

這種方法也很實用,比如使文字垂直置中對齊

7,使用css3的display:-webkit-box屬性,再設定-webkit-box-pack:center/-webkit-box-align:center

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)屬性

 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 }

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

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

.nine{            position:fixed;            display:block;            top:0;            right:0;            bottom: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;}

這種方法在我的前面一片文章有詳細的介紹:http://www.cnblogs.com/White-Quality/p/4530867.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.