1. 程式人生 > >css水平垂直居中方法介紹

css水平垂直居中方法介紹

在專案中經常會碰到對元素進行垂直水平居中的需求,下面就來介紹一下不同場景可用的方法!

一,水平居中方法

1,text-align: center

    對於inline 和 inline-block元素,可以對其父元素新增text-align: center樣式

<div style="text-align: center;">  
  <div style="display: inline;">this is inline or inline-block element, it parent use text-align: center</div>  
</div>

2,margin: auto

    對於block元素,可以對它新增margin: auto樣式

<div style="margin: 0 auto;">  
  <div style="width: 200px;">this is block element, it parent use margin: auto</div>  
</div>

3,position: absolute + margin

    對於inline-block 和 block 元素,如果知道它的寬度,可以對其父元素新增position: relative樣式, 自身新增position: absolute和margin樣式

<div style="position: relative;">  
  <div style="width: 200px; position: absolute; left: 50%; margin-left: -100px;">  
    this is inline-block or block element, it use position: absolute + margin  
  </div>  
</div>

4,position: absolute + transform

    對於inline-block 和 block 元素,如果不知道它的寬度,可以對其父元素新增position: relative樣式, 自身新增position: absolute和transform樣式

<div style="position: relative;">  
  <div style="display: inline-block; position: absolute; left: 50%; transform: translateX(-50%);">  
    this is inline-block or block element, it use position: absolute + transform  
  </div>  
</div>

5,position: absolute + margin: auto

    對於inline-block 和 block 元素,如果知道它的寬度,可以對其父元素新增position: relative樣式, 自身新增position: absolute和margin樣式

<div style="position: relative;">  
  <div style="display: inline-block; position: absolute; left: 0; right: 0; margin: auto; width: 200px;">  
    this is inline-block or block element, it use position: absolute + margin: auto  
  </div>  
</div>

6,display: flex

    flex佈局適合任意型別的元素,可以對父元素新增display: flex + justify-content: center樣式

<div style="display: flex; justify-content: center;">  
  <div>this is block element, it parent use display: flex</div>  
</div>

二,垂直居中方法

1,line-height

    對於單行字元,可以對其父元素新增line-height樣式

<div style="height: 200px; line-height: 200px;"> 
  this is one line text, it parent use line-height  
</div>

2,vertical-align: middle + 空div

    對於inline(單行字元) 和 inline-block元素,可以對其新增vertical-align: middle樣式,並且緊接著在後面新增一個空div(要緊挨著前面元素)

<div style="text-align: center;">  
  <div style="display: inline; vertical-align: middle;">this is inline or inline-block element, it use   
vertical-align: middle</div><div style="display: inline-block; height: 100%; vertical-align: middle;"></div>  
</div>

3,display: table-cell + vertical-align: middle

    對於任意型別元素,可以對其父元素新增display: table-cell + vertical-align: middle樣式

<div style="display: table-cell; vertical-align: middle;">  
  <div style="display: inline;">this is inline or inline-block   
or block element, it parent use display: table-cell + vertical-align: middle</div>  
</div>

4,position: absolute + margin

    對於inline-block 和 block 元素,如果知道它的高度,可以對其父元素新增position: relative樣式, 自身新增position: absolute和margin樣式

<div style="position: relative;">  
  <div style="height: 200px; position: absolute; top: 50%; margin-top: -100px;">  
    this is inline-block or block element, it use position: absolute + margin  
  </div> 
</div>

5,position: absolute + transform

    對於inline-block 和 block 元素,如果不知道它的高度,可以對其父元素新增position: relative樣式, 自身新增position: absolute和transform樣式

<div style="position: relative;">  
  <div style="display: inline-block; position: absolute; top: 50%; transform: translateY(-50%);">  
    this is inline-block or block element, it use position: absolute + transform  
  </div>  
</div>

6,position: absolute + margin: auto

    對於inline-block 和 block 元素,如果知道它的高度,可以對其父元素新增position: relative樣式, 自身新增position: absolute和margin樣式

<div style="position: relative;">  
  <div style="display: inline-block; position: absolute; top: 0; bottom: 0; margin: auto; height: 200px;">  
    this is inline-block or block element, it use position: absolute + margin: auto  
  </div>  
</div>

7,display: flex

    flex佈局適合任意型別的元素,可以對父元素新增display: flex + justify-content: center樣式

