讓文本居中很簡單,一句 text-align: center 就好。但本文說的「元素」指的是容器——用 div 來指代最為貼切。
讓容器水平居中,對網頁設計師來說並不陌生。可有時我會想:為什麼要讓它居中?背後的原因是什麼?這裡記錄一些個人見解。
要談居中,得先弄清 CSS 設計中固定布局與流式布局的區別。最直觀的判斷標準,就是看有沒有給元素設定固定寬度。
固定布局示例:
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
.wrapper{width:750px;position:relative;margin:0 auto;text-align:left;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
流式布局示例:
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>col3-margin-layout</title>
<style type="text/css">
.contentArea{margin:0 160px;height:500px;background:#96c;}
.leftPanel{width:150px;float:left;height:500px;background:#999;}
.rightPanel{width:150px;float:right;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="leftPanel"></div>
<div class="rightPanel"></div>
<div class="contentArea"></div>
</div>
</body>
</html>
由這兩個例子不難看出:流式布局是滿屏鋪展的,本身不存在水平居中的概念;只有固定布局因為限定了寬度,才談得上讓元素水平居中。
而固定布局之所以要居中,是為了讓瀏覽器左右兩側留出等寬的留白,而不是一邊緊貼邊緣、另一邊空出一大片,影響觀感。
這是最為人熟知的做法:給容器設定寬度,再寫上 margin: 0 auto。
<style type="text/css">
.wrapper{width:750px;margin:0 auto;position:relative;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
這段程式碼在當前主流瀏覽器(包含 IE6)下都能正常居中,只有 IE6 以下版本不生效,元素會繼續靠左排列。如果專案不需要照顧那麼古老的版本,它就是最省事的选择。
這種組合專門用來兜住 IE6 以下版本不認 margin: 0 auto 的問題,做法是在 body 上寫 text-align: center。
<style type="text/css">
body{text-align:center;}
.wrapper{width:750px;position:relative;margin:0 auto;text-align:left;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
此處的 text-align: center 實際上是被當作 CSS hack 在用——它本該只管文本對齊,卻被拿來實現容器居中,做了本職之外的事。也正因為如此,容器內部需要再寫一次 text-align: left 把文本對齊方式還原。
這一種稍麻煩,需要配合定位使用。
<style type="text/css">
.wrapper{width:750px;position:relative;left:50%;margin-left:-375px;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
思路是兩步:先讓容器相對文檔向右偏移 50%,再把左外邊距設為容器寬度的一半並取負值,兩者相加剛好抵消,容器便落在正中。這種寫法不含任何 hack,相容性極好,能在最廣泛的瀏覽器環境中表現一致。
以上就是我所知道的三種水平居中方案,都不算難,寫出來當作一次知識回顧與總結。