---
title: "CSS 居中大全"
url: "https://yjyj.net/css/4841.html"
author: "小 虾 (生猛大蝦编辑部)"
published_at: "2014-11-04T09:24:33+08:00"
updated_at: "2014-11-04T09:24:33+08:00"
categories: "前端杂烩"
tags: "CSS布局技巧, Docker容器, HTML/CSS, 前端开发实战, 网页视觉设计"
summary: "这篇《CSS 居中大全》系统梳理前端常见居中方案，从 text-align、vertical-align、line-height 到 margin:auto、绝对定位与 translate(-50%,-50%)，再到响应式与溢出处理，帮你快速判断不同场景该用哪种写法，少踩坑、少折腾。"
source: "生猛大蝦 (https://yjyj.net)"
---

# CSS 居中大全

> 来源：[生猛大蝦 (yjyj.net)](https://yjyj.net/css/4841.html) · 作者：小 虾 · 发布时间：2014-11-04

## 30 秒核心看点 (TL;DR)

- 水平居中、垂直居中、未知尺寸居中，本文把常见写法一次归纳清楚。
- 绝对定位配合 margin:auto 或 translate(-50%,-50%)，是实战里最常被拿来救场的方案。
- 内容尺寸不固定时要特别注意溢出，优先考虑 overflow:auto、max-height 等防翻车手段。

我看最近微博流行 CSS 居中技术，老外码农争相写相关的文章，一篇赛一篇的长啊，我把几篇归纳总结了一下，算是笔记。
孔乙己曾说：“茴香豆的回字有四种写法”，万一哪天有个面试官问你：“居中一共有几种写法”呢，哈哈，先备着吧~~
各种方法各有利弊，大家自己权衡吧，至少在需要居中时多个思路。

****
不建议用了。

**text-align:center**
在父容器里水平居中 inline 文字，或 inline 元素

**vertical-align:middle**
垂直居中 inline 文字，inline 元素，配合 display:table ，display:table-cell，有奇效。

**line-height**
与 height 联手，垂直居中文字

**margin:auto** 

```

Hello World

```

 

**hacks, hacks（小技巧）**
有许多 hacks ，负 margin，影子元素 ::before 等。如果你的内容不是固定大小的话，它们大部分是很脆弱的。

**translate(-50%,-50%)**
用 position 加 translate translate(-50%,-50%) 比较奇特，百分比计算不是以父元素为基准，而是以自己为基准。

示例： 

```

Hello World

```

 
这个技巧相当嚣张，同样适用于没固定大小的内容，min-width，max-height，overflow:scroll等。

**绝对定位居中**
父容器元素：position: relative 

```
.Absolute-Center {
  width: 50%;
  height: 50%;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
```

 
**注意：高度必须定义，建议加 overflow: auto，防止内容溢出。**

**视口居中**
内容元素：position: fixed，z-index: 999，**记住父容器元素 position: relative**

```
.Absolute-Center.is-Fixed {
  width: 50%;
  height: 50%;
  overflow: auto;
  margin: auto;
  position: fixed;
  top: 0; left: 0; bottom: 0; right: 0;
  z-index: 999;
}
```

 

**响应式**
百分比宽高，最大、最小宽度均可以，加 padding 也可以 

```
.Absolute-Center.is-Responsive {
  width: 60%;
  height: 60%;
  min-width: 400px;
  max-width: 500px;
  padding: 40px;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
```

 

**偏移**
只要 margin: auto; 在，内容块将垂直居中，top, left, bottom, right 可以设置偏移。 

```
.Absolute-Center.is-Right {
  width: 50%;
  height: 50%;
  margin: auto;
  overflow: auto;
  position: absolute;
  top: 0; left: auto; bottom: 0; right: 20px;
  text-align: right;
}
```

 

**溢出**
居中内容比父容器高时，防止溢出，加 overflow: auto （没有任何 padding 时，也可以加 max-height: 100%;）。 

```
.Absolute-Center.is-Overflow {
  width: 50%;
  height: 300px;
  max-height: 100%;
  margin: auto;
  overflow: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
```

 

**调整尺寸**
resize 属性可以让尺寸可调。 设置 min- /max- 限制尺寸，确定加了 overflow: auto 。 

```
.Absolute-Center.is-Resizable {
  min-width: 20%;
  max-width: 80%;
  min-height: 20%;
  max-height: 80%;
  resize: both;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
```

 

**图像**
图像同样适用，设置 height: auto; 

```
.Absolute-Center.is-Image {
  width: 50%;
  height: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
```

 

**可变高度**
高度必须定义，但可以是百分比或 max-height。不想定义高度的话，用 display: table （需要考虑 Table-Cell 兼容性）。 

```
.Absolute-Center.is-Variable {
  display: table;
  width: 50%;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
```

 

**负 margin**
确切知道宽高，负 margin 是宽和高的一半。 

```
.is-Negative {
width: 300px;
height: 200px;
padding: 20px;
position: absolute;
top: 50%; left: 50%;
margin-left: -170px; /* (width + padding)/2 */
margin-top: -120px; /* (height + padding)/2 */
}
```

 

**Table-Cell**
参考文章：Flexible height vertical centering with CSS, beyond IE7

HTML结构： 

```

  
    
    &lt!-- CONTENT -->
    


  



```

 
CSS样式： 

```
.Pos-Container.is-Table { display: table; }
.is-Table .Table-Cell {
  display: table-cell;
  vertical-align: middle;
}
.is-Table .Center-Block {
  width: 50%;
  margin: 0 auto;
}
```

 

**FlexBox**
参考文章：Designing CSS Layouts With Flexbox Is As Easy As Pie 

```
.Pos-Container.is-Flexbox {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
     -moz-box-align: center;
     -ms-flex-align: center;
  -webkit-align-items: center;
          align-items: center;
  -webkit-box-pack: center;
     -moz-box-pack: center;
     -ms-flex-pack: center;
  -webkit-justify-content: center;
          justify-content: center;
}
```

 


感谢原作者:http://www.csswang.com/exp/5837.html

## 延伸问答与排错指南 (FAQ)

### CSS 居中最常见的写法有哪些？
常见思路包括：text-align:center 做水平居中，vertical-align:middle 配合 table/table-cell 做垂直居中，margin:auto 做块级居中，以及 position:absolute/fixed 配合 top/left/right/bottom 和 transform:translate(-50%,-50%)。不同场景适用性差别很大。

### 内容大小不固定时怎么居中更稳？
内容尺寸不固定时，优先考虑绝对定位加 translate(-50%,-50%)，或绝对定位配合 margin:auto 的方案；同时建议加 overflow:auto，必要时配合 max-height，避免内容超出容器后布局崩掉。

### 为什么说有些居中技巧不建议长期用？
因为不少 hack 方案依赖固定尺寸或脆弱的结构假设，比如负 margin、伪元素等，一旦内容变长、字号变化或容器响应式调整，就容易失效。实战里更推荐可维护、可扩展的居中方式。

---
更多 Linux 运维、WordPress 性能优化、前端工程化、Shopify 独立站与极客折腾指南：[生猛大蝦全站文章归档 (https://yjyj.net/archive)](https://yjyj.net/archive)
