利用CSS進行元素的水平居中,行級元素設定其父元素的text-align center,區塊級元素設定本身的left和right margins為auto即可。本文收集了六種利用css進行元素的垂直居中的方法,一起看看吧!
Line-Height Method
#試用:單行文字垂直居中,demo
程式碼:
html
<p> </p><p>Text here</p>
css
##child { line-height: 200px; }
垂直居中一張圖片,程式碼如下
html
#<p> <img src="/static/imghwm/default1.png" data-src="image.png" class="lazy" alt="淺談使用CSS實現元素垂直居中的幾種方法" > </p>
css
##parent { line-height: 200px; } #parent img { vertical-align: middle; }
CSS Table Method
#適用:通用,demo
##程式碼:html
Content here
css##
#parent {display: table;} #child { display: table-cell; vertical-align: middle; }
低版本 IE fix bug:
#child { display: inline-block; }
Absolute Positioning and Negative Margin
#適用:區塊級元素,demo
#程式碼:
html
<p> </p><p>Content here</p>
css
##parent {position: relative;} #child { position: absolute; top: 50%; left: 50%; height: 30%; width: 50%; margin: -15% 0 0 -25%; }
Absolute Positioning and Stretching
#適用:通用,但在IE版本低於7時無法正常工作,demo#
##程式碼:html
Content here
css
##parent {position: relative;} #child { position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 50%; height: 30%; margin: auto; }Equal Top and Bottom Padding
Content here
css## Floater p 適用:通用,demo 程式碼: ##html
Content here#parent {
padding: 5% 0;
}
#child {
padding: 10% 0;
}
css
##parent {height: 250px;} #floater { float: left; height: 50%; width: 100%; margin-bottom: -50px; } #child { clear: both; height: 100px; }以上就是六種方法,可以在實際的使用過程中合理選擇.更多程式相關知識,請造訪:程式設計入門! !

了解如何通過Chris Coyier實施WordPress的Gutenberg編輯器來創建一個自定義Codepen塊,並為Sanity Studio提供預覽。

格子呢是一塊圖案布,通常與蘇格蘭有關,尤其是他們時尚的蘇格蘭語。在Tartanify.com上,我們收集了5,000多個格子呢


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。