今天是鄧呆呆球衣退役的日子,在這個頗具紀念意義的日子裏我寫下自己的第一篇博客,還望前輩們多多提攜,多多指教!
接下來,就進入正文,來說說關於垂直居中的事。(以下這11種垂直居中的實現方式均爲筆者在日常的開發過程中總結得到,如發現和您的觀點確有雷同之處,那麼實在榮幸能和前輩們英雄所見略同!)
另:這裏的demo都只是針對現代瀏覽器所做,未兼容低版本的IE以及其它非主流瀏覽器。
11種實現方式分別如下:
<div id="box"> <div id="child">我是測試DIV</div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; position: relative; } #child { width: 150px; height: 100px; background: orange; position: absolute; top: 50%; margin: -50px 0 0 0; line-height: 100px; }
運行結果如下:
<div id="child"> 我是一串很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長的文本 </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; position: relative; } #child { background: #93BC49; position: absolute; top: 50%; transform: translate(0, -50%); }
運行結果如下:
<div id="box"> <div id="child">我也是個測試DIV</div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; position: relative; } #child {
width: 50%; height: 30%; background: pink; position: absolute; top: 50%; margin: -15% 0 0 0; }
運行結果如下:
<div id="box"> <div id="child">呆呆今天退役了(。﹏。)</div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; position: relative; } #child { width: 200px; height: 100px; background: #A1CCFE; position: absolute; top: 0; bottom: 0; margin: auto; line-height: 100px; }
運行結果如下:
<div id="box"> <div id="child">今天西安的霾嚴重的嚇人,剛看了一眼PM2.5是422</div> </div>
css代碼:
#box { width: 300px; background: #ddd; padding: 100px 0; } #child { width: 200px; height: 100px; background: #F7A750; line-height: 50px; }
運行結果如下:
<div id="box"> <div id="base"></div> <div id="child">今天寫了第一篇博客,希望可以堅持寫下去!</div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; } #base { height: 50%; background: #AF9BD3; } #child { height: 100px; background: rgba(131, 224, 245, 0.6); line-height: 50px; margin-top: -50px; }
運行結果如下:
<div id="box">霧霾天氣,太久沒有打球了</div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; display: flex; align-items: center; }
運行結果如下:
這種方式同樣適用於塊級元素:
html代碼:
<div id="box"> <div id="child"> 程序員怎麼才能保護好眼睛? </div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; display: flex; align-items: center; } #child { width: 300px; height: 100px; background: #8194AA; line-height: 100px; }
運行結果如下:
<div id="box"> <div id="child"> 答案當然是多用綠色的背景哈哈 </div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; display: flex; flex-direction: column; justify-content: center; } #child { width: 300px; height: 100px; background: #08BC67; line-height: 100px; }
運行結果如下:
<div id="box"> 我是一段測試文本 </div>
css代碼:
#box{ width: 300px; height: 300px; background: #ddd; line-height: 300px; }
運行結果如下:
<div id="box"> <img src="duncan.jpeg"> </div>
css代碼:
#box{ width: 300px; height: 300px; background: #ddd; line-height: 300px; } #box img { vertical-align: middle; }
運行結果如下:
<div id="box"> <div id="child">我也是一段測試文本</div> </div>
css代碼:
#box { width: 300px; height: 300px; background: #ddd; display: table; } #child { display: table-cell; vertical-align: middle; }
運行結果如下:
這裏關於vertical-align囉嗦兩句:vertical-align屬性只對擁有valign特性的html元素起作用,例如表格元素中的<td><th>等等,而像<div><span>這樣的元素是不行的。
valign屬性規定單元格中內容的垂直排列方式,語法:<td valign="value">,value的可能取值有四種: