首頁 web前端 css教學 css讓容器水平垂直居中的7種方式

css讓容器水平垂直居中的7種方式

May 21, 2017 am 10:31 AM
css 垂直 居中 水平

這篇文章主要為大家詳細介紹了css讓容器水平垂直居中的7種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這種css佈局平時用的比較多,也是面試題常出的一個題,網路上一搜一大丟,不過還是想自己總結一下。
這種方法比較多,本文只總結其中的幾種,以便加深印象。
效果圖都為這個:

方法一:position加上margin


XML/HTML Code複製內容到剪貼簿

  1. #<p #class ="wrap">  

  2.     <#p class="center">p>  


p>  
  1.    #CSS Code複製內容到剪貼簿 /**CSS**/ .wrap { width

  2. 200px
  3. height200px#background yellowpositionrelative;   } .wrap .#center { width100pxheight100pxbackgroundgreen##; marginautoposition

  4. absolute
  5. left

    : 0; 
  6. right

#right

: 0; 

top: 0; #bottombottom

: 0;   


#}  

}  
  1. 相容性:主流瀏覽器皆支持,IE6不支援

  2. #方法二:
  3. diaplay:table-cell

    XML/HTML Code複製內容到剪貼簿

  4.   #<##pclass# #"wrap">  

  5. #     

    <p class= "center"

  6. >

  7. p

>
  

  1. p>  

    ###  ############################ #############CSS Code######複製內容到剪貼簿#################/*&/ ### .wrap{ ###width###: ###200px###; ###height###: ###200px####; ###background###: ###yellow ###; ###display###: ###table-cell###; ###vertical-align###: ###middle###; ####text-align###: ###center###;   ####
  2. } .centerdisplay#: inline-block#vertical-alignmiddlewidth100pxheight100pxbackground# : green;   

  3. }   

  4. ##  

#相容性:由於display:table-cell的原因,IE67不相容

方法三:position加transform


XML/HTML Code複製內容到剪貼簿

  1. #  

  2. <p class="wrap">  

  3.     

    <p class="center"> p>  

  4. #p>  

  5.   


#CSS Code複製內容到剪貼簿

  1. /* css */ .wrap { #positionrelativebackgroundyellowwidth200pxheight 200px;} .center { positionabsolutebackground##: greengreen# top:50%; left:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%); width

  2. 100px
  3. height

  4. 100px
  5. ;   

}   

  

相容性:ie9以下不支援transform,手機端表現的比較好。


方法四:

flex;align-items: center;justify-content: center

  1. ##XML/HTML Code

    複製內容到剪貼簿

  2.   <p 

    class
  3. =
  4. "wrap"

    #>     <#p class="center"#>

  5. p

    >  ##


####################################################。 p######>###  ############  #########################CSS Code ######複製內容到剪貼簿######
  1. /* css */ .wrap { backgroundyellowwidth 200pxheight200pxdisplay: flex; align-items: center -contentcenter;   

  2. } .

    center { #background greenwidth100pxheight100px;   

  3. #}

  4.   

行動端首選

方法五:display :flex;margin:auto


#XML/HTML Code複製內容到剪貼簿

  1. #  

  2. <#p  class="wrap">  

  3.     

    #<# #p class="center">p> ;  

  4. #p>  


#CSS Code#複製內容到剪貼簿

  1. ##/* css */

     .wrap { backgroundyellowwidth200px#; height

  2. #1

    display: flex;    } .center { #backgroundgreenwidth100pxheight

    100px
  3. #margin

    auto
  4. #;
  5. }   

  移動端首選


方法六:

純position

  1. #XML/HTML Code

    複製內容到剪貼簿

  2. #  <

  3. ## p 

    class
  4. =
  5. "wrap"

    >

  6.   


    

<#p class

=###"center"######>#########  ##################  ######################################################################## ###  ########################CSS Code#######複製內容到剪貼簿######

  1. /* css */ .wrap { backgroundyellowwidth 200pxheight200pxpositionrelative;   

  2. #} /**方法一**/ .center { backgroundgreenposition#: # absolutewidth100pxheight100px#; left

  3. #如果111 ##; top50px;         #} /** 方法二**/ .center { backgroundgreen

  4. position
  5. absolute

  6. width
  7. 100px

  8. height

100px

left
##: 50%; 
top
#: 50%; 
margin-left
:-
50px

margin-top

:-50px;   


}     

  1. #相容性:適用於所有瀏覽器方法六中的方法一計算公式如下:

    子元素(conter)的left值計算公式:left=(父元素的寬- 子元素的寬) / 2=(200-100) / 2=50px;
  2. 子元素(conter)的top值計算公式:top=(父元素的高- 子元素的高) / 2=(200-100) / 2=50px;
  3. 方法二計算公式:

    left值固定為50%; 子元素的margin-left= -(子元素的寬/2)=-100/2= -50px;top值也是一樣,固定為50%子元素的margin-top= -(子元素的高/2)=-100/2= -50px;#方法七:相容於低版瀏覽器,不固定寬高

  4. XML/HTML Code複製內容到剪貼簿   

  5. #<##pclass# #"table">  #    <

     
  6. class
  7. =

    "tableCell">  

    #        
  8. <
  9. #ofclass##<p

    #class#1 #=
  10. "content"
  11. >不固定寬高,自適應

p

>

    

  • #p

    >  ############################ ####  ############  ################################################################# ########CSS Code######複製內容到剪貼簿##################/*css*/### .table { ###height###: ###200px###;###/*高度值不能少*/### ###width###: ###200px###;###/ *寬度值不能少*/### ###display###: table; ###position###: ###relative###; ###float####:###left## #; ###background###: ###yellow###;   ####

  • } .tableCell { displaytable-cellvertical-alignmiddlevertical-alignmiddle

    text-align
  • center

    ;               *> 10px;   

  • #    *top: 50%;   

  • ## *

    left: 50%;   

  • #} .

    content {   

  • #    *

    #position    *

    #position
  • :
  • relative

    ;   

  •     *
  • top

    : -50%;       *#left: -50%; 

    background
  • green

    ;   

  • }   

  • ##}   

    #  


    暫時總結上面的七種,這個方法太多,其實只要習慣了其中的一兩種就夠用了。

    總結如果是行動端,那麼用方法四和方法五是比較方便的。而且支援不固定寬高的情況,快、準、狠

    也就是用flex; align-items: center; justify-content: center;
    1. ##XML/HTML Code

    2. 複製內容到剪貼簿
    3.   <

    4. p
    5.  

      class="wrap"#>     <#p class="center"

    6. #>

    7. p>  

    8. ##


    1. #。 p>    

    2. #CSS Code 複製內容到剪貼簿/* css */ .wrap { background

    3. yellow
    4. width

    5. #200px
    6. height

    7. #200px

    display

    : flex align- items: 

    center

    justify

    -contentcenter;   

    1. #} .center { 

      background
    2. green

      width100pxheight100pxheight#: 

      100px
    3. ;   
    4. }     或display:flex;margin:auto; XML/HTML Code

    5. #複製內容到剪貼簿


    ## ####  ################<######p### ###class#######class###=# ##"wrap"######>###  #############    ###<######p#### ###class### =###"center"######>#########  ##################################################################################### ##########  ############  ########### ########

    CSS Code複製內容到剪貼簿

    1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxdisplay : flex;   

    2. } .center { backgroundgreen#width##green#width##green#width##green#width100px

    3. height
    4. 100px

    5. margin
    6. auto

      ;   


    1. ##1
    2. #}   
    3.   #如果是PC端,要考慮相容性的話。方法六是不錯滴,也就是純position。

    4. XML/HTML Code
    5. #複製內容到剪貼簿

    6.   #<

      p
    7.  
    8. class

      #class
    9. =
    "wrap"


    >

      

    #    <

      p
    1. class ="center">#p>  

    2. ##p>    

    3. #CSS Code#複製內容到剪貼簿##/* css */  .wrap { backgroundyellowwidth200px#; height#1 positionrelative;   

    4. /**方法一**/
    5.  .
    6. center

       { 

      background
    green

    position

    absolute###; ####width###: ###100px ###; ###height###: ###100px###; ###left###: ###50px###; ####top###: ###50px## #;     ############     ###########} ###/**方法二**/### .####center### { ###background###: ###green###; ###position###: ###absolute###; ###width###: ####100px####; ###width###: ####100px####; ## #height###: ###100px###; ###left###: 50%; ###top###: 50%; ####margin-left####:-### 50px###; ###margin-top###:-###50px###;   ###########}   ############  # ###########如果PC端的中間的元素高度不固定,那麼就用方法七即可,程式碼就不複製了。 ######這種css元素垂直的如果真的要總結起來,應該有十幾二十幾種。不過也沒必要全部掌握吧,只要大概了解一些,用起來沒有副作用就好。 ###

    以上是css讓容器水平垂直居中的7種方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅動的應用程序,用於創建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

    使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

    熱門文章

    <🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    北端:融合系統,解釋
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌

    熱工具

    記事本++7.3.1

    記事本++7.3.1

    好用且免費的程式碼編輯器

    SublimeText3漢化版

    SublimeText3漢化版

    中文版,非常好用

    禪工作室 13.0.1

    禪工作室 13.0.1

    強大的PHP整合開發環境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網頁開發工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神級程式碼編輯軟體(SublimeText3)

    熱門話題

    Java教學
    1666
    14
    CakePHP 教程
    1425
    52
    Laravel 教程
    1327
    25
    PHP教程
    1273
    29
    C# 教程
    1252
    24
    vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

    在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

    了解HTML,CSS和JavaScript:初學者指南 了解HTML,CSS和JavaScript:初學者指南 Apr 12, 2025 am 12:02 AM

    WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

    HTML,CSS和JavaScript的角色:核心職責 HTML,CSS和JavaScript的角色:核心職責 Apr 08, 2025 pm 07:05 PM

    HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

    bootstrap怎麼插入圖片 bootstrap怎麼插入圖片 Apr 07, 2025 pm 03:30 PM

    在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

    bootstrap怎麼寫分割線 bootstrap怎麼寫分割線 Apr 07, 2025 pm 03:12 PM

    創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

    bootstrap怎麼設置框架 bootstrap怎麼設置框架 Apr 07, 2025 pm 03:27 PM

    要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

    bootstrap怎麼調整大小 bootstrap怎麼調整大小 Apr 07, 2025 pm 03:18 PM

    要調整 Bootstrap 中元素大小,可以使用尺寸類,具體包括:調整寬度:.col-、.w-、.mw-調整高度:.h-、.min-h-、.max-h-

    bootstrap按鈕怎麼用 bootstrap按鈕怎麼用 Apr 07, 2025 pm 03:09 PM

    如何使用 Bootstrap 按鈕?引入 Bootstrap CSS創建按鈕元素並添加 Bootstrap 按鈕類添加按鈕文本

    See all articles