用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種方法--前端觀察

相關指導

盼先生

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/370019.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/370019.shtml
英文地址,請注明出處:http://en.pswp.cn/news/370019.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

功能性Java集合

如今&#xff0c;在功能上大肆宣傳&#xff0c;因此至少在Java集合方面&#xff0c;我將簡要介紹一下其中的功能。 我個人喜歡標準 集合API&#xff0c;但在某些情況下可能會很尷尬并添加其他詳細信息。 在Java 8的更高版本中&#xff0c;這應該不是問題。 在那里&#xff0c;…

python繪制帕累托圖

python繪制帕累托圖代碼 1 import pandas as pd2 import matplotlib.pyplot as plt3 plt.rcParams[font.sans-serif][SimHei]#表示可以顯示中文4 plt.rcParams[axes.unicode_minus]False#表示可以正常顯示正負號5 datapd.read_csv(catering_dish_profit.csv,index_coltype)6 pr…

currentStyle、getComputedStyle 獲取樣式

style.height 獲取的是行間的樣式 currentStyle.height、getComputedStyle(elem,null).height 獲取的是 div 的 content 的寬高&#xff0c; clientHeight 獲取的是 contentpadding&#xff0c; offsetHeight 獲取的是contentpaddingborder。 <script> window.onload…

html5 測評游戲,暗黑之王評測:HTML5游戲鑄就最華麗ARPG冒險

由白鷺時代(Egret Technology)與比悅科技聯手推出的重度大型HTML5游戲《暗黑之王》&#xff0c;一款典型的ARPG手游&#xff0c;其HTML5版本推出以來&#xff0c;獲得了來自業界、玩家和媒體的大量關注。其豐富的游戲內容和玩法&#xff0c;加上卓越的游戲性能表現&#xff0c;…

搞定flex布局

這幾種方式的搭配使用可以輕松搞定 PC 端頁面的常見需求&#xff0c;比如實現水平居中可以使用 margin: 0 auto&#xff0c;實現水平垂直同時居中可以如下設置&#xff1a;.dad {position: relative; } .son {position: absolute;margin: auto;top: 0;right: 0;bottom: 0;left…

Java基礎5一數組的常見應用算法

常用算法 1.冒泡排序: 原理&#xff1a;比較兩個相鄰的元素&#xff0c;將值大的元素交換至右端 示例: public static void bubbleSort(int[] a) {int n a.length;//總共進行n-1輪的比較for (int i 1; i < n; i) {for (int j 0; j < n - i; j) {if (a[j] > a[j 1]…

使用Xtend構建Vaadin UI

今天&#xff0c;我決定向Xtend打個招呼。 我希望學習一些新的編程語言。 選擇一個標準的清單并不多。 它必須是在JVM上運行的編程語言&#xff0c; 如果我不需要學習用于建筑應用的全新生態系統&#xff0c;那就太好了。 我已經檢查了幾個選項。 JVM的編程語言列表已不多了…

python 瀏覽器顯示本地文件夾_瀏覽器讀取本地文件

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云文件存儲NAS是一個可共享訪問&#xf…

p15頁

給你一個n*n的01矩陣&#xff0c;只能夠將0變成1&#xff0c;使得每個元素上下左右之和均是偶數&#xff0c; 比如 0 0 0 0 1 0 1 0 0>>>>1 0 1 0 0 0 0 1 0 一個轉變了3步 多實例T 然后一個n&#xff08;n<15&#xff09; 表示n*n的矩陣 …

html 登陸sql server,jsp實現注冊與登錄頁面+sqlsever2008

//index.jspString path request.getContextPath();String basePath request.getScheme()"://"request.getServerName()":"request.getServerPort()path"/";%>登陸用戶登陸用戶名&#xff1a;密 碼:注冊新用戶//Logon.jspString path req…

百度前端學院-基礎學院-第四課

今天是第四天&#xff0c;進度可以&#xff0c;表揚一下自己。 今天的課程目標是&#xff1a;掌握 CSS 稍微復雜的一些選擇器&#xff0c;還有背景&#xff0c;邊框等一些 CSS 樣式屬性。 CSS背景&#xff1a; 背景色&#xff1a;background-color:gray; 背景圖&#xff1a…

JUnit測試方法訂購

直到4.10版為止的Junit都使用反射API返回的測試類中測試方法的順序作為測試方法執行的順序– Class.getMethods&#xff08;&#xff09; 。 引用getMethods&#xff08;&#xff09;api的Javadoc&#xff1a; 返回的數組中的元素未排序&#xff0c;并且沒有任何特定順序。 …

html中padding和margin的區別和用法與存在的bug消除

關于margin&#xff1a;在需要border外側添加距離時。空白處不需要背景時。相連的兩個部分的地方需要加外邊的邊距時使用。 關于padding&#xff1a;在需要border內側添加距離時。空白處需要背景時。相連的兩個部分的地方需要加內部的邊距時使用。 IE6中雙邊距Bug&#xff1a; …

python發微信提醒天氣冷了注意保暖_2019天氣變冷的朋友圈說說 注意保暖的微信問候語...

1.天冷了&#xff0c;注意添加衣物&#xff0c;別著涼。可你還是著涼了。看你難受的樣子&#xff0c;我的心&#xff0c;唉&#xff0c;只有一句話能表達&#xff1a;小樣&#xff0c;你也有今天&#xff01;為了不讓我得逞&#xff0c;你要注意身體哦。2.天氣變涼要注意&#…

Fiddler抓包使用教程-QuickExec

轉載請標明出處&#xff1a;http://blog.csdn.net/zhaoyanjun6/article/details/73468287 本文出自【趙彥軍的博客】 在 Fiddler 中自帶了一個 QuickExec 命令行&#xff0c;用戶可以直接輸入并快速執行腳本命令。 常見命令 help 打開官方的使用頁面介紹&#xff0c;所有的命令…

自己整理的css3動畫庫,附下載鏈接

動畫調用語法 animation: bounceIn 0.3s ease 0.2s 1 both; 按順序解釋參數&#xff1a; 動畫名稱 如&#xff1a;bounceIn 一周期所用時間 如&#xff1a;0.3s 速度曲線 如&#xff1a;ease 值 描述 linear 動畫從頭到尾的速度是相同的。 ease 默認。動畫以低速開始&#xff0…

帶有Spring的REST的ETag

1.概述 本文將重點介紹ETags-Spring支持&#xff0c;RESTful API的集成測試以及帶有curl的使用場景。 這是關于使用Spring 3.1和Spring Security 3.1和基于Java的配置來建立安全的RESTful Web服務的系列文章的第9篇。 REST with Spring系列&#xff1a; 第1部分 – 使用Spring…

html5與css3都要學嗎,前端要學css3嗎?

前端要學css3&#xff1b;HTML5、CSS3是前端工程師必須要學會。現在移動端的興起&#xff0c;導致web前端開發的技術逐變向css3和html5轉變&#xff0c;所以css3一定要學。CSS3是CSS(層疊樣式表)技術的升級版本&#xff0c;于1999年開始制訂&#xff0c;2001年5月23日W3C完成了…

PHP中cookie和session的區別

1、cookie數據存放在客戶的瀏覽器上&#xff0c;session數據放在服務器上。 2、cookie不是很安全&#xff0c;別人可以分析存放在本地的COOKIE并進行COOKIE欺騙考慮到安全應當使用session。 3、session會在一定時間內保存在服務器上。當訪問增多&#xff0c;會比較占用你服務器…

ubuntu下anaconda3+pygame

有是很無語的地方&#xff0c;網上教程一堆&#xff0c;又是要下載什么包&#xff0c;然后又是什么依賴亂七八糟的整一堆。都不知道怎么想的 試了 sudo apt-get install python-pygame 這個是行不通的&#xff01;&#xff01;&#xff01;根本沒有任何卵用 害我捯飭了半天&am…