溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

css中常用的幾種居中方法介紹

發布時間:2021-09-10 20:54:41 來源:億速云 閱讀:207 作者:chen 欄目:web開發

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

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

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

CSS Code復制內容到剪貼板

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

CSS Code復制內容到剪貼板

  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這個必須不要忘記了。

CSS Code復制內容到剪貼板

  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屬性,設置top/bottom/right/left

CSS Code復制內容到剪貼板

  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屬性使內容垂直居中

CSS Code復制內容到剪貼板

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

CSS Code復制內容到剪貼板

  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

CSS Code復制內容到剪貼板

  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復制內容到剪貼板

  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元素

CSS Code復制內容到剪貼板

  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. }  

到此,相信大家對“css中常用的幾種居中方法介紹”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

css
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女