DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> 前端技巧
div 垂直居中的五種解法:單行、多行、已知與未知高度
編輯:前端技巧     

常有人問:CSS 不是有 vertical-align 嗎?問題在於這個屬性只對擁有 valign 特性的元素生效,例如 <td>、<th>、<caption>。<div> 與 <span> 沒有這個特性,因此設了也沒用。

一、單行文字:line-height 等於 height

最簡單的情形。讓行高等於容器高度即可:

div {
  height: 25px;
  line-height: 25px;
  overflow: hidden;
}

overflow: hidden 是保險,避免內容溢出或自動換行破壞居中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>單行文字垂直居中</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body { font-size: 12px; font-family: tahoma; }
div {
  height: 25px;
  line-height: 25px;
  border: 1px solid #FF0099;
  background-color: #FFCCFF;
}
</style>
</head>
<body>
<div>現在我們要使這段文字垂直居中顯示!</div>
</body>
</html>

二、多行、高度未知:靠 padding 撐開

內容高度可變時,讓上下 padding 相同:

div {
  padding: 25px;
}

這其實是「看起來居中」——文字把 div 填滿了,所以上下留白自然對稱。優點是相容性極好、程式碼極短;前提是容器高度必須可伸縮。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>多行文字垂直居中</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body { font-size: 12px; font-family: tahoma; }
div {
  padding: 25px;
  border: 1px solid #FF0099;
  background-color: #FFCCFF;
  width: 760px;
}
</style>
</head>
<body>
<div><pre>現在我們要使這段文字垂直居中顯示!</pre></div>
</body>
</html>

三、多行、高度固定:讓 div 假裝成表格

既然 vertical-align 對表格有效,那就用 display: table 與 display: table-cell 讓 div 模仿表格行為。前者放在父元素,後者放在子元素:

div#wrap {
  height: 400px;
  display: table;
}
div#content {
  vertical-align: middle;
  display: table-cell;
  border: 1px solid #FF0099;
  background-color: #FFCCFF;
  width: 760px;
}
<div id="wrap">
  <div id="content">現在我們要使這段文字垂直居中顯示!</div>
</div>

這個方案很漂亮,但 IE6 及更早版本完全不認 display: table,因此當年還需要下一招。

四、IE6 的定位百分比繼承特性

IE6 在高度計算上有一個特殊行為:當父元素被絕對定位後,子元素的百分比定位基準會「繼承」父元素的定位高度,而不是自身高度。

<div id="wrap">
  <div id="subwrap">
    <div id="content"></div>
  </div>
</div>

若 subwrap 設為 top: 50%,則 content 要回到原位需設 top: -100%;若把 content 設為 -50%,它就正好落在垂直中線上。

div#wrap {
  border: 1px solid #FF0099;
  background-color: #FFCCFF;
  width: 760px;
  height: 400px;
  position: relative;
}
div#subwrap {
  position: absolute;
  top: 50%;
}
div#content {
  position: relative;
  top: -50%;
}

這段代碼只在存在該計算缺陷的舊版 IE 上才會生效。

五、合併方案(含 CSS Hack)

把標準寫法與 IE6 hack 用底線前綴分流,兩邊都能正確顯示:

div#wrap {
  display: table;
  border: 1px solid #FF0099;
  background-color: #FFCCFF;
  width: 760px;
  height: 400px;
  _position: relative;
  overflow: hidden;
}
div#subwrap {
  vertical-align: middle;
  display: table-cell;
  _position: absolute;
  _top: 50%;
}
div#content {
  _position: relative;
  _top: -50%;
}

底線前綴只有 IE6 認得,其他瀏覽器會忽略該宣告。

今天只需要記兩件事

上述五招是歷史產物。現代的標準答案是:

/* 已知或未知高度皆可 */
.parent {
  display: flex;
  align-items: center;      /* 垂直 */
  justify-content: center;  /* 水平 */
}

/* 或使用 grid,程式碼更短 */
.parent {
  display: grid;
  place-items: center;
}

單一行文字仍可用 line-height,這是最省資源的做法。

附帶一提容易混淆的兩個關鍵字:垂直居中在 vertical-align 中寫作 middle,而水平居中在 text-align 中寫作 center。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved