日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

用CSS让DIV上下左右居中的方法

發布時間:2023/12/2 CSS 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用CSS让DIV上下左右居中的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

例如 一個父div(w:100%;h:400px)中有一個子div(w:100px;100px;)。讓其上下左右居中。

方法一(varticle-align

理念

利用表格單元格的居中屬性。

步驟

  • 父div外層配置一個div,同時設置為表格元素 (display: table),寬度為100%
  • 父div配置為表格單元格元素 (display: table-cell)
  • 父div配置居中屬性(vertical-align: middle),使子div上下居中
  • 子div通過margin配置左右居中(margin-left:auto; margin-right:auto)

例子

<style>* {margin: 0; padding: 0; box-sizing: border-box;}.table {display: table; width: 100%;}.father {display: table-cell; vertical-align: middle;}.son {margin: auto;} </style> <body><div class="table" ><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div></div> </body>

注:

  • 表格單元格比較特殊,如果只有一個單元格時,它的寬度默認會占父級(table|tr)寬度的100%;
  • table默認寬度不會撐開,需要手動配置width:100%;

方法二(line-height

理念

當父div的行高等于自身高度時,內部的行內元素會上下居中顯示。行內塊沒有固定高度時也會上下居中顯示。通過文本居中屬性

text-align:center,可以使內部行內元素或行內塊元素左右居中顯示。

步驟

  • 子div設定為行內塊元素(display:inline-block);
  • 父div設置行高(line-height)使子div上下居中
  • 父div設置文本居中(text-align:center)使子div左右居中。

例子

<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {line-height: 500px; text-align: center; font-size: 0;}.son { display: inline-block;/* display: inline-flex;display: inline-grid;display: inline-table; */} </style> <body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div> </body>

注: 行高如果設置為當前父div的高度(400px)的話,有固定高度的子div并不會居中顯示的,問題出在瀏覽器默認將其當做文本居中的,即把它當做了一段文本(chrome默認font-size:16px;hight:21px)進行居中,沒把它當做高度100px進行居中。所以需要對父div的

line-height進行調整。以 font-size:0(對應的字體高度為0)為例子,則需要line-height增加一個子div的高度(400px 100px;)。

方法三(絕對定位

理念

利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。

步驟

  • 父div標記下定位(position:relative|absolute|fixed);子div絕對定位(position:absolute)
  • 子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:50%;margin-bottom:-h/2;;
  • 子div左右居中: left:50%;margin-left:-w/2 或是 right:50%;margin-right:-w/2;

例子

<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {position: relative;}.son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px;} </style> <body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div> </body>

方法四(絕對定位)

理念:

定位屬性top和bottom(或是left和right)值分別設置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設置margin:auto會使它居中顯示。

步驟:

  • 父div標記下定位(position:relative|absolute|fixed|sticky);子div絕對定位(position:absolute)
  • 子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
  • 子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto

例子

<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {position: relative;}.son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto} </style> <body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div> </body>

方法五(flex)

理念

彈性盒子中只要給彈性子元素設置 margin: auto; 可以使得彈性子元素上下左右居中。

例子

<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {display: flex;}.son {margin: auto} </style> <body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div> </body>

flex兼容性,以及存在的已知問題

方法六(相對定位)

理念

利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。

步驟

  • 父div標記下定位(position:relative|absolute|fixed);子div相對定位(position:relative)
  • 子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:-50%;margin-top:-h/2;;
  • 子div左右居中: left:50%;margin-left:-w/2 或是 right:-50%;margin-left:-w/2;

例子

<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {position: relative;}.son {position: relative; top:50%;margin-top:-50px;left:50%;margin-left:-50px} </style> <body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div> </body>

注:

  • 與絕對定位不同的是,定位屬性相對的點是自身元素的左上角。(所以bottom是負值,和方法三絕對定位寫法有不一樣的地方)
  • 如果有同級元素,可能會受到影響。
  • 如果子div是浮動元素,也有居中效果。

結尾

方法二和方法三兼容性要比其它好些。最簡單的是彈性盒子。

補充

margin負值可以用2D轉換代替。優點是不用計算子div的寬高。

margin-top: -h/2 => transform: translateY(-50%)
margin-bottom: -h/2 => transform: translateY(50%)
margin-left: -h/2 => transform: translateX(-50%)
margin-right: -h/2 => transform: translateX(50%)

參考資料

Can I use
css-vertical-center-solution
CSS實現垂直居中的5種方法--前端觀察

相關指導

盼先生

總結

以上是生活随笔為你收集整理的用CSS让DIV上下左右居中的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。