DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML和Xhtml
HTML 水平垂直居中問題小結:文字與元素篇
編輯:HTML和Xhtml     

最近接連遇到各種居中問題,乾脆花點時間整理成一篇筆記,之後要查也方便。內容分成兩大類:讓文字居中,以及讓元素居中。

一、讓文字居中

結構:

<div class="wrap">
 我在中間……
</div>

1.1 單行文字:height + line-height + text-align

.wrap{
  width: 200px;
  height: 100px;
  border: 1px solid red;
  text-align: center;
  line-height: 100px;   /* 與 height 相同,文字即垂直居中 */
}

注意:text-align: center 只是讓元素內部的行內內容水平居中,並不會讓元素本身置中。而這招只能處理單行,一旦文字換行就會失效。

1.2 多行固定高度:display: table-cell

.wrap{
  width: 200px;
  height: 100px;
  border: 1px solid red;
  text-align: center;
  display: table-cell;
  vertical-align: middle;
}

display: table-cell 在 IE6/7 下無效,實務上最好搭配 display: table 一起使用。

IE6/7 的舊式解法(僅供參考)

方法一:讓 em 的高度與父層等高等,如此 span 與 em 的垂直對齊就等同於 span 在父層中垂直居中。

<div class="wrap">
  <span>
    我在中間…… 我在中間……
  </span>
  <em></em>
</div>

.wrap{
  width: 200px;
  height: 100px;
  border: 1px solid red;
  text-align: center;
}
.wrap span{
  vertical-align: middle;
  display: inline-block;
  width: 100px;
}
.wrap em{
  height: 100%;
  vertical-align: middle;
  display: inline-block;
}

方法二:為子元素多包一層絕對定位的父層,再配合子元素的相對定位完成水平垂直居中。

<div class="wrap">
  <span class="span">
    <span class="span">我在中間…… 我在中間……</span>
  </span>
</div>

.wrap{
  width: 200px;
  height: 100px;
  border: 1px solid red;
  display: table;
  position: relative;
  overflow: hidden;
}
.wrap .span{
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  *position: absolute;   /* 僅 IE6/7 讀得懂的 hack */
  top: 50%;
  left: 50%;
}
.wrap .span{
  *position: relative;
  top: -50%;
  left: -50%;
}

1.3 用 padding 硬撐

最樸素的做法:靠內距把內容推到位。

.wrap{
  width: 200px;
  border: 1px solid red;
  padding: 50px;
}

二、讓元素居中

結構:

<div class="wrap">
  <span></span>
</div>

2.1 position: absolute + margin 負值

前提是元素必須有明確的寬高,才能計算出負 margin。

.wrap{
  width: 200px;
  height: 200px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -100px;
  border: 1px solid red;
}
.wrap span{
  width: 100px;
  height: 100px;
  background: red;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -50px;
}

完整可運行的範例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>上下垂直居中 線上演示 DIVCSS5</title>
<style>
#main {
  position: absolute;
  width: 400px;
  height: 200px;
  left: 50%;
  top: 50%;
  margin-left: -200px;
  margin-top: -100px;
  border: 1px solid #00F
}
</style>
</head>
<body>
  <div id="main">DIV 水平居中和上下垂直居中<a href="https://www.divcss5.com/">DIVCSS5</a></div>
</body>
</html>

原理說明

這裡用的是絕對定位 position: absolute,先以 left 與 top 把元素左上角移到包裝容器的 50% 位置。但單靠 50% 其實並沒有真正置中——元素的左上角落在中心點上,整體偏右下。因此還要補上 margin-left: -200px 與 margin-top: -100px。

關鍵技巧就在這裡:margin-left 的值取元素寬度的一半、margin-top 取高度的一半,且兩者都設為負值。這樣就能把元素拉回真正的中心,完成水平與垂直雙向居中。

當然,這個做法的前提是元素尺寸已知。若寬高不確定,改用 transform: translate(-50%, -50%) 或 Flexbox 會更省事——那兩種方式不需要寫死任何像素值。

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