<div style="display: flex; justify-content: center;">  
  <div>this is block element, it parent use display: flex</div>  
</div>

三,水平垂直居中方法

1,text-align + line-height

    對於inline(單行文字)元素,可以對其父元素新增text-align: center + line-height樣式


<div style="text-align: center; line-height: 200px; height: 200px;">  
    <div style="display: inline;">this is inline element</div>  
</div>

2,text-align + vertical-align + 空div

    對於inline(單行文字) 和 inline-block元素,可以對其父元素新增text-align: center樣式, 自身新增vertical-align: middle樣式

(注:空div要緊挨著前面元素,不能換行)

<div style="text-align: center;">  
    <div style="display: inline;vertical-align: middle;">this is inline or inline-block element</div><div   
    style="vertical-align: middle; display: inline-block; height: 100%;"></div>  
</div>

3,text-align + table-cell

    對於inline 和 inline-block元素,可以對其父元素新增text-align: center + dispaly: table-cell樣式

<div style="display: table-cell; text-align: center; vertical-align: middle;">  
  this is inline or inline-block element, it parent use text-align + table-cell  
</div>

4,margin: auto + table-cell

對於block元素,可以對其父元素新增display: table-cell + vertical-align: middle樣式, 自身新增margin: 0 auto樣式

<div style="display: table-cell; vertical-align: middle;">  
  <div style="margin: 0 auto; width: 200px;">  
    this is block element, it parent use display: table-cell + vertical-align: middle</div>  
</div>

5,position + margin

    對於inline-block 和 block(知道自身寬度和高度)元素,可以對其父元素新增position: relative樣式, 自身新增position: absolute + margin樣式


<div style="position: relative;">  
  <div style="width: 200px; height: 100px; position: absolute; top: 50%; left: 50%; margin: -50px 0 0 -100px;"  
  >this is inline-block or block element, it parent use position + margin</div>  
</div>

6,position + transform

    對於inline-block 和 block(不知道自身寬度和高度)元素,可以對其父元素新增position: relative樣式, 自身新增position: absolute + transform樣式


<div style="position: relative;">  
  <div style="display: inline-block; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);"  
  >this is inline-block or block element, it parent use position + transform</div>  
</div>

7,position + margin: auto

    對於inline-block 和 block(知道自身寬度和高度)元素,可以對其父元素新增position: relative樣式, 自身新增position: absolute + margin: auto樣式


<div style="position: relative;">  
  <div style="display: inline-block; width: 200px; height: 100px; position: absolute; top: 0; left: 0;   
  right: 0; bottom: 0; margin: auto;">this is inline-block or block element, it parent use position + margin: auto</div>  
</div>

8,display: flex

    flex佈局適合任意型別的元素,可以對父元素新增display: flex + justify-content: center + align-items: center樣式


<div style="display: flex; justify-content: center; align-items: center;">  
  <div>this is block element, it parent use display: flex</div>  
</div>  

相關推薦

css水平垂直居中方法介紹

在專案中經常會碰到對元素進行垂直水平居中的需求,下面就來介紹一下不同場景可用的方法!一,水平居中方法1,text-align: center    對於inline 和 inline-block元素,可以對其父元素新增text-align: center樣式<div s

10種CSS水平垂直居中方法

10 種css 水平垂直居中方法 參考地址:https://mp.weixin.qq.com/s/uTnMr4lv_Hrlt2TH9A01gA (直接網上搜索到的地址,人家整理的比較好) 編寫該博文僅僅作為梳理,鞏固學習,加強記憶。 場景一:居中元素寬高已知

53.CSS---CSS水平垂直居中常見方法總結

out 嵌套 span 垂直 ems 寬度 分享圖片 .net tag CSS水平垂直居中常見方法總結 1、元素水平居中 當然最好使的是: margin: 0 auto; 居中不好使的原因: 1、元素沒有設置寬度,沒有寬度怎麽居中嘛! 2、設置了寬度依然不好使,

css實現水平垂直居中方法總結

form 位置 absolute div 技術 .com 完成 flex image 方法一:使用position 元素已知寬度 <div class="box"> <div class="content"></div> <

css實現水平垂直居中方法

html: <div class="box box1"> <span>垂直居中</span> </div> 法一: .box1{ display: table-cell; vertical-align: mi

css水平垂直居中解決方法

css居中方案是一個老生常談的問題,主要包括水平居中和垂直居中,水平居中大家用的比較多,最常用的莫過於margin:0 auto方案了,而垂直居中,很多時候會讓很多新手頭疼。 常用居中方案(水平) margin:0 auto解決方案 (水平居中) 適用於已經知道寬度

CSS元素(文字、圖片)水平垂直居中方法

  1、text-align:center; 2、margin:0 auto; 3、display:inline-block; + text-align:center; 4、position:relative; + float:left; 5、line-height 6、上下左右padd

CSS水平垂直居中常見方法總結

說明:本篇文章只是總結一些方法,例子用到的各個元素屬性不做解釋,詳情請看MDN文件,非常的詳盡,例子在chrome瀏覽器下完全好使,IE這個渣渣 附上鍊接:https://developer.mozi

CSS 水平垂直居中的幾種實現方法

ren parent frame right iframe 偏移 orm 結構 img 前言 項目中經常碰到需要實現水平垂直居中的樣式。下面就總結幾種常用的方法 水平對齊+行高 【思路一】text-align + line-height實現單行文本水平垂直居中 <st

CSS水平垂直居中

水平居中 ron lac 表現 -a posit ble ive width 水平居中 HTML代碼: 1 <div class="parent"> 2 <div class="child"></div> 3 </div&

div垂直居中-CSS元素垂直居中方法

div垂直居中-CSS元素垂直居中方法分2類: 1、文字垂直居中的解決方案-2種 https://edu.csdn.net/course/play/9950/211677 2、塊級元素垂直居中的解決方案-6種 https://edu.csdn.net/course/play/9950

未知寬高元素水平垂直居中方法

CSS未知寬高元素水平垂直居中 方法一 思路:顯示設定父元素為:table,子元素為:cell-table,這樣就可以使用vertical-align: center,實現水平居中 優點:父元素(parent)可以動態的改變高度(table元素的特性) 缺點:IE8以下不支援 <style typ

div盒子水平垂直居中方法?

一: 盒子沒有固定的寬和高 1. 在父級元素上面加上上面3句話,就可以實現子元素水平垂直居中。 <div class="wrapper"> 我不知道我的寬度和高是多少,我要實現水平垂直居中。 </div>   .wrappe

HTML+CSS水平垂直居中

  啦啦啦,好了,今天來分享自己的第一個知識點,難得自己還能想起來過來部落格園,寫寫部落格的。   好了,言歸正傳,今天分享關於html和css的一個簡單的知識點,對於大部分從事前端開發的人員來說可能都是很簡單的,但是,對於我這種患有嚴重健忘症的人還有一些初入前端的小夥伴來說,整理一下可能是有百利而無一害的

塊級元素水平垂直居中方法

一、加padding減height <!DOCTYPE html> <html lang="en"> <head> <meta charset="

div盒子水平垂直居中方法

這個問題比較老,方法比較多,各有優劣,著情使用。 一、盒子沒有固定的寬和高 方案1、Transforms 變形 這是最簡單的方法,不僅能實現絕對居中同樣的效果,也支援聯合可變高度方式使用。內容塊定義transform: translate(-50%,-50%)  必須加上 top: 50%; left: 50

CSS 水平垂直居中

塊級元素 水平垂直居中 <div class="wrap"> <div class="box">fdsa</div> </div> 1.方法一 .wrap { width:

css 水平垂直居中 兩端對齊

單行垂直居中 單行垂直居中可以直接用line-height=width來做 <div  style="width:100px;height:100px;line-height:100px;"><span>hello world</span>

HTML篇之CSS樣式——CSS水平垂直居中對齊(多種方式)

用CSS來實現元素的垂直居中效果是件苦差事,雖然說實現方法有多種,但有很多方式在某些瀏覽器下可能無法正常的工作。接下來我們就一起來看看這些不同方法實現垂直居中的各自優點和其不足之處。 方法一:這種方法用來實現單行垂直居中是相當的簡單的,你只要保證元素內容是單行,並且其高度是

CSS元素水平垂直居中方法

垂直 .com put ron spa blank span osi 絕對定位 1. 元素水平居中 1.1 設置父元素的屬性 text-align: center; 說明:此屬性只針對父元素的子元素為內聯元素時有效,比如:img,input,select,